4

I'm trying to read a text file but nothing is coming out. I feel like maybe It's not linking correctly in my Visual Studio Resources folder but if I double click it - it opens fine in visual studio and it doesn't run into any problems if I test to see if it opens or if it is good. The program compiles fine right now but there's not output. Nothing prints to my command prompt. Any suggestions?

Code

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    char str[100];
    ifstream test;
    test.open("test.txt");

    while(test.getline(str, 100, '#'))
    {
        cout << str << endl;
    }

    test.close();
    return 0;
}

Text File

This is a test Textfile#Read more lines here#and here
Howdy_McGee
  • 10,422
  • 29
  • 111
  • 186
  • 1
    Do you know whether the file even opens ok? You should be checking return values. – Dabbler Oct 22 '12 at 18:56
  • I ran an IF statement to see if it is_open() and it entered the conditional so I'm assuming it opened it (return 1 / true) – Howdy_McGee Oct 22 '12 at 19:00
  • it just tested your example in linux an all work as expected. – Agus Oct 22 '12 at 19:01
  • Maybe you just need to put a breakpoint in VS IDE on the `return 0;` statement to see some output, or add something like `system("PAUSE");` or `cin.get()` before `return 0;`. – Mr.C64 Oct 22 '12 at 19:02
  • @Agus Did you test it via Visual Studio? I feel like that's the issue here unless my problem is reading into a character array like the answer below stated. – Howdy_McGee Oct 22 '12 at 19:03
  • Kinda irrelevant, but why are you using C-strings? `std::string` is safer. `string str; while (getline(test, str, '#')) {}` – Geoff Montee Oct 22 '12 at 19:05
  • Works from VS2010 command line. – Mr.C64 Oct 22 '12 at 19:08
  • Are you sure that the `test.txt` that you're editing is the `test.txt` that it's actually opening and reading? Try removing `test.txt` and see if `is_open` still returns true. – Nate Kohl Oct 22 '12 at 19:09
  • @NateKohl You're right, if I remove text.txt it still opens it? I have my `text.txt` file in the same folder as my solution – Howdy_McGee Oct 22 '12 at 19:10

2 Answers2

10

You try to open file by name without path, this means the file shall be in current working directory of your program.

The problem is with current directory when you run your program from VS IDE. VS by default sets current working directory for runnning program to project directory $(ProjectDir). But your test file resides in resources directory. So open() function could not find it and getline() immediately fails.

Solution is simple - copy your test file to project directory. Or copy it to target directory (where your program .exe file is created, typically $(ProjectDir)\Debug or $(ProjectDir)\Release) and change working directory setting in VS IDE: Project->Properties->Debugging->Working Directory, set to $(TargetDir). In this case it will work both from IDE and command line/Windows Explorer.

Another possible solution - set correct path to file in your open() call. For testing/education purposes you could hardcode it, but actually this is not good style of software development.

Rost
  • 8,779
  • 28
  • 50
1

Not sure if this will help but I wanted to simply open a text file for output and then read it back in. Visual Studio (2012) seems to make this difficult. My solution is demonstrated below:

#include <iostream>
#include <fstream>
using namespace std;

string getFilePath(const string& fileName) {
 string path = __FILE__; //gets source code path, include file name
 path = path.substr(0, 1 + path.find_last_of('\\')); //removes file name
 path += fileName; //adds input file to path
 path = "\\" + path;
 return path;
}

void writeFile(const string& path) {
 ofstream os{ path };
 if (!os) cout << "file create error" << endl;
 for (int i = 0; i < 15; ++i) {
  os << i << endl;
 }
 os.close();
}

void readFile(const string& path) {
 ifstream is{ path };
 if (!is) cout << "file open error" << endl;
 int val = -1;
 while (is >> val) {
  cout << val << endl;
 }
 is.close();
}

int main(int argc, char* argv[]) {
 string path = getFilePath("file.txt");
 cout << "Writing file..." << endl;
 writeFile(path);
 cout << "Reading file..." << endl;
 readFile(path);
 return 0;
}
peterdcasey
  • 171
  • 1
  • 4