fscanf,fgets,fread,fgetc

2024-11-27 16:44:57
推荐回答(1个)
回答1:

注意修改下names.txt的路径,我是在d盘下

#include
#include

int main()
{
FILE *fp;
char buf[20],c;
char str[100][10];
int count=0;
if((fp=fopen("d:\\names.txt","r"))==NULL)
{
printf("open names.txt failed");
getchar();
return -1;
}

while(1)
{
if(fscanf(fp,"%[^,]",buf)<=0) break;
buf[strlen(buf)-1]='\0';
strcpy(str[count],buf+1);
puts(str[count]);
count++;
if(fscanf(fp,"%c",&c)<=0) break;
}
fclose(fp);
getchar();
return 0;
}