Hi. I was trying to figure out how to read and write files in C++. To understand it better I wrote this code on "codepile.com": https://www.codepile.net/pile/WDPkzrep. What it does is it allows a user to create their own text file then write information in it. Lastly, the contents of the file will be read.

The problem is that it will allow a user create a text file but will not allow them to type anything into the file. Also, because the program stops after the line that says "please enter text:" (keep in mind that the program will not allow user to type anything at this point), I does not run the last part of the code which is to read what was in the file.

How would I fix this? Should I break the program up into functions and call the functions? Thank you.

Here is the code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string> 

using namespace std;

int main()
{
    cout << "Type in file name followed by the extention \".txt\"" << endl;
    string user_file;
    cin >> user_file;
    fstream file; //object of fstream class

   //opening file "sample.txt" in out(write) mode
    file.open(user_file, ios::out);

    if (!file)
    {
        cout << "Could not open file" << endl;
        return 0;
    }

    cout << "File created." << endl;
    //write text into file
    string user_input;
    cout << "Please enter text: \n";
    getline(cin, user_input);
    file << user_input;
    //cout << "For testing. User input is: " + user_input<< endl;

    file.close();//close the file
    //system("pause");

    //open file to be read
    file.open(user_file, ios::in);
    if (!file)
    {
        cout << "Could not open file" << endl;
        return 0;
    }

    char ch; //read character
    cout << "File Information: ";

    while (!file.eof())
    {
        file >> ch;
        cout << ch;
    }
    file.close(); 
    return 0;
}

Just like scanf() and the JAVA Scanner Class, when you >> the next string, int, double, char, etc., then it reads, initially skipping over white space, into the variable from input until it has what you want, and when it hits an incompatible character, like white space for a string, it pushes that character back into the IO buffer. http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

So, your line feed gets pushed back and waits for the next cin call. This is way common, but you can just read a char or do getline() to flush out the linefeed, or do a cin.getline() for the file name, which discards the linefeed. http://www.cplusplus.com/reference/istream/istream/getline/

So your saying that it is skipping some white spaces so I need a way to delete some white spaces? May be I don't understand what you mean. Also, I tried writing line 28 a different way based on the link you gave. It still doesn't work. Is there another way I can do this?

What is weird about this is that if I cut out the part of the code that allows the user to write the file name (meaning that I just put a default file name instead of allowing the user to type in thier own file name) then the code works. However, I don't see why this version won't work. Here is the new code I have written:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string> 

using namespace std;

int main()
{
    cout << "Type in file name followed by the extention \".txt\"" << endl;
    string user_file;
    cin >> user_file;
    fstream file; //object of fstream class

   //opening file "sample.txt" in out(write) mode
    file.open(user_file, ios::out);

    if (!file)
    {
        cout << "Could not open file" << endl;
        return 0;
    }

    cout << "File created." << endl;

    //write text into file
    char user_input[256];
    cout << "Please enter text: \n";
    std::cin.getline(user_input, 256);
    file << user_input;
    //cout << "For testing. User input is: " + user_input<< endl;

    file.close();//close the file
    system("pause");

    //open file to be read
    file.open(user_file, ios::in);

    if (!file)
    {
        cout << "Could not open file" << endl;
        return 0;
    }

    cout << "File Information: ";
    std::string line_;

    if (file.is_open())
    {
        while (getline(file, line_))
        {
            std::cout << line_ << endl;
        }
        file.close();
        std::cin.get();//pause screen
    }

    /*char ch; //read character
    cout << "File Information: ";

    while (!file.eof())
    {
        file >> ch;
        cout << ch;
    }

    file.close(); */

    return 0;
}
commented: "As it was foretold." +15

I wonder why I can't find information on this? Is it uncommon for people to handle files like this (what I mean is that do most people have a default file that people can write in rather than allowing the user to write in it because when I do it like that it works. However, I want to give the user the option of making thier own file an writting in it)? I don't know why that would make a difference though?

EOF check between read and write, use get() to pick up white space characters, getline() removes the lew line.

dgp@dgp-p6803w:~
$ myCC c++rw
dgp@dgp-p6803w:~
$ c++rw
Type in file name followed by the extention ".txt"
x.txt
File created.
Please enter text: 
hohoho
File Information: hohoho
dgp@dgp-p6803w:~
$ 

#include <iostream>
#include <fstream>
#include <sstream>
#include <string> 
using namespace std;
int main()
{
    cout << "Type in file name followed by the extention \".txt\"" << endl;
    string user_file;
    getline( cin, user_file );
    fstream file; //object of fstream class
   //opening file "sample.txt" in out(write) mode
    file.open(user_file, ios::out);
    if (!file)
    {
        cout << "Could not open file" << endl;
        return 0;
    }
    cout << "File created." << endl;
    //write text into file
    string user_input;
    cout << "Please enter text: \n";
    getline(cin, user_input);
    file << user_input << endl ;
    //cout << "For testing. User input is: " + user_input<< endl;
    file.close();//close the file
    //system("pause");
    //open file to be read
    file.open(user_file, ios::in);
    if (!file)
    {
        cout << "Could not open file" << endl;
        return 0;
    }
    char ch; //read character
    cout << "File Information: ";
    while ( true )
    {
        file.get( ch );
    if (file.eof())break;
        cout << ch;
    }
    file.close(); 
    return 0;
}

Wow. Thank You DGPickett. This works. Hopeful I will get better at programming and learn how to solve these types of problems.

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.