【发布时间】:2020-03-07 14:10:36
【问题描述】:
我同时使用 fread() 和 fseek() 来收集字符串的一部分。我没有在整行上使用 fread() 。
我会采取整条线,但据我所知,您不能在字符数组上使用 fseek() 对吗?
`int parse(const char *f, const struct stat *flightD, int type){
//file pointer
Airport_S *Air_Pts = (Airport_S *)malloc(sizeoftype(Airport_S));
FILE *fp;
//char need[10];
char airLineFile[2];
char chkAirPt[3];
fp = fopen(f, "r");
if(type == FTW_F){ // test for 'type' of FTW_F
//check to see if the file opened successfully
if(fp == NULL)
printf("Cannot open file %s", f)
return 1;
while (!(FEOF)){
//fgets(need,10,fp)
//must return zero to parent funtion to continue tree traversal
// ?? While current dir != originally called dir?
//open the file, read it's contents and assess them
fseek(fp, 5, SEEK_SET) //set FP to right before airport code
chkAirPt = fread(chkAirPt,sizeof(char),3, fp)
fseek(fp,0,SEEK_SET);
//combine the airline abbreviation with '.txt'
airLineFile = strcat(fread(airLineFile, sizeof(char), 2, fp),".txt");
//if the struct has no values in it, populate it with this first one.
if(Air_Pts->airport == NULL){
//Set info for very first node
Air_Pts->airPt=strcpy(Air_Pts->airport, chkAirPt);
fseek(fp,0,SEEK_SET);
Air_Pts->fltInfo->airLine=airLineFile;
Air_Pts->fltInfo->next = NULL;
Air_Pts->fltInfo->prev = NULL;
Air_Pts->next = NULL;
Air_Pts->prev = NULL;
//what is the file going to do after this?
}
else if(strcmp(Air_Pts->airport, chkAirPt) == 0){
if(strcmp(Air_Pts->fltInfo->airLine, airLineFile) == 0){
Air_Pts->fltInfo->occ++;
}
else
Air_Pts->fltInfo = addAirline(Air_Pts->fltInfo);
}
// some code
return 0;
else //anything other than a file -or- FTW_D
return 1;
}
}
}`
【问题讨论】:
-
“整行不使用 fread()”是什么意思? fread 对“线条”一无所知。它读取数据。如果您想在该数据中搜索“\n”,这取决于您。此外,使用全局并不能解决 stackoverflow.com/questions/5431941/… 中概述的问题
-
我只使用 fread() 读取第一行的几个字符如果我没记错,那么我想转到下一行并重新做一遍我需要这些字符:_ _ _ _ ! ! ! _然后回到开头,得到前两个:! ! _ _ _ _ _ _。然后我需要转到下一行并重新执行,直到到达文件末尾。
-
如果你不阅读整个当前行,你怎么知道下一行从哪里开始?
-
while (!(FEOF)){是什么?请阅读Why is “while ( !feof (file) )” always wrong?。 -
应该是--while(!(EOF){}--?
标签: c struct fread fseek char-pointer