Before you flag this as a duplicate post and refer me on how to correctly open a text file and print to the console, I have looked at numerous StackOverflow posts about this topic and cannot find a solution for myself.
I am trying to open a text file I created (currently in the same project folder as my main.cpp), read the text, and print it to the console. I go through the if file is open statement fine but the while loop does not go through even once. I will post the function below. Please suggest any changes or ideas on how to correctly call and open/read the text file. (and I would prefer not to call the exact file location of the text file for this to work ex. C://example/textFile.txt/ Though I have not tried this method yet, I'd prefer avoiding it)
Also, I am using CLion IDE from jetbrains, C++17, and Ninja to build.
printing text file to console fucntion
#include <iostream>
#include <iomanip>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
#include "printTest.h"
void printTest::print() {
std::string line; //string that holds the line of a text file
std::ifstream textFile("test.txt", std::ios::in); //file creation
if(textFile.is_open()) //checking if file was opened
{
while(std::getline(textFile, line))
{
//std::getline(textFile, line);
std::cout << line << "\n";
}
} else { //this is always printing i.e. file is not correctly being opened for reading
std::cout <<"Unable to open the text file..." <<std::endl; //Prints if file was not opened
}
textFile.close();
}
Related
I'm trying to create a simple terminal program that generates random Japanese Gojuōn. I have a source file, named "source", which looks something like this:
A I U E O
Ka Ki Ku Ke Ko
...
あ い う え お
か き く け こ
...
Now, I'm trying to read each line of content into a string variable, and print it out onto the screen, but I was not able to open the file. Here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream source ("source");
string line;
if (source.is_open())
{
while (getline(source, line))
{
cout << line << "\n";
}
source.close();
}
else cout << "Unable to open file!\n";
}
When I ran the code on terminal, I got "Unable to open file!". The code was in the same directory as the source file.
Are you running from inside your IDE? This is a very common thing when using an IDE.
The current working directory when running from the IDE may not be what you think it is. Try changing the path to the file to be the absolute path so you KNOW it's finding it.
I have a .txt file and I tried using the absolute path "C:\Users\(full path)\A3Data" and static paths (shown in code):
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream MyReadFile("A3Data.txt");
if(MyReadFile.is_open()) //checks whether file is being opened
{
while (getline(MyReadFile, line)) //uses getline to get the string values from the .txt file to read line by line
{
cout << line << '\n';
}
MyReadFile.close();
}
else if (MyReadFile.fail())
{
cout << "A3Data.txt failed to open" << endl;
}
return 0;
}
Expected output:
(contents in A3Data.txt)
Actual Output:
"A3Data.txt failed to open"
Double backslashing the absolute path e.g. C:\\Users\\(full path)\\A3Data.txt gives me the error too.
Changing the filepaths and ifstream-ing it to the different path also shows me the error.
When i tried to read my file from cmd, I cd'ed it to the full path and typed in the text file name. I could open & read it properly. Hence I feel that the w.r.x are accessible to me and I have rights to the file.
UPDATE:
I have figured out my issue. Thank you all for the answers!
You need to double backslash or declare your file path as a string literal. You can do this like:
string myPath = L"C:\Users\(full path)\A3Data.txt";
As a string literal, or
string myPath = "C:\\Users\\(full path)\\A3Data.txt";
As a properly escaped file path
If the above does not work and you have guaranteed that you have the proper paths to the file then you may not have proper rights to the file. You could try running a command line as administrator and then executing your code from that, if that also fails let us know.
I have a tool, that makes many cerr outputs.
if i run it over "run configuration"s in Clion i see all the cerr messages in the output window.
How can i disable some output stream in Clion/Intellij?
I use windows 10.
You should redirect the cerr output to a file.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ofstream output("output.txt");
std::streambuf* p_cerrbuffer=std::cerr.rdbuf();
std::cerr.rdbuf(output.rdbuf()); // redirecting to a file
std::cout<<"cout"<<std::endl; // "cout" appears on the standard output.
std::cerr<<"cerr"<<std::endl; // "cerr" appears in the output.txt file
}
This question already has answers here:
How to append text to a text file in C++?
(5 answers)
Closed 7 years ago.
I have created a code writing stuff in a .txt file and read from it. But if I close the program and start to write again, it deletes the old text and overwrites it with the new one.
Is there a way to not overwrite existed data?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void check() {
string text;
ifstream file;
file.open("test.txt");
getline(file, text);
if (text == "") {
cout << "There's no data in file" << endl;
} else {
cout << "File contains:" << endl;
cout << text << endl;
}
}
int main() {
check();
string text;
ofstream file;
file.open("test.txt");
cout << "Type some text" << endl;
getline(cin, text);
file << text;
return 0;
}
You need to open the file in 'append' mode like in the following example
#include <fstream>
int main() {
std::ofstream outfile;
outfile.open("yourfile.txt", std::ios_base::app);//std::ios_base::app
outfile << "your data";
return 0;
}
You can read here about fstream flagshttp://www.cplusplus.com/reference/fstream/fstream/open/
Keep in mind that in c++ there are several ways to open, save, and read text data to and from a file. It sounds like you opened with with a function (and its options) to always create a new file. One thing you could do is run your program and then close the program and use a text editor to open the file to verify whether the text you wrote to the file is actually there. Also take a look at the code that was provided by Evyatar. That example uses ofstream which allows options for read, write, and append. The "app" parameter tells the program to keep what is already in the file and append any new data that you add in the next run. When testing files where you are appending, be careful you don't end up with a huge file you did not intend to have so large.
In the code that you posted in your revised question, be sure to close the file in your check function and at the end of the program. It is possible to get things hung up if you don't. As a precaution, I usually close a file prior to opening it, just to be sure it is closed with no problems. This practice comes form my days programming in BASIC where it was an essential. If the program crashed, you couldn't open it again until you got it closed. Also, of course, close the file after you're done with it and before the end of the program. Then, when you open it in main, open with the append option.
Please, insert code for next time. If you open file in write mode, than is normal that every time you write to file, the content of file is changed. You need to use append mode.
I have this code that suppose to read a txt file.
But for some reason i am always getting *File not found that means that fileIn.fail() failed...
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <stdio.h>
using namespace std;
int main ()
{
string fileName;
ifstream fileIn;
bool x;
cout << "enter file name \n";
cin >> fileName;
fileIn.open(fileName);
if(fileIn.fail())
{
cerr << "* File not found";
return true;
}
the file located in the same folder as my main.cpp file and named input.txt. I have tried to set the fileName hard coded but this also didn't work.
What is wrong with my code?
here is the project:
Here is a checklist:
Do you have permissions to read/access the file?
Are you the owner of the file?(Linux)
Are you giving the correct path, relative or absolute from the executable?
If the answer to any of these is a no, then that is where the problem lies, not just "file not found" error.
--EDIT--
#VladIoffe the executable I see there, is qustion2 and the relative path you have to give is ../input.txt and not input.txt
You should use absolute path to the fileName.
Absoulut path will always works. But I hate full path I prefer relative path for a simple reason: code is more portable.
If you run your program with input.txt in the same path of executable it will work. But when you use an IDE you must set the current directory in the IDE settings.