G
gogomei
I have a text file like following and need to read out the names of each
person. Since the only accurate info is there is a null line before
starting a new name, I have written following code to take out the
names, but it doesn't work. Looks like "strcmp(tmp,"\n")==0" is never
reached. How can I do?
Thank you!
my code:
FILE * fp;
char tmp[1024];
if((fp = fopen("Membership.txt", "r")) == NULL)
{
printf("Open Membership.txt error!\n");
_exit(EXIT_FAILURE);
}
if(fscanf(fp,"%s",tmp) == EOF)
{
printf("Date file over or Read data file error.\n");
break;
}
if((strcmp(tmp," \n"))==0){
fscanf(fp,"%s",tmp);
printf("%s \n ",tmp);
}
Membership.txt:
Bell, Dennis A.
NI Gatural Xeatures Inventory
Mason Bldg
PO Box 30456
Okemos, XY12345-7944
636.455.1552
4, 8, 9, 10
Clixia, Wichelle R.
536 W Bbcroft St #3
Toledo, XY 12345-3240
(e-mail address removed)
Ally, John W.
4 Oxford Rd
East Lansing 48823
P 430.852.7590
(e-mail address removed)
3, 16, 6, 9, 13
...
person. Since the only accurate info is there is a null line before
starting a new name, I have written following code to take out the
names, but it doesn't work. Looks like "strcmp(tmp,"\n")==0" is never
reached. How can I do?
Thank you!
my code:
FILE * fp;
char tmp[1024];
if((fp = fopen("Membership.txt", "r")) == NULL)
{
printf("Open Membership.txt error!\n");
_exit(EXIT_FAILURE);
}
if(fscanf(fp,"%s",tmp) == EOF)
{
printf("Date file over or Read data file error.\n");
break;
}
if((strcmp(tmp," \n"))==0){
fscanf(fp,"%s",tmp);
printf("%s \n ",tmp);
}
Membership.txt:
Bell, Dennis A.
NI Gatural Xeatures Inventory
Mason Bldg
PO Box 30456
Okemos, XY12345-7944
636.455.1552
4, 8, 9, 10
Clixia, Wichelle R.
536 W Bbcroft St #3
Toledo, XY 12345-3240
(e-mail address removed)
Ally, John W.
4 Oxford Rd
East Lansing 48823
P 430.852.7590
(e-mail address removed)
3, 16, 6, 9, 13
...