7

I've been all over the ifstream questions here on SO and I'm still having trouble reading a simple text file. I'm working with Visual Studio 2008.

Here's my code:

// CPPFileIO.cpp : Defines the entry point for the console application.
//

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

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

    ifstream infile;
    infile.open("input.txt", ifstream::in);

    if (infile.is_open())
    {
        while (infile.good())
            cout << (char) infile.get();
    }
    else
    {
        cout << "Unable to open file.";
    }
    infile.close();
    _getch();
    return 0;
}

I have confirmed that the input.txt file is in the correct "working directory" by checking the value of argv[0]. The Open method just won't work.

I'm also having trouble debugging- should I not be able to set a watch on infile.good() or infile.is_open()? I keep getting

Error: member function not present.

EDIT: Updated code listing with full code from .CPP file.

UPDATE: The file was NOT in the Current Working Directory. This is the directory where the project file is located. Moved it there and it works when debugging in VS.NET.

Patrizio Bertoni
  • 2,582
  • 31
  • 43
Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
  • Its the working directory that is your problem. Whem you run it from the command line the working directory is obvious. When you run it in the debugger you need to explicitly set the working directory otherwise it is not so obvious (check the debugger options). – Martin York Apr 29 '09 at 00:41
  • Thanks for posting the update section. It had been a minute since I needed to think about where the working directory is in debug mode. Saved me a few minutes of head scratching. – JR Smith Jun 25 '14 at 01:55

6 Answers6

8

Try using the bitwise OR operator when specifying the open mode.

infile.open ("input.txt", ios::ate | ios::in);

The openmode parameter is a bitmask. ios::ate is used to open the file for appending, and ios::in is used to open the file for reading input.

If you just want to read the file, you can probably just use:

infile.open ("input.txt", ios::in);

The default open mode for an ifstream is ios::in, so you can get rid of that altogether now. The following code is working for me using g++.

#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

int main(int argc, char** argv) {
    ifstream infile;
    infile.open ("input.txt");

    if (infile)
    {
        while (infile.good())
            cout << (char) infile.get();
    }
    else
    {
        cout << "Unable to open file.";
    }
    infile.close();
    getchar();
    return 0;
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • Heh. Sometimes I wish my Rep was high enough to accept an answer for someone else. :-) – T.E.D. Apr 28 '09 at 17:11
  • Still no luck :( I noticed that I had the "infile" as an "fstream" instead of "ifstream". I removed the ios:ate. The open method still isn't working. – Dave Swersky Apr 28 '09 at 17:35
  • Bizarre! It doesn't work when I debug, but it works perfectly when I run the debug .exe from a command line! Any ideas why? – Dave Swersky Apr 28 '09 at 19:47
  • More interesting: When I open the "Visual Studio 2008 Command Prompt" and run from there, I get the error. It works when I use the Windows command prompt... – Dave Swersky Apr 28 '09 at 19:49
  • 3
    GOT IT: I had to use the _getcwd() function to get the Current Working Directory, which was the *project file* directory, NOT the solution or Debug directory. Strangely, no amount of hard-coding the path would work when debugging. Thanks for your help! – Dave Swersky Apr 28 '09 at 19:57
  • You're welcome for what little help I was able to give. Looks like you worked through most of it on your own. :) – Bill the Lizard Apr 28 '09 at 22:56
  • Dave, your comments just saved me hours of work. Can't thank you enough! – rsp1984 Dec 15 '13 at 02:57
6

Sometimes Visual Studio puts your exe file away from your source code. By default VS may only look for the file starting from your exe file. This process is a simple step for getting the input txt file from the same directory as your source code. Should you not want to fix your IDE setup.

using namespace std;

ifstream infile;

string path = __FILE__; //gets source code path, include file name
path = path.substr(0,1+path.find_last_of('\\')); //removes file name
path+= "input.txt"; //adds input file to path

infile.open(path);

Hopefully this helps other people for a quick solution. It took me a while to find this setup myself.

Dustin Taylor
  • 61
  • 1
  • 1
  • hi, just to add For linux based machine the path should be path = path.substr(0,1+path.find_last_of('/')); //removes file name – hariudkmr May 17 '20 at 04:53
1

I've found two problems in your code:

a) syntax error in "ios::ate || ios::in" => should be "ios::ate | ios::in"

b) "ios::ate" sets the cursor to the end of file - so you get nothing when you start reading

So just remove "ios::ate" and you are fine :)

ciao, Chris

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
3DH
  • 1,461
  • 11
  • 11
0

If you use the default Vs code setup, place the text file that you want to read from in the same folder as your executable, I know it is not pretty but yeah it works

  • This question is about VS, not VS Code, though. And for Code, working directory can be changed in `launch.json` file using `cwd` [property](https://code.visualstudio.com/docs/cpp/launch-json-reference#_cwd) (by default it's the workspace directory). – YurkoFlisk Aug 02 '22 at 22:41
0
infile.open ("input.txt", ios::ate || ios::in);

|| is the logical or operator, not the bitwise operator (as Bill The Lizzard said).

so i guess you are doing the equivalent to:

infile.open ("input.txt", true);

(assuming neither ios::ate or ios::in are 0)

Tom
  • 43,810
  • 29
  • 138
  • 169
0

Try using:

ifstream fStm("input.txt", ios::ate | ios::in);

I'm also having trouble debugging- should I not be able to set a watch on "infile.good()" or "infile.is_open()"? I keep getting "Error: member function not present."

and the proper includes:

#include <fstream> 

etc.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • I have the #include in my cpp file. – Dave Swersky Apr 28 '09 at 17:36
  • You need to show us the complete file in order to fix your second problem. BTW, were you able to read in the file? – dirkgently Apr 28 '09 at 17:39
  • Change ifstream::in to ios::in. Why do you need conio.h or _getch? These are non-standard. Do you have VS2008 SP1 installed? – dirkgently Apr 28 '09 at 18:08
  • I added conio.h and _getch just to be able to break execution and wait for a keystroke before the program terminates. Do you think this would cause a problem? I'll try removing them. – Dave Swersky Apr 28 '09 at 18:17
  • Are you able to compile your code cleanly? Or, are you still stuck with compilation? Check if you have SP1 installed or not and remove everything but the bare minimum and try to compile. Update your post if it doesn't. – dirkgently Apr 28 '09 at 18:27