ifstream says it opened a file but the file doesnt open - c++

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

Related

ifstream.open fails when inputting file name

I am writing a program in c++ that allows a user to enter the name of a file they want to open. However, whenever the program tries to open that file, even if the user inputs an existing file name and the file is in the same directory as the program, the file opening will fail. Why is this happening and how do I address this? I am using MacOS and writing this program in Visual Studio Code and compiling using Xcode's command line tools. The file I am trying to open is a .txt file. For additional background, I am in my first programming class and this is my first time using I/O streams.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream inStream;
ofstream outstream;
string filename1;
//accepting user input of file name
cout << "Enter the file name: ";
cin >> filename1;
//** OPENING FILE **
inStream.open(filename1);
//checking if open fails
if(inStream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
return 0;
}
I have also tried writing this code so that the full path of the file is included in the open command, but that doesn't work, either.

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 cannot open file with amdinistrator rights

I used this code to try to open and read the file (not empty), but ifstream did not work - it could not open the file: I addded the check on file opening and it showed, that ifstream even did not (could not) open the file.
I gave administrator rights to the program, but ifstream still could not read the file.
I also tried to find a path, where ifstream would read this file, but I did not success, and at last I tried to open file using the absolute path - but result is the same.
The file is situated in the root folder of the program, but I placed it everywhere and nothing changed.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string s;
ifstream file("fix.txt");
if (file)
cout << "SUCCESSFULL OPENING" << endl;
while (getline(file, s)) {
cout << s << endl;
s += "+";
cout << s << endl;
}
file.close();
return 0;
}
You may have access to a more detailed error code by activating exceptions on the stream via
file.exceptions(std::ios_base::failbit);
Then, you get more details by writing
try {
file.open("fix.txt");
}
catch(std::ios_base::failure& f) {
// f.what() contains a message, f.code() returns a std::error_code
}

Why isn't my input stream opening my file? C++

I'm reading in a file name from the keyboard and opening the specified file. However it is not meeting my if statement that determines if it's open or not. Heres my code:
#include "prog.hh"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string fileName;
cout << "Enter the file name to be read: ";
cin >> fileName;
ifstream input_file("fileName");
std::string line_; // string which text file contents will be stored in
if(input_file.is_open()){ // validation to see if the file is open
while(getline(input_file, line_)){
std::cout<<line_<< '\n'; //prints the contents of the file into the console
}
input_file.close();
}
else {
std::cout<<"File is not open"<< '\n';
}
std::cin.get();
After being compiled, I type in the file name I want to open and I get back the else message "File is not open", although the ifstream should of opened it. I definitely have the right file I'm trying to open in the correct folder. Any help appreciated, thanks.
change
ifstream input_file("fileName");
to
ifstream input_file(fileName);

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.