Error in reading from a .txt file in c++ - c++

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//reading the text file
ifstream inputFile("testfile1.txt");
inputFile.open("testfile1.txt");
while(!inputFile.eof())
//eof till end of file, reads the txt till end of file
{
string str;
getline(inputFile,str);
cout <<str<< endl;
}
inputFile.close();
return 0;
}
// The problem that i am having is that it doesn not read the file or anything in it. Doing nothing it says Program ended with exit code: 0. Could anyone check the mistake in the code

First Bug: You are opening the input file twice. Per the C++ standard, regarding the behavior of your second open request (the direct call to the open member):
C++11 § 27.9.1.9 [ifstream.members/3]
void open(const char* s, ios_base::openmode mode = ios_base::in);
Effects: Calls rdbuf()->open(s, mode | ios_base::in). If that function
does not return a null pointer calls clear(), otherwise calls
setstate(failbit) (which may throw ios_base::failure (27.5.5.4)).
which therefore asks the question, what does rdbuf()->open(...) do ? Well, a std::ifstream uses a filebuf for it's buffering, and once again, per the standard:
C++11 §27.9.1.4 [filebuf.members/2]
basic_filebuf<charT,traits>* open(const char* s, ios_base::openmode mode);
Effects: If is_open() != false, returns a null pointer. Otherwise, initializes the filebuf as required. ...
In short, your double-open is putting your stream into a fail-state, which means all data-related operations with it are going to fail outright from that point on.
Second Bug: Improper use of .eof in a loop conditional expression. you'll run into this once you fix the first bug. The reasons this is not being done correctly are explained in the following question far better than I can explain it here.
Why is iostream::eof inside a loop condition considered wrong?
Suffice it to say, check your IO operations, not just the eof-state of the stream. Get into that habit and stick with it.
Fixing both, your code can literally be reduced to simply this:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream inputFile("testfile1.txt");
std::string str;
while (std::getline(inputFile, str))
std::cout << str << std::endl;
}
Obviously if you're shooting for more robust code, you probably want to perform some error handling in there, something like:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
int main()
{
std::ifstream inputFile("testfile1.txt");
if (!inputFile)
{
std::cerr << "Failed to open file\n";
return EXIT_FAILURE;
}
std::string str;
while (std::getline(inputFile, str))
std::cout << str << std::endl;
}

This is the correct way to read a file according to this article!
The problem in your code it seems that you are using an IDE and it cannot find the path you are giving to ifstream so try to give a full path to the file. Hope it can help u.
string line;
ifstream f("/YOUPARTH/testfile1.txt");
if (!f.is_open())
perror("error while opening file");
while(getline(f, line)) {
cout << line << endl;
}
if (f.bad())
perror("error while reading file");
return 0;

Translate the while statement: "While inputFile is at End Of File" .. you want the negation of that.

Related

Code to print out number of lines and integer values

I created a data file called program.txt. I need to create code that prints out number of lines and integer values from that program.txt
Heres the text I made
1
35
45
87
9
100
the program.text has these values in it
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string calc;
int test;
int cool;
int book;
ifstream myfile;
ifstream outof;
myfile.open ("program.txt");
outof.open("program.txt");
if (myfile.fail())
{
cerr<<"Error Opening file"<<endl;
}
if(outof.fail ())
{
cerr<<"Error Opening file"<<endl;
}
while (!myfile.eof ())
{
getline(myfile,calc);
++book;
}
cout <<book<<endl;
while (!outof.eof ())
{
outof<<test;//
cool++;
}
cout<<cool<<endl;
myfile.close();
outof.close();
}
Also after cerr I tried exit (1) and it said exit was not defined.
I am new to this any help would be greatly appreciated. Thanks This is C++ btw.
The problem is that you are using ifstream, which stands for INPUT file stream. You want to use ofstream, which is OUTPUT file stream. You cannot write to an input file stream, hence why the << operator is not defined.
Also, rather than using exit(1) after your errors, you can just return 1; as you are inside your main function. This will terminate the program, returning 1 as the exit code.
If you really want to use exit, you need to #include <cstdlib>.
Your defined input and expected output aren't clearly defined, so I'm not sure what you're trying to do. However, here's a general idea:
Putting the filename in a fstream's constructor will automatically try to open the file for read/write. You dont need to call .open() anymore. Also, you shouldnt be reading and writing to the same file simultaneously if you dont know what you're doing.
std::ifstream myInputFile("program.txt");
std::ofstream myOutputFile("programOut.txt");
Rather than checking myInputFile.fail(), just use the overloaded boolean operator. In depth explanation: ifstream::is_open vs ifstream::fail?
if (!myInputFile) {
//Something went wrong
std::cerr << "Failed to open input file" << std::endl;
return 1;
}
Define your std::string to hold lines as you read them, read all of your input file, and count the lines.
std::string line;
int lineCount = 0;
while (getline(myInputFile,line)) {
++lineCount;
//Do something with 'line'
}
Maybe you'll need to store the lines from your input file so that you can output the count of the lines at the beginning of your output file, you might want to #include <vector> and do something like this:
std::string line;
std::vector<std::string> linesFromFile;
//Read in all lines of the input file and store them in the vector
while (getline(myInputFile, line)) {
linesFromFile.emplace_back(line);
}
//All lines read, good time to close input file
myInputFile.close();
//Print number of lines read
myOutputFile << linesFromFile.size() << std::endl;
//Loop through lines and print them
for (auto &lineFromFile : linesFromFile) {
myOutputFile << lineFromFile << std::endl;
}
//Done outputting, close output file
myOutputFile.close();

How to read names into a pointer array and output them?

Here is what I got so far:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int characterList = 0;
char* dynamo = new char[1000];
char* buffer = dynamo;
ifstream input("wordlist.txt");
if (input.is_open())
{
input >> dynamo[characterList];
while (input.eof())
{
characterList++;
input >> dynamo[characterList];
cout << dynamo[characterList];
}
}
else
{
cout << "File not opened" << endl;
}
return;
}
I'm a beginner so I do apologize if this looks like terrible coding practice. I created a text file with a quote from Bill Cosby that I'm trying to read one word at a time. The quote is "I don't know the key to success, but the key to failure is trying to please everybody." I'm trying to read one word at a time from a text document ignoring punctuation. I know there are a lot of questions similar to this, but they are using code that I have not learned so I'm sorry for having a repeating question. I have not learned getline (I used cin.getline) and #include <string>.
Edit: I forgot to mention, so I'm sorry for not doing so earlier, but I'm studying dynamic memory allocation which is why I'm using the new char[1000].
I'd suggest you to use std::string instead of manually allocating buffers on the heap with new[] and trying to read text manually from the file into those buffers (and don't forget to free the buffer with proper delete[] calls!).
C++ input stream classes like std::ifstream can simply read text into std::string instances thanks to a proper overload of operator<<.
The syntax is as simple as:
string word;
while (inFile >> word)
{
cout << word << endl;
}
Here's a complete compilable sample code for you to experiment and learn:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream inFile("test.txt");
if (inFile.is_open())
{
string word;
while (inFile >> word)
{
cout << word << endl;
}
}
else
{
cout << "Can't open file." << endl;
}
}
This is the output I got on a test text file having the content specified in your question:
I
don't
know
the
key
to
success,
but
the
key
to
failure
is
trying
to
please
everybody.
NOTE
Of course, once you have your words read into a std::string instance, you can store them in a container like std::vector<std::string>, using its push_back() method.
I would do something like this:
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string array[6];
std::ifstream infile("Team.txt");
std::string line;
int i = 0;
while (std::getline(infile, line)) {
array[i++] = line;
}
return 0;
}
based on this answer.
Here, we assume we have to read 6 lines from the file "Team.txt". We use std:::getline() and we put inside a while so that we read all the file.
At every iteration, line holds the current line of the file read. Inside the body we store it in array[i].

end of an Exe file in C++

I am writing a C++ program to read an exe file. I wrote it and I test it on a text file instead of exe file. it was true.
when I test it with an exe file I understand that my exe file have 0x00 value in it (not at its end). so my while loop stop before end of file because I used:
class A{
private:
ifstream myFile;
void Read(char *filename)
};
void A::Read(char *str)
{
myFile.open(str,ios::binary);
while (!myFile.eof())
{
InputFile.get(ch);
myString.push_back(ch);
}
}
what should I do? if I should use size of the file, how can i get it?
You must open the file stream with the std::ios::binary mode flag.
As James McNellis pointed out you need to open the file in binary mode: Try something like the following:
#include <iostream>
#include <fstream>
int main()
{
std::ifstream in("main.obj", std::ios_base::binary);
std::streamsize bytes_read = 0;
if (in.is_open())
{
while (!in.eof())
{
char buf[1024];
// Use unformatted read.
in.read(buf, 1024);
if (in.gcount() > 0)
{
// The first 'in.gcount()' chars in
// 'buf' were read.
bytes_read += in.gcount();
}
}
}
in.close();
std::cout << "bytes read=" << bytes_read << "\n";
return 0;
}
EDIT:
Example modified to use get():
#include <iostream>
#include <fstream>
int main()
{
std::ifstream in("main.obj", std::ios_base::binary);
std::streamsize bytes_read = 0;
if (in.is_open())
{
while (!in.eof())
{
in.get();
if (1 == in.gcount())
{
bytes_read++;
}
}
}
in.close();
std::cout << "bytes read=" << bytes_read << "\n";
return 0;
}
Tested and works correctly.
In addition to opening the file in binary mode, the current code has a subtle bug which will cause the last character in the file to be read twice. The problem is that the myFile.eof() call does not do what you think it does. It does not tell you when you're at the end of the file. It tells you that you have tried to read beyond the end of the file. The idiomatic way to write a read-until-eof loop in C++ is:
while (myFile.get(ch))
myString.push_back(ch);
get returns an istream reference which, in this context, is implicitly convertible to bool and is used to indicate that there is no more data to read.
Only a hunch here, but my suspicion is that you're actually reading the whole file correctly, but measuring it wrong.
File reading (with binary mode) won't stop on a 0-byte, but there are several string related methods that will.
For example, you can't measure the size of a binary "blob" using strlen(), you can't copy it using strcpy().
Without seeing the actual way you're storing and measuring the data, it's hard to see where things go wrong, but I strongly suspect that you're actually reading the whole file correctly if you're using binary mode.
I found my mistake, The program read all the bytes but I cout that bytes in a vector<char>, So it's obvious I saw just bytes before 0x00.

Reading line from text file and putting the strings into a vector?

I am trying to read each line of a textfile which each line contains one word and put those words into a vector. How would i go about doing that?
This is my new code: I think there is still something wrong with it.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
std::string line;
vector<string> DataArray;
vector<string> QueryArray;
ifstream myfile("OHenry.txt");
ifstream qfile("queries.txt");
if(!myfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
if(!qfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
cout<<QueryArray[0]<<endl;
cout<<DataArray[0]<<endl;
}
Simplest form:
std::string line;
std::vector<std::string> myLines;
while (std::getline(myfile, line))
{
myLines.push_back(line);
}
No need for crazy c thingies :)
Edit:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::string line;
std::vector<std::string> DataArray;
std::vector<std::string> QueryArray;
std::ifstream myfile("OHenry.txt");
std::ifstream qfile("queries.txt");
if(!myfile) //Always test the file open.
{
std::cout<<"Error opening output file"<< std::endl;
system("pause");
return -1;
}
while (std::getline(myfile, line))
{
DataArray.push_back(line);
}
if(!qfile) //Always test the file open.
{
std::cout<<"Error opening output file"<<std::endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
std::cout<<QueryArray[20]<<std::endl;
std::cout<<DataArray[12]<<std::endl;
return 0;
}
Keyword using is illegal C++! Never use it. OK? Good. Now compare what I wrote with what you wrote and try to find out the differences. If you still have questions come back.
#FailedDev did, indeed, list the simplest form. As an alternative, here is how I often code that loop:
std::vector<std::string> myLines;
std::copy(std::istream_iterator<std::string>(myfile),
std::istream_iterator<std::string>(),
std::back_inserter(myLines));
The entire program might look like this:
// Avoid "using namespace std;" at all costs. Prefer typing out "std::"
// in front of each identifier, but "using std::NAME" isn't (very) dangerous.
#include <iostream>
using std::cout;
using std::cin;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <iterator>
using std::istream_iterator;
#include <algorithm>
using std::copy;
int main()
{
// Store the words from the two files into these two vectors
vector<string> DataArray;
vector<string> QueryArray;
// Create two input streams, opening the named files in the process.
// You only need to check for failure if you want to distinguish
// between "no file" and "empty file". In this example, the two
// situations are equivalent.
ifstream myfile("OHenry.txt");
ifstream qfile("queries.txt");
// std::copy(InputIt first, InputIt last, OutputIt out) copies all
// of the data in the range [first, last) to the output iterator "out"
// istream_iterator() is an input iterator that reads items from the
// named file stream
// back_inserter() returns an interator that performs "push_back"
// on the named vector.
copy(istream_iterator<string>(myfile),
istream_iterator<string>(),
back_inserter(DataArray));
copy(istream_iterator<string>(qfile),
istream_iterator<string>(),
back_inserter(QueryArray));
try {
// use ".at()" and catch the resulting exception if there is any
// chance that the index is bogus. Since we are reading external files,
// there is every chance that the index is bogus.
cout<<QueryArray.at(20)<<"\n";
cout<<DataArray.at(12)<<"\n";
} catch(...) {
// deal with error here. Maybe:
// the input file doesn't exist
// the ifstream creation failed for some other reason
// the string reads didn't work
cout << "Data Unavailable\n";
}
}
Simplest version:
std::vector<std::string> lines;
for (std::string line; std::getline( ifs, line ); /**/ )
lines.push_back( line );
I'm omitting the includes and other gunk. My version is almost the same as FailedDev's but by using a 'for' loop I put the declaration of 'line' in the loop. This is not just a trick to reduce the line count. Doing this reduces the scope of line -- it disappears after the for loop. All variables should have the smallest scope possible, so therefore this is better. For loops are awesome.
A short version for C++11 and above. The vector is constructed directly from the file contents:
ifstream qfile("queries.txt");
vector<string> lines {
istream_iterator<string>(qfile),
istream_iterator<string>()
};
Note that this code will only work if the input file is in the format described by the OP, i.e. "each line contains one word". Or if you set special locale via qfile.imbue(), as mheyman kindly pointed out.

Try-Catch Block For C++ File-IO Errors Not Working

I'm very new to the world of C++ error handling, but I was told here:
Checking for file existence in C++
...that the best way to checks for file existence was with a try-catch block. From my limited knowledge on the topic, this sounds like sound advice. I located this snippet of code:
http://www.java2s.com/Tutorial/Cpp/0240__File-Stream/Readafileintrycatchblock.htm
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
try{
char buffer[256];
ifstream myfile ("test.txt");
while (! myfile.eof() )
{
myfile.getline (buffer,100);
cout << buffer << endl;
}
}catch(...){
cout << "There was an error !\n";
}
return 0;
}
...but when I compile it using
g++ -Wall -pedantic -o test_prog main.cc
And run the program in a directory where test.txt does not exist, the prog keeps spitting out empty lines to the terminal. Can anyone figure out why?
Also is this a good way to check for file existence for a file you actually want to open and read from (versus just something where your indexing a bunch of files and checking them over)?
Thanks!
In C++ iostreams do not throw exeptions by default. What you need is
ifstream myfile("test.txt");
if(myfile) {
// We have one
}
else {
// we dont
}
By default the fstream objects do not throw. You need to use void exceptions ( iostate except ); to set the exception behavior. You can fetch the current settings using iostate exceptions ( ) const;. Change your code just a bit:
#include <fstream>
#include <iostream>
#include <stdexcept>
using namespace std;
int main ()
{
try{
char buffer[256];
ifstream myfile ("test.txt");
myfile.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );
while (myfile)
{
myfile.getline (buffer,100);
cout << buffer << endl;
}
myfile.close();
}catch(std::exception const& e){
cout << "There was an error: " << e.what() << endl;
}
return 0;
}
First of all, for the try block to do any good, you need to enable exceptions for the stream.
Second, a loop like:
while (! myfile.eof() )
Will lead to nothing but trouble, and you're seeing that here. The problem (in this case) is that when the file failed to open, eof will never be signaled -- you can't/don't reach the end of the file because there is no file. Therefore, your loop runs forever, on an existentialist search for the end of a nonexistent file. Fix the loop, and things get better in a hurry:
char buffer[256];
ifstream myfile ("test.txt");
while (myfile.getline(buffer,100))
{
cout << buffer << endl;
}
While you're at it, a bit more fixing wouldn't hurt (unless you really meant to use less than half of the space you allocated for your buffer):
char buffer[256];
ifstream myfile ("test.txt");
while (myfile.getline(buffer,sizeof(buffer)))
{
cout << buffer << endl;
}
Or, of course, eliminate the problem entirely:
std::string buffer;
ifstream myfile("test.txt");
while (getline(myfile, buffer))
cout << buffer << "\n";
Edit: note that none of these (at least currently) depends on exceptions at all. They're all set up to write a line to the output if we succeeded in our attempt at reading a line from the input. If the file didn't open, the body of the loop simply won't execute, because we won't be able to read from a file that didn't open. If we want to print an error message telling the user that the file didn't open, we'd have to handle that separately from what's above. For example:
ifstream myfile("test.txt");
if (!myfile) {
std::cerr << "File failed to open";
return FAIL;
}
while (std::getline(myfile // ...