Input file and store in string

I'd like to input a file and store the contents of the file in a string.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    std::string inputFile();

    int main()
    {
        std::string fileContents = inputFile();
    }

    std::string inputFile()
        {
            std::string fileName;
            std::cout << "\nEnter file name, including path:\n";
            std::getline(std::cin, fileName);

            std::ifstream input(fileName.c_str());
            std::string buffer;
            std::string result; 

            if (!input.fail()) // if input does not fail
            {
                while (!input.eof()) 
                {
                    std::getline(input, buffer); 
                    result.append(buffer); 
                }
                input.close();

                return result; 
            }
            else
            {
                std::cout << "\nInvalid file name or path";
                inputFile(); // recursive call to inputFile
            }
        }


It works fine if the file name and path is input correctly.

But, if the file name or path is entered incorrectly, the recursive call to inputFile is executed, and the user is given another opportunity to enter the file. Then, if the file name is entered correctly an error is thrown in Visual Studio 2013:

"Unhandled exception at 0x77F7A9E8 (msvcr120d.dll) in Assignment4.exe: 0xC0000005: Access violation reading location 0xCCCCCCC0."

How could this be done better?
Last edited on
Don't loop on EOF!

I assume you mean to strip all EOLs from the string?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::string inputFile()
{
    std::string fileName;
    std::cout << "\nEnter file name, including path:\n";
    std::getline(std::cin, fileName);

    while (true)
    {
        std::ifstream input(fileName.c_str());
        if (input)
        {
            std::string buffer, result;
            while (std::getline(input, buffer)
            {
                result.append(buffer);
            }
            return result;
        }
        std::cout << "\nInvalid file name or path";
    }
}

Notice also that the original code, by stripping EOL, also joins things formerly separated. For example:

    Hello
    world!

becomes "Helloworld! (all one word -- no spaces).

If that is not the case (meaning, you meant to keep the newlines) you can do much simpler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::string inputFile()
{
    std::string fileName;
    std::cout << "\nEnter file name, including path:\n";
    std::getline(std::cin, fileName);

    while (true)
    {
        std::ifstream input(fileName.c_str());
        if (input)
        {
            std::stringstream buffer;
            buffer << input.rdbuf();
            return buffer.str();
        }
        std::cout << "\nInvalid file name or path";
    }
}

I feel obliged to point out one other thing:

This code does not give the user the option to escape. The only way to do so is with a ^C to kill the entire program. (Suppose the user cannot remember the full path, for example? Or changes his mind?)

Hope this helps.
Topic archived. No new replies allowed.