Hi

I have a program i was writing to solidify my knowledge of files and how to handle them. I seem to have hit a snag though. What the program does is create files based on a user inputted date and allows them to add content to the files (kind of like a schedule book). The feature I am having trouble with is the part of the program that displays the contents of the file. It starts at a user inputted date and searches for files within a given range of that date. The problem is that the program will only display a file's contents if it is on the date entered by the user.

e.g: User enters in 1-5-08 for the date. He knows he has files saved for 1-5, 1-6, and 1-8-08 but only the one on 1-5-08 is displayed.

I tracked the file name through the functions it goes through and it stays correct all the while but it still won't open when I need it.

I have posted all the relevant code for you to look at.

This is the function that creates the file name from the date:

void fileName()
{
     ifstream fin;
     ofstream fout;
     
     fout.open("fileCreate");
     fout << month;
     fout << day;
     fout << year;
     fout.close();
     
     fin.open("fileCreate");
     fin >> file;
     fin.close();
     
     DeleteFile("fileCreate");
     
     return;
}

This is the function that opens and reads the file:

void viewEntries()
{
     ifstream fin;
     ofstream fout;
     
     cout << "Date to start searching:" << endl;
     cout << "Year: ";
     cin >> year;
     cout << "Month: ";
     cin >> month;
     cout << "Day: ";
     cin >> day;
     cout << endl;
     dayCount=daysInAdvance;
     
     for (dayCount; dayCount>0; dayCount--)
     {
         dayOverlap();
         fileName();
         fin.open(file);
         if (!fin)
         {
            cout << "\nNo entry on: " << month;
            cout << "-" << day;
            cout << "-" << year;
         }
         while (fin.get(ch))
               cout << ch;
         fin.close();
         else
         {
            cout << "\nEntry found on: " << month;
            cout << "-" << day;
            cout << "-" << year << endl << endl;
            while (fin.get(ch))
                  cout << ch;
         }
         fin.close();
         day++;
     }
     cout << endl;
     menu();
}

Hi

I have a program i was writing to solidify my knowledge of files and how to handle them. I seem to have hit a snag though. What the program does is create files based on a user inputted date and allows them to add content to the files (kind of like a schedule book). The feature I am having trouble with is the part of the program that displays the contents of the file. It starts at a user inputted date and searches for files within a given range of that date. The problem is that the program will only display a file's contents if it is on the date entered by the user.

e.g: User enters in 1-5-08 for the date. He knows he has files saved for 1-5, 1-6, and 1-8-08 but only the one on 1-5-08 is displayed.

I tracked the file name through the functions it goes through and it stays correct all the while but it still won't open when I need it.

I have posted all the relevant code for you to look at.

This is the function that creates the file name from the date:

void fileName()
{
     ifstream fin;
     ofstream fout;
     
     fout.open("fileCreate");
     fout << month;
     fout << day;
     fout << year;
     fout.close();
     
     fin.open("fileCreate");
     fin >> file;
     fin.close();
     
     DeleteFile("fileCreate");
     
     return;
}

This is the function that opens and reads the file:

void viewEntries()
{
     ifstream fin;
     ofstream fout;
     
     cout << "Date to start searching:" << endl;
     cout << "Year: ";
     cin >> year;
     cout << "Month: ";
     cin >> month;
     cout << "Day: ";
     cin >> day;
     cout << endl;
     dayCount=daysInAdvance;
     
     for (dayCount; dayCount>0; dayCount--)
     {
         dayOverlap();
         fileName();
         fin.open(file);
         if (!fin)
         {
            cout << "\nNo entry on: " << month;
            cout << "-" << day;
            cout << "-" << year;
         }
         while (fin.get(ch))
               cout << ch;
         fin.close();
         else
         {
            cout << "\nEntry found on: " << month;
            cout << "-" << day;
            cout << "-" << year << endl << endl;
            while (fin.get(ch))
                  cout << ch;
         }
         fin.close();
         fin.clear ();  // add this line
         day++;
     }
     cout << endl;
     menu();
}

You may need to post more code. I think I get the idea, but I'm not positive. However, a few thoughts. One, I'm confused by the while loop in red above. It's between an "if" block and an "else" block. When is this supposed to execute? Right now it appears to be executing even when the file doesn't open, which doesn't seem like something you want, if it even compiles, which I doubt.

Second, you need to clear the fail bits after you get to the end of the file (see green above):

Once the fail bits are set, they stay set until you clear them, even if you have a new file. Add the line in green above and see if it helps. If it doesn't, I think you have to post some more code and probably the exact text files and the exact input you give, along with the exact results. But I think this might be your problem, possibly.

http://www.cplusplus.com/reference/iostream/ios/clear.html

Thank you so much. It works great now. I actually ended up doing a few things. I put in fin.clear() like you said but I also moved an "else" block and deleted a fin.close().

You were wondering what the for loop does. It tells the program how many files to look for before terminating.

Again thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.