File I/O Problem

Posted 21 August 2009 - 01:47 AM

Hi there, im having trouble with a project of mine. I am creating a program/database for storing cigar information. Specifically the brand, name and other specificts are saved individually to structs and the structs are connected via simple linked list at the moment. There is an empty text file to begin with and when the program runs you can insert as many cigars into the linked list as you want but at the end of the program I want to save the information to the file. I know how to do this normally but I seem to have some trouble because if I wish to start the program again I want it to upload the list I just created previously. The source file uses a few member functions from my class. It first uses the upload function I have which puts any data in the file into the linked list. Then the interface function which then will let you do varios things to the list such as searching, deleting, adding more, updating information etc. After completion of that function I call the final function save. I want this function to erase everything in the text file then save all the information from this linked list to the file. For some reason i cant get it to save, erase, and write properly. I would appreciate any help!!!!

The exact problem I am having is that my Upload() function isnt working and I dont know why? Code is below and class is attached.

also your UpLoad() is reading data into local variables - you need to add the data to your linked list

Thanks again for the help, it put me one ste closer but there is still a bug in it. It only grabs the data in the fle if I just created it on the previous run. When it grabs the data, the data is outputted twice. And when I put the code in to insert into the linked list the program doesnt run at all.

Re: File I/O Problem

Although I use C i have this solution that I think will fix the one extra execution of the while loop.

The end of file is true only if the last read failed.

You should check for eof just after reading a line and before performing any actions on the the values, to see if eof becomes true. If it does you should immediately break out of the while loop.

If you perform the check after performing some actions e.g. printing the values just read before checking for an eof it will invariably cause an extra line to be printed.

I present both the correct and the incorrect versions below. Although they are in C I hope it will help you understand the logic.

Although EOF is read in both the cases the first code checks for valid data before printing it while the second while loop performs this checks only after printing the data.

FILE *fp;
fp = fopen("filename","mode"); // open stream for reading
/***********correct***************/
while(1)
{
fscanf("%d %d %d",&vara,&varb,&varc); //try reading a line
if(eof(fp)) //if the line just read was end of file
{
break; //break out of the while loop
}
/****print only after the check has been performed**/
printf("%d %d %d",vara,varb,varc);
}
/***************incorrect************/
while( !eof(fp))
{
fscanf("%d %d %d",&vara,&varb,&varc);
printf("%d %d %d",vara,varb,varc);
}

The last version is buggy. Because when the last [b] valid[\b] line is read eof(fp) is false because the end of file hasn't been reached. So the while loop executes once more reading EOF and storing unwanted data into the variables etc. It is only now that eof(fp) becomes true and the while loop ends.