Xiang
05-30-2003, 01:46 PM
Dear sir,
Refers to the below coding.
I would like to capture the data from users, but in my data file it will displayed incorrect data. Why???
char *book_title;
char *author;
FILE *sfile;
void main(void)
{
sfile = fopen("c:\\BOOKREC.txt","a+");
fscanf(sfile, "%[^/]%*c", part.book_title);
fscanf(sfile, "%[^/]%*c", part.author);
gotoxy(25,12);
buffer[0] = 51; /* There's space for 50 characters plus the NULL terminator */
part.book_title = cgets(buffer);
if (strlen(part.book_title) != 0)
{
gotoxy(25,12); cprintf("%s", part.book_title);
}
else
{
gotoxy(7,23); cprintf("Do not leave the 'Book Title' field blank!");
delay(2000);
gotoxy(7,23); cprintf(" ");
}
}while (strlen(part.book_title) == 0);
do {
gotoxy(25,13);
buffer[0] = 51;
part.author = cgets(buffer);
if (strlen(part.author) == 0)
{
gotoxy(7,23); cprintf("Do not leave the 'Author' field blank!");
delay(2000);
gotoxy(7,23); cprintf(" ");
}
else
{
gotoxy(25,13); cprintf("%s", part.author);
}
}while (strlen(part.author) == 0);
//write remaining data to a data file
fprintf(sfile, "%s\n", part.book_title);
fprintf(sfile, "%s\n", part.author);
getch();
}
I have entered below data to my data file.
C programming (Book Title)
Alex (Author)
Java (Book Title)
Steven (Author)
But, in my data file, it will displayed the data as below.
Alex
Alex
Steven
Steven
Do you know why???
Thanks,
Xiang
Refers to the below coding.
I would like to capture the data from users, but in my data file it will displayed incorrect data. Why???
char *book_title;
char *author;
FILE *sfile;
void main(void)
{
sfile = fopen("c:\\BOOKREC.txt","a+");
fscanf(sfile, "%[^/]%*c", part.book_title);
fscanf(sfile, "%[^/]%*c", part.author);
gotoxy(25,12);
buffer[0] = 51; /* There's space for 50 characters plus the NULL terminator */
part.book_title = cgets(buffer);
if (strlen(part.book_title) != 0)
{
gotoxy(25,12); cprintf("%s", part.book_title);
}
else
{
gotoxy(7,23); cprintf("Do not leave the 'Book Title' field blank!");
delay(2000);
gotoxy(7,23); cprintf(" ");
}
}while (strlen(part.book_title) == 0);
do {
gotoxy(25,13);
buffer[0] = 51;
part.author = cgets(buffer);
if (strlen(part.author) == 0)
{
gotoxy(7,23); cprintf("Do not leave the 'Author' field blank!");
delay(2000);
gotoxy(7,23); cprintf(" ");
}
else
{
gotoxy(25,13); cprintf("%s", part.author);
}
}while (strlen(part.author) == 0);
//write remaining data to a data file
fprintf(sfile, "%s\n", part.book_title);
fprintf(sfile, "%s\n", part.author);
getch();
}
I have entered below data to my data file.
C programming (Book Title)
Alex (Author)
Java (Book Title)
Steven (Author)
But, in my data file, it will displayed the data as below.
Alex
Alex
Steven
Steven
Do you know why???
Thanks,
Xiang