简体   繁体   中英

C++ (Visual Studio): Recieving nothing when trying to read input from a .txt file

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string line = "test";
ifstream myfile("example.txt");
myfile.open("example.txt");

if (myfile.is_open())
{
    cout << line << "\n";
    cout << "File Opened\n";
    getline(myfile, line);
    cout << line;
    while (getline(myfile, line))
    {
        cout << line << '\n';
        cout << "test";
    }
    myfile.close();
}

else cout << "Unable to open file";

//return 0;
//getchar();
}

Apologies in advance if this has been answered, but while I've found several answers that are very close to what I need, I can't find an answer to this specific problem.

I'm new to Visual Studio, but have dabbled in c++ in the past. I'm trying to read in data from a text file and (for now) simply print that back out with cout. But, I'm not seeing any results.

At first I figured I just had my txt file in the wrong place - and I did. Initially I would receive the line "Unable to open file", indicating that the file could not be opened. So I moved it around and found out where Visual Studio wanted me to put the file.

So now I successfully see the "File Opened\\n" line get printed to the screen, followed by nothing. I thought I might be using getline wrong, but if I replace the file input "myfile" with a "cin" instead, getline will happily read in keyboard input all day, so that's not it either.

So I've put in some test cout statements that print out the value of my string, line. The first one prints out "test" as it should. Then I read in a line of the txt file to that string variable, and when I cout it again I get nothing. It's a blank string.

Also, the line " cout << "test"; " From within the loop does NOT print either. So the loop's not even happening, it seems.

So, as near as I can tell, the program is able to find my textfile, example.txt. But it's not actually seeing the contents within.

The contents of the textfile (and what I'd like the program to print out) are as follows:

"This is the first line

This is the second line

Third

Fourth

Fifth"

Any and all help is much appreciated.

Figured it out.

What went wrong is this line:

ifstream myfile("example.txt");

I don't know exactly why, but since I specify the file to open in the next line down ( myfile.open("example.txt"); ), specifying the filepath in the ifstream declaration caused the issue.

I don't entirely get it, as others have said that the code runs fine for them. But this seems to work, anyway.

If there's any reason why I shouldn't use this solution, please let me know.

you might want to take a look at your file open "myfile.open("example.txt");" i found if you don't give a file path weird things happen. myfile.open("c:\\test\\example.txt"); is the adjustment I made to the code and it work like a dream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM