【发布时间】:2014-03-21 21:54:36
【问题描述】:
我有一个 txt 文件,其中的单词和数字用逗号分隔。我想读取字符直到下一个逗号,处理数据,然后从找到最后一个逗号的位置继续读取。我使用 fgetc(),但不确定它是否更新了 FILE 指针中的最后读取位置。
我遵循了这里建议的一般想法,这还行不通,但已经接近了。一开始的条件检查似乎效果不佳(EOF)。似乎在复制航空公司名称时我得到了一个额外的字符,然后它就会崩溃。
// Read data from file, data is comma delimited!
flight* read_from_text()
{
#define DATA_CHUNK 20
FILE *fp;
flight temp_data;
flight *data=malloc(sizeof(*data));
data=&temp_data;
char buffer[DATA_CHUNK];
int c=0,n=0,i=0,state=0;
// Open file for reading
if((fp=fopen("c:\\data.txt","r"))==NULL)
{
printf("Error opening flight data file.");
return NULL;
}
// read a single entry from file
while(1)
{
while(((c=fgetc(fp))!=',')||(c=!EOF))
buffer[n++]=(char)c;
if(c==EOF) break;
switch(state)
{
case CODE:
// Check if flight code is valid
if((buffer[0]<'0')||(buffer[0]>'9')||(buffer[1]<'0')||(buffer[1]>'9'))
printf("Error reading in flight number\n");
else
temp_data.code=atoi(buffer);
state++;
break;
case AIRLINE_NAME:
// Check airline name length is OK
if(n>(sizeof(temp_data.airline_name)))
printf("Airline name is too long, some characters will be cut\n");
strncpy(temp_data.airline_name,buffer,n);
state++;
break;
case DESTINATION:
if(n>(sizeof(temp_data.destination)))
printf("Destination name is too long, some characters will be cut\n");
strncpy(temp_data.destination,buffer,n);
state++;
break;
case RESERVED_SEATS:
temp_data.reserved_seats=atoi(buffer);
state++;
break;
case DATE:
if(n>(sizeof(temp_data.date)))
printf("Date format is too long, might be corrupted\n");
strncpy(temp_data.date,buffer,n);
state=0;
break;
}
// Clear buffer
for(i=0;i<DATA_CHUNK;i++)
buffer[i]='\n';
n=0;
}
printf("%d\n",temp_data.code);
printf("%s\n",temp_data.airline_name);
printf("%s\n",temp_data.destination);
printf("%d\n",temp_data.reserved_seats);
printf("%s\n",temp_data.date);
getchar();
return data;
}
【问题讨论】:
-
您可以逐行动态读取它,然后用逗号对其进行标记。
-
当然 fgetc 会更新 FILE 指针中的最后读取位置。如果 yozu 有一个包含“ABC”的文件,第一个 fgetc 将返回 'A',第二个 'B' 以此类推。