Hi,
I have been playing with the write to file functions lately which are very useful and mostly straight forward. However I have come across one problem I can't get my head around. The problem is that when I read a few lines characters and hit return for each line.... every second line is not read - starting with the first one.
I entered the following and hit return after each "line"
1
2
3
4
5
6
7
8
New Proverbs written to file.
About to read proverbs from file.
The proverbs in the file are:
Many a mickle makes a muckle.
Too many cooks spoil the broth.
He who laughs last didn't get the joke in the first place.
2 <----- These are the new entries appended to the file.
4
6
8
Where is 1,3,5,7? I believe this is down to the Carriage return and the buffer but I can't explain it. I double checked that my getch() calls were not causing this.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int LENGTH = 80; /* maximum input length */
int main(void)
{
int i=0;
char *proverbs[] =
{ "Many a mickle makes a muckle.\n",
"Too many cooks spoil the broth.\n",
"He who laughs last didn't get the joke in"
" the first place.\n"
};
char more[LENGTH]; /* store a new proverb */
FILE *pfile = NULL;
char *filename = "c:\\myfile.txt";
/* create a new file, if myfile does not already exist */
if(!(pfile=fopen(filename,"w")))
{
printf("\nError opening %s for writing. Program terminated.",filename);
exit(1);
}
/* write out first three proverbs to the file */
printf("\nNew file created. About to write out first three proverbs to the file.");
/* getch(); */
int feedback = sizeof proverbs;
printf("\nsizeof proverbs:%d",feedback);
feedback = sizeof proverbs;
printf("\nsizeof proverbs[0]:%d",feedback);
int count = sizeof proverbs / sizeof proverbs[0]; /* result = 16/16 = 1 */
for(i=0; i<count; i++)
fputs(proverbs[i], pfile);
fclose(pfile);
printf("\nProverbs written to file.");
/* getch(); */
/* open the file to append more proverbs */
if(!(pfile=fopen(filename,"a")))
{
printf("\nError opening %s for writing. Program terminated.",filename);
exit(1);
}
printf("\nEnter proverbs of less than 80 characters to append to file or press Enter to end:\n");
while(fgets(more, LENGTH, stdin))
{
fgets(more, LENGTH, stdin); /* read a proverb */
if(more[0]=='\n') /* if its empty line */
break; /* end input operation */
fputs(more, pfile); /* write the new proverb */
}
fclose(pfile);
printf("\nNew Proverbs written to file.");
/* getch(); */
if(!(pfile=fopen(filename,"r")))
{
printf("\nError opening %s for reading. Program terminated.",filename);
exit(1);
}
printf("\nAbout to read proverbs from file.");
/* getch(); */
/* read and output the file contents */
printf("\nThe proverbs in the file are:\n\n");
while(fgets(more, LENGTH, pfile)) /* read a proverb */
printf("%s", more); /* display it */
fclose(pfile);
remove(filename);
printf("\nPress any key to exit.");
getch();
return(0);
}
I think I have come across this ghost before, and I'm sure its the buffer - maybe I need to use flush?