ifstream::open not working in Visual Studio debug mode - c++

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.

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;
}

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.

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

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)

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.

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

Related

Ifstream is opening the file, but doesn't output the lines inside the file

Im new to c++ and i was trying to open a ".txt" file using ifstream. the file im using is called "ola.txt" which literally just contains two lines of text without punctuation just plain and simple text. The code that i wrote is this
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int x;
string line;
vector<int> vect;
ifstream inFile("C:\\Users\\ruial\\Desktop\\ola.txt");
inFile.open("C:\\Users\\ruial\\Desktop\\ola.txt");
if (inFile.is_open()) {
while (getline(inFile, line))
{
cout << line << '\n';
}
inFile.close();
}
else {
cout << "Unable to open file";
exit(1); // terminate with error
}
return 0;
}
The path to the file that i wrote is correct such that the file opens, but when the program runs it doesn´t cout the lines that i wrote on the txt file to the cmd, i dont know if this is somewhat important but im coding in visual studio 2019.
I can't seem to find the answer to this problem anywhere in the internet and to be honest i think im doing it right, any help would be much appreciated,thanks in advance.
You are trying to open the inFile twice. First time during inFile construction, ifstream inFile("C:\\Users\\ruial\\Desktop\\ola.txt"), second time you try to open it again with inFile.open("C:\\Users\\ruial\\Desktop\\ola.txt"), when it's already open, which is erroneous, and flags the stream as no longer good.
3 possible fixes:
Remove inFile.open("C:\\Users\\ruial\\Desktop\\ola.txt")
Use default constructor, without specifying the file name
inFile.close() before you open it again (obviously, not the nicest fix).

ifstream says it opened a file but the file doesnt open

I am using visual studio 2017
I am new to c++ and here I tried to open a txt file, and confirming that it was opened.
#include <iostream>
#include <fstream>
#include <String>
using namespace std;
int main()
{
ifstream infile;
string text;
infile.open("C:\\Users\\gab_a\\source\\repos\\one\\testing.txt");
if (!infile.is_open()) {
cerr << "Specified file could not be found ";
exit(1);
}
else {
cout << "Opened file ";
infile >> text;
cout << text;
}
return 0;
}
it says that it opened it, and it even read the text that was inside the file, but the actual file isn't opening, I even put the file in the same directory as the project. There are also no errors, so why isn't my file opening?
What you're doing is reading the data from the file into a stream. This is not the same as executing a program to open the file. To do that is generally OS specific, but if you're on Windows you can use ShellExecute or CreateProcess. I do suggest you brush up on your C++ a bit - no offense intended

c++ write to a file, but file is empty

I have this code for turning decimal into binary and write it to a file:
#include <iostream>
#include <fstream>
using namespace std;
int d,num,s,r,k,sum=0;;
void main()
{
ofstream myfile;
myfile.open("binary.txt",ios::in);
cin >> d;
while(d>0)
{
r=d%2;
sum=sum + (k*r);
d=d/2;
k=k*10;
myfile << sum;
}
cout << "Encryption Succesfull"<<endl;
myfile.close();
}
The program runs successfully, but the file is empty.
The file name is right, no syntax errors, etc.
What to do?
You use the wrong flags for opening the file:
myfile.open("binary.txt",ios::in);
It should be
myfile.open("binary.txt",ios::out | ios::binary);
myfile.open("binary.txt",ios::in);
opens the file for reading. If you want to output something, use ios::out or leave the param empty (since the default value is ios_base::out anyhow):
myFile.open("binary.txt");
I've found this line on cplusplus.com, but it doesn't seem to be true (tested on devC++ 4.9.9.2, uses some MinGW version):
out is always set for ofstream objects (even if explicitly not set in argument mode).
Removing the second parameter or changing it to ios::out does fix the problem.

can't open file ,mystate for datafile2 =2

When I debug this I can see it opens datafile1 , it reads the firstline and
in the logfile I get roma-3-4.log
It change to c:/temp/roma-3-4.log but when I want to open it , it fails. I have check that the _Mystate = 2 .
What is the meaning of that
Thanks
in the transfersubs.cfg there is this
roma-3-4.log
** In the directory c:/temp/ I have the following file
roma-3-4.log
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
string logfile;
string errorfile;
short logfilesize1;
fstream dataFile1("c:/temp/transfersubs.cfg", ios::in);
if (dataFile1)
{
getline(dataFile1, input, '$');
logfile=input;
logfilesize1=input.size();
errorfile=input;
errorfile[logfilesize1-4]='e';
errorfile[logfilesize1-3]='r';
errorfile[logfilesize1-2]='r';
logfile="C:/Temp/"+logfile;
fstream dataFile2( logfile, ios::in);
if (dataFile2)
{
dataFile2.close();
}
else
{
cout << "ERROR: Cannot open logfile.\n";
}
dataFile1.close();
}
else
{
cout << "ERROR: Cannot open file.\n";
}
system("Pause");
return 0;
}
I believe your getline doesn't bother looking the newline but only for a $. You didn't post the file you are reading from, but check to ensure it has a $ at the end of the file name otherwise it will fetch the entire file.
It appears that unless you put a \n or endl after writing to the file using ofstream, ifstream won't be able to read anything from the file. In fact, adding a space after whatever you've written into file won't help either.
So always add a newline right after whatever it is that you've written to file using ofstream.

C++ ifstream will not open any files

Whenever I try to open a file with ifstream, it compiles fine, but will not open the file.
The file in this example doesn't exist, but ifstream *s*should*s* create the file for me.
i have some example code that i think should work, but does not open or create the file
"foo.txt". Is there something that i'm missing, or is my IDE just messed up?
i'm using visual studio 2008 VC++ , btw
thanks
here's the code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream in;
string hold;
in.open("foo.txt",ios::in);
if(!in){
cerr << "Couldn't open file!" << endl;
}
in >> hold;
cout << hold << endl;
system("pause");
return 0;
}
The problem is you are using an in stream instead of an out stream, as Adam Liss mentioned(ios::out rather than ios::in). You also need to make sure you close the file before return 0; to make sure everything from the buffer is actually written to the file.
The open function will not create files in ios::in mode; you need to use ios::out.