vishalkhialani
03-26-2007, 04:41 PM
Hello,
Please see my below code. Once you run it the data shows up twice from the file. Please advice why does this happen ?
/* fscanf example */
#include <stdio.h>
//creating a structure
struct attribute
{
int style;
char buyer[44];
}att;
int status=1;
int main ()
{
int stylenew;
char buyernew[44];
FILE * pFile;
printf("Please enter style number\n");
scanf("%d",&att.style);
printf("Please enter buyername\n");
scanf("%s",att.buyer);
pFile = fopen ("myfile.txt","a+");
fprintf (pFile, " %d %s", att.style,att.buyer);
rewind (pFile);
while(status!=EOF)
{
status=fscanf(pFile, "%d", &stylenew);
printf("--------------\n");
printf("%d",stylenew);
printf("+++++++++++++++++\n");
status=fscanf (pFile, "%s", buyernew);
printf("@@@@@@@@@@@@@@@@@@@@@@\n");
printf("%s",buyernew);
printf("$$$$$$$$$$$$$$$$$$$$$$$\n");
}
fclose(pFile);
return 0;
}
Please see my below code. Once you run it the data shows up twice from the file. Please advice why does this happen ?
/* fscanf example */
#include <stdio.h>
//creating a structure
struct attribute
{
int style;
char buyer[44];
}att;
int status=1;
int main ()
{
int stylenew;
char buyernew[44];
FILE * pFile;
printf("Please enter style number\n");
scanf("%d",&att.style);
printf("Please enter buyername\n");
scanf("%s",att.buyer);
pFile = fopen ("myfile.txt","a+");
fprintf (pFile, " %d %s", att.style,att.buyer);
rewind (pFile);
while(status!=EOF)
{
status=fscanf(pFile, "%d", &stylenew);
printf("--------------\n");
printf("%d",stylenew);
printf("+++++++++++++++++\n");
status=fscanf (pFile, "%s", buyernew);
printf("@@@@@@@@@@@@@@@@@@@@@@\n");
printf("%s",buyernew);
printf("$$$$$$$$$$$$$$$$$$$$$$$\n");
}
fclose(pFile);
return 0;
}