I've never had a problem with files before, but now I can't seem to get it to work properly.

I have this code:

fstream someFile("file.txt");
    switch(choice){
        case 1:
            cout << "Enter date: ";
            cin >> date2;
            cout << "Month: ";
            cin >> month2;
            cout << "Year: ";
            cin >> year2;
            cout << "Enter name: ";
            cin >> name;
            cout << "Enter any other information: ";
            getline(cin, info);
            someFile << date2 << " " << month2 << " " << year2 << " " << name << " " << info;
            break;
    }

When I use getline(cin, info) instead of cin >> info, it doesn't let me input anything when I run the program, and it doesn't create the file.

I've never had a problem with files before, but now I can't seem to get it to work properly.

I have this code:

fstream someFile("file.txt");
    ...

Is the file opened for read, write, append, ???
Did the open return an error?

Is the file opened for read, write, append, ???
Did the open return an error?

I opened it for append, by using ("file.txt", ios_base::app).

It works without the append, but not with it.

Bump. I really need help with this asap :/

Edit:

I have this code now:

fstream someFile("somefile.txt", ios_base::app);
    switch(choice){
        case 1:
            cout << "Enter date: ";
            cin >> date2;
            cout << "Month: ";
            cin >> month2;
            cout << "Year: ";
            cin >> year2;
            cout << "Enter name: ";
            cin >> name;
            cout << "Enter any other information: ";
            cin.get();
            getline(cin, info);
            someFile << date2 << " " << month2 << " " << year2 << " " << name << " " << info << endl;
            someFile.close();
            break;
        case 2:
            cout << endl;
            while(someFile >> date2 >> month2 >> year2 >> name){
                if((date==date2 && month==month2) && year==year2){
                    getline(someFile, info);
                    cout << "Date: " << date2 << "/" << month2 << "/" << year2 << "\nName: " << name << "\nInfo:" << info << "\n\n";
                }
            }
            break;
    }

The program works perfectly when case 2 is deleted; it creates the file somefile.txt and it writes the correct stuff to it. But as soon as I add case 2, which uses file input, it doesn't write anything to the file when choice == 1. I think it's something to do with using fstream, but I'm not sure.

Edited 5 Years Ago by ben1996123: n/a

What does append mean? What does choice 2 do?

append = opens the file without deleting whatever is in it.
choice 2 = checks the date that was entered when the program first opens, prints out the date, name and info if the date in the file is the same as the date that was entered in the program.

append = opens the file without deleting whatever is in it.
choice

Wrong. Open at the end of the file for writing...

2 = checks the date that was entered when the program first opens, prints out the date, name and info if the date in the file is the same as the date that was entered in the program.

What does it do with the file? doesn't it read? Isn't that at odds with opening the file
a) at the end? What's it going to read?
b) for writing? How's it going to read?

Change where and how you are opening to file so you open it properly for the task you want to perform.

Change where and how you are opening to file so you open it properly for the task you want to perform.

Well I know that... I just don't know how to...

I tried something like this:

ofstream someFile("file.txt", ios_base::app);
    ifstream someOtherFile("file.txt");

but that didn't work either...

Well I know that... I just don't know how to...

I tried something like this:

ofstream someFile("file.txt", ios_base::app);
    ifstream someOtherFile("file.txt");

but that didn't work either...

Then you did it wrong. Since you didn't bother to post the bad code, we can't very well help.

Before you just post more code and ask us to tell you what to do, think about it...
1) How do you open a file for writing? You seem to know this one OK.
2) Where do you need to open the file for writing? What lines of code does the file have to be open for?
3) How do you open a file for reading?
4) Where do you need to open the file for reading?

Then you did it wrong. Since you didn't bother to post the bad code, we can't very well help.

Before you just post more code and ask us to tell you what to do, think about it...
1) How do you open a file for writing? You seem to know this one OK.
2) Where do you need to open the file for writing? What lines of code does the file have to be open for?
3) How do you open a file for reading?
4) Where do you need to open the file for reading?

1. ofstream
2.

someFile << date2 << " " << month2 << " " << year2 << " " << name << " " << info << endl;

3. ifstream
4. choice 2

OK....
So open the file in the correct way in the correct place, do the read or write, close the file.
Test the code.
If it still doesn't work, post an explanation of exactly what happened and the code you ran.

This article has been dead for over six months. Start a new discussion instead.