對UTF-8編碼的文本檔案讀取出了問題,fgets讀取怎么不是按行讀取,
簡單的fgets()測驗代碼:
#include <stdio.h>
#include <cstring>
#define MAXSTRPATH 1024
int main()
{
char* cfgfile="gamp.cfg";
FILE *fp=NULL;
char *p,tmp[MAXSTRPATH]={'\0'};
if ((fp=fopen(cfgfile,"r"))==NULL) {
printf("*** ERROR: open configure file failed, please check it!\n");
return 0;
}
while (!feof(fp)) {
tmp[0]='\0';
fgets(tmp,MAXSTRPATH,fp);
printf("%s\n",tmp );
}
fclose(fp);
return 1;
}
待讀取的文本:
# GNSS data Analysis software for Multi-constellation and multi-frequency Precise positioning (GAMP) options
obs file/folder = 1 %(0:file 1:folder)
= /path
start_time = 0 2020/12/24 06:00:00.0 %(0:from obs 1:from inp)
end_time = 0 2020/12/24 23:59:30.0 %(0:from obs 1:from inp)
posmode = 6 %(0:spp 6:ppp_kinematic 7:ppp_static)
soltype = 0 %(0:forward 1:backward 2:combined-bf 3:combined-fb)
實際輸出結果:
# GNSS data Analysis software for Multi-constellation and multi-frequency Precise positioning (GAMP) options
start_time = 1 0000/2020/12f 25.00:03:0.000000 3.000000:0.000000:0.000000x/CLionProjects/PPP_VS2Linux/cfg/data
start_time = 1 0000/2020/12f 25.00:03:0.000000 3.000000:0.000000:0.000000
end_time = 1 0000/2020/12f 25.00:05:59.00000 5.000000:59.000000:30.000000osmode = 6 %(0:spp 6:ppp_kinematic 7:ppp_static)
soltype = 0 %(0:forward 1:backward 2:combined-bf 3:combined-fb)
多出來幾行而且亂碼,求論壇大佬解答下問題!!!
uj5u.com熱心網友回復:
#include <stdio.h>
#include <cstring>
#define MAXSTRPATH 1024
int main()
{
char* cfgfile="gamp.cfg";
FILE *fp=NULL;
char *p,tmp[MAXSTRPATH]={'\0'};
if ((fp=fopen(cfgfile,"r"))==NULL) {
printf("*** ERROR: open configure file failed, please check it!\n");
return 0;
}
while (1) {
if (NULL==fgets(tmp,MAXSTRPATH,fp)) break;
printf("%s",tmp );
}
fclose(fp);
return 1;
}
uj5u.com熱心網友回復:
趙老師
,還是一樣的啊;# GNSS data Analysis software for Multi-constellation and multi-frequency Precise positioning (GAMP) options
start_time = 1 0000/2020/12f 25.00:03:0.000000 3.000000:0.000000:0.000000x/CLionProjects/PPP_VS2Linux/cfg/data
start_time = 1 0000/2020/12f 25.00:03:0.000000 3.000000:0.000000:0.000000
end_time = 1 0000/2020/12f 25.00:05:59.00000 5.000000:59.000000:30.000000osmode = 6 %(0:spp 6:ppp_kinematic 7:ppp_static)
soltype = 0 %(0:forward 1:backward 2:combined-bf 3:combined-fb)
uj5u.com熱心網友回復:
d:\test>type gamp.cfg
# GNSS data Analysis software for Multi-constellation and multi-frequency Precise positioning (GAMP) options
obs file/folder = 1 %(0:file 1:folder)
= /path
start_time = 0 2020/12/24 06:00:00.0 %(0:from obs 1:from inp)
end_time = 0 2020/12/24 23:59:30.0 %(0:from obs 1:from inp)
posmode = 6 %(0:spp 6:ppp_kinematic 7:ppp_static)
soltype = 0 %(0:forward 1:backward 2:combined-bf 3:combined-fb)
d:\test>test
# GNSS data Analysis software for Multi-constellation and multi-frequency Precise positioning (GAMP) options
obs file/folder = 1 %(0:file 1:folder)
= /path
start_time = 0 2020/12/24 06:00:00.0 %(0:from obs 1:from inp)
end_time = 0 2020/12/24 23:59:30.0 %(0:from obs 1:from inp)
posmode = 6 %(0:spp 6:ppp_kinematic 7:ppp_static)
soltype = 0 %(0:forward 1:backward 2:combined-bf 3:combined-fb)
d:\test>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/264900.html
標籤:C++ 語言
