Read from file c++ always can't read - c++

ifstream myfile;
myfile.open("FileTest");
string line;
if(myfile.is_open())
{
cout<<"Reading from file...";
getline(myfile,line);
}
if(myfile.fail())
{
cout<<"Unable to open file"<<endl;
}
myfile.close();

C++ tries to open the file in the current directory with the exact name FileTest. Check to see if the file is in the current directory? Maybe you spelled the name incorrectly? Maybe you forgot to write FileTest.txt? You are using ifstream, which will fail if the file you're trying to open does not exist or is corrupted.

Related

Input file unable to be read. Console showing "Unable to open file : InFile.tx"

I am trying to get an input file to be opened and read, but the console is unable to read the file named "InFile.txt" and saying "Unable to open file : InFile.tx". I am using eclipse c++ IDE and have the InFile.txt and OFile.txt saved alongside the main.cpp.
int main() {
string infile, outfile;
readFilenames(infile,outfile);
ifstream file;
file.open(infile.c_str());
...
void readFilenames(string &infile, string &outfile)
{
cout<<"What input file would you like to use? ";
getline(cin,infile);
infile = infile.substr(0,infile.length()-1);
cout<<"What output file would you like to use? ";
getline(cin,outfile);
outfile = outfile.substr(0,outfile.length()-1);
}

Can't open file in folder. path is not correct

I'm having a problem to open my file, and hence when I try to read it using geline a get an empty string.
I have a folder with several text files, and I want to read all of them. my code is in folder C:\root\pjt and my files are in folder C:\root\files.
I try to read the files using dirent.h library, I used getline with no success, but than I realized it couldn't even open the file, so I just tried opening the file, but that didn't work either.
void listFile() {
DIR *pDIR;
struct dirent *entry;
string line;
if (pDIR = opendir("../../../files")) {
while (entry = readdir(pDIR)) {
string filename(entry->d_name);
ifstream infile(filename);
infile.open(filename);
//I need the files with "records" in their name
if (filename.find("records") != string::npos) {
//infile.open(filename);
//while (!infile.eof()) {
//infile.get(buffer, '\r\n');
//getline(infile, line);
//}
//infile.close();
infile.open(filename);
//IT NEVER GETS TO OPEN THE FILE!!!
if (infile.is_open()) {
while (getline(infile, line))
{
cout << line << '\n';
}
infile.close();
} else cout << "Unable to open file";
}
}
closedir(pDIR);
}
}
I want to be able to read the file, I suspect I can't do it because the path is wrong.
Thanks for the help

Problems with reading a .txt file

I am looking for an answer to my question, but i didn't find it in any other place.
I'm trying to read from a .txt file, that is located in the same directory as my project files.
I wrote this simple code:
ifstream file("file.txt");
std::string line;
std::getline(file, line);
cout << line;
...but unfortunately, nothing happened, not even an error or crashing.
Upon exploring a little further... even if I change the name of the txt("file") file, to the name of a file that doesn't exist, nothing happens.
What am I missing?
How do you know there were no errors? You did not check.
#include <cerrno>
and then
ifstream file("file.txt");
if (file) // is the file readable?
{
std::string line;
if (std::getline(file, line)) // did we manage to read anything?
{
cout << line;
}
else
{
cout << "File IO error";
}
}
else
{
cout << "error opening file: " << strerror(errno);
}
performs rudimentary checking.
if your error is due to opening file then provide full path to the file and check.
in your code you are reading the first line so if it is a white space then you can see nothing as output.
you must to iterate over each line until the last line (reaching the end of file EOF).
// let's say your file is "test.txt" which is located in D\\MyDB
// ifstream file("file.txt");
ifstream file("D:\\MyDB\\test.txt"); // use full path instead and check manully whether the file is there or not
std::string line;
if(file.fail())
cout << "Opening file failed!" << endl;
else
while(std::getline(file, line))
{
cout << line;
}
if it works when providing the full path then your current path is not the same as your project.
you can change the current directory using some API so if you are on windows then use: SetCurrentDirectory(path); and on linux use: chdir(sDirectory.c_str());
** I mean compilers not OS

Detecting if a file is open

How can I detect if a file is open in C++?
I am trying to use a code like this:
int main()
{
ifstream file("file.txt");
if ( /*here comes the check if file is open*/ ) cout<<"File open successfully"; else cout<<"File couldn't be opened. Check if the file is not used by another program or if it exists";
}
You are looking for the function is_open()
if(file.is_open()){}
if(file.is_open())
You are calling ifstream::is_open() function

C++ Trouble opening a file for output

So I am working on a program for class in which we have to open two different text files to retrieve the appropriate text to be displayed in the console. My code is not opening the file and keeps outputting the else statement ".txt file cannot be open". I've tried several different ways to open the file but with no luck. Any help here would be greatly appreciated.
//
// main.cpp
// PunchLine program
// Page 896 Problem 3
//
//
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//File stream objects
fstream inFile;
string line;
//Open joke file to read lines to console
inFile.open("joke.txt", ios::in);
if (inFile.is_open())
{
//Read lines from file to console
while (getline(inFile, line))
{
cout << line << endl;
inFile.close();
}
}
else
cout << "joke.txt file cannot be open.\n";
//Open punchline file to read last line joke to console
inFile.open("punchline.txt", ios::in);
if (inFile.is_open())
{
//Read last line from file to console
inFile.seekp(-52L, ios::end);
getline(inFile, line);
}
else
cout << "punchline.txt file cannot be open.\n";
return 0;
}
When declaring an input file use
ifstream inFile;
Also make sure the input file is in the same folder as your .exe
Edit: http://www.cplusplus.com/doc/tutorial/files/ also, this link should help with working with files.
Edit 2: I already posted this in a comment, but I'll just add it to the official answer: "Change your while loop as well. Instead of the if test, use while(inFile.is_open()) and then use your getline statement inside the loop. Because right now your code reads like while get this line from the file is true cout line. So it might not even be doing the while loop."
I don't think you should close the file inside the while loop. Otherwise, your file gets closed after only the first line is read in. Move the close statement outside the loop. Same for the second block.
if (inFile.is_open())
{
//Read lines from file to console
while (getline(inFile, line))
{
cout << line << endl;
}
inFile.close();
}
else
cout << "joke.txt file cannot be open.\n";
Check that your file exist. If it does, check whether you have the correct path when you open it (check if your .txt files are in the same directory as your .exe file, or specify the full path in your code). If yes, check if the files are read-only.
use
if(!infile)
{
cout<<"cannot open file";
}
I think you need to flush the screen. Once you have flushed and closed the stream. The next time you run an application it should open the file.
e.g.
inFile.flush();
inFile.close();