I am trying to explore ifstream class and have written below code which reads a file Test.txt
'Test.txt' - Content
This is Line One
This is Line Two
This is Line Three
This is Line Four
This is Line Five
Code Written:
#include <iostream>
#include <fstream>
#include <limits>
using namespace std;
int main()
{
char buff[50];
char ch;
ifstream is("test.txt");
if (!is)
cout << "Unable to open " << endl;
while(is)
{
ch=(char)is.get();
if(ch != EOF)//If EOF is not checked then
//EOF converted as a char is displyed as
// last char of the file
cout << ch;
}
cout << "\n\n###########\n\n";
is.clear(); //clearing ios_base::eofbit which was set
//in previous action
is.seekg(0,ios_base::beg); //Going back to start of File
while(is)
{
is.get(buff,50,'\n');
cout << buff ;
cout << "\n--------------\n";
is.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
//Flushing the is stream as '\n' was left by get fn
}
cout << "\n\n##############\n\n";
is.clear();
is.seekg(0,ios_base::beg);
while(!is.eof())
{
is.getline(buff,50,'\n');
cout << buff;
cout << "\n--------------\n";
//No need to flush the is stream as '\n'
//was extracted and discarded by getline
}
cout << "\n\n$$$$$$$$$$$$$$\n\n";
is.clear();
is.seekg(0,ios_base::end);
int size=is.tellg();
is.seekg(0,ios_base::beg);
cout << "size : " << size << endl;
//char* readBuff = (char *) ::operator new(sizeof(char)*size);
char* readBuff = new char[size];
is.read(readBuff,size);
cout << readBuff;
delete(readBuff);
is.close();
return 0;
}
OutPut:
Gaurav#Gaurav-PC /cygdrive/d/Trial
$ ./Trial
This is Line One
This is Line Two
This is Line Three
This is Line Four
This is Line Five
###########
This is Line One
--------------
This is Line Two
--------------
This is Line Three
--------------
This is Line Four
--------------
This is Line Five
--------------
##############
This is Line One
--------------
This is Line Two
--------------
This is Line Three
--------------
This is Line Four
--------------
This is Line Five
--------------
$$$$$$$$$$$$$$
size : 92
This is Line One
This is Line Two
This is Line Three
This is Line Four
This is Line Five▒u
There are some issues which I want to ask and get clarified:
1) When I use get as below
while(is)
{
is.get(buff,50,'\n');
cout << buff ;
// cout << "\n--------------\n";
is.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
//Flushing the is stream as '\n' was left by get fn
}
i.e. I commented out cout << "\n--------------\n"; then the file is read as
###########
This is Line Fivee
i.e. it misses first four lines and reads only last one with extra 'e' .. not able to figure out why so ?
2) When I use getline as below:
// while(!is.eof())
while(is)
{
is.getline(buff,50,'\n');
cout << buff;
cout << "\n--------------\n";
//No need to flush the is stream as '\n'
//was extracted and discarded by getline
}
i.e. I used while(is) instead of while(!is.eof()) - I got the output:
##############
This is Line One
--------------
This is Line Two
--------------
This is Line Three
--------------
This is Line Four
--------------
This is Line Five
--------------
--------------
i.e. after the last line I get two extra lines. Again not able to figure out why so?
3) With read function the size I am getting is 92 where as total number of charaters in the file is 89 including EOF,spaces and '\n'. Also the last line shows two garbage characters after rearing the last character of the file. Why such behavior?
cout << "\n\n$$$$$$$$$$$$$$\n\n";
is.clear();
is.seekg(0,ios_base::end);
int size=is.tellg();
is.seekg(0,ios_base::beg);
cout << "size : " << size << endl;
//char* readBuff = (char *) ::operator new(sizeof(char)*size);
char* readBuff = new char[size];
is.read(readBuff,size);
cout << readBuff;
delete(readBuff);
OutPut:
$$$$$$$$$$$$$$
size : 92
This is Line One
This is Line Two
This is Line Three
This is Line Four
This is Line Five▒u
Thanks
EDIT:
As Per Answer received by Mats Peterson , I tried below code:
while(is.get(buff,50,'\n'))
{
cout << buff ;
//cout << "\n--------------\n";
is.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
//Flushing the is stream as '\n' was left by get fn
}
cout << "\n\n##############\n\n";
is.clear();
is.seekg(0,ios_base::beg);
// while(!is.eof())
while(is.getline(buff,50,'\n'))
{
cout << buff;
//cout << "\n--------------\n";
//No need to flush the is stream as '\n'
//was extracted and discarded by getline
}
But got the output:
###########
This is Line Fivee
##############
This is Line Fivee
i.e Only Last line read... if I uncomment //cout << "\n--------------\n"; I get proper reading
#Down Votes At least comment what made you do so? I faced this issue that is why asked here to gain more insight from experts..
In the first two questions, you are because you are reading "one more than you have", which is a typical consequence of "the failure state is not set until we have tried to read past the end". This is why you should use
while(is.get(... ))
while(is.getline(...))
as the condtions for ending loops - because that will not run the loop when the read fails.
The third issue is because Windows used "CR+LF" for newlines, where reading a file in text mode (which is the default) collapses these into a single newline character. So the size of your file according to is.tellg is larger by one character for each newline than the data you actually read. You can use is.gcount() to see how many characters you ACTUALLY read. (and if (!is.read(... )) actual = is.gcount(); else actual = size; should give you a complete piece of code).
One of the main reason of reading extra value or line which is termed as garbage value (i thinks it so) because of use of eof ( you have used it) . when we used it, for ex, to read a character from a file, then it reads the character but because file does'nt end as well loop does'nt end at point, so it again reads extra value from file. so main thing which i want to say is that avoid function eof in any looping statement untill to end the file reading and not in any input-output conditions.
Related
I am trying to get the number of lines and words from a text file in c++. But one extra line is being read by compiler.
#include <iostream>
#include<fstream>
using namespace std;
int main(void)
{
ifstream f;
string name;
char a;
int line, words;
line = 0;
words = 0;
f.open("file.txt");
while (f) {
f.get(a);
if (a == '\n')
{
line++;
words++;
}
if (a == ' ')
{
words++;
}
}
f.close();
cout << "Number of words in file : " << words << endl;
cout << "Numbers of lines in the file : " << line << endl;
}
OUTPUT:-
Number of words in file : 79
Numbers of lines in the file : 3
file.txt:-
This C++ Program which counts the number of lines in a file. The program creates an input file stream, reads a line on every iteration of a while loop, increments the count variable and the count variable is printed on the screen.
Here is source code of the C++ program which counts the number of lines in a file. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
I am puzzled why one extra line is being read. Kindly help.
You are not checking if f.get() succeeds or fails. When it does fail, a is not updated, and you are not breaking the loop yet, so you end up acting on a's previous value again. And then the next loop iteration detects the failure and breaks the loop.
Change this:
while (f) {
f.get(a);
...
}
to this instead:
while (f.get(a)) {
...
}
That being said, you are also not taking into account that the last line in a file may not end with '\n', and if it does not then you are not counting that line. And also, you are assuming that every line always has at least 1 word in it, as you are incrementing words on every '\n' even for lines that have no words in them.
I would suggest using std::getline() to read and count lines, and std::istringstream to read and count the words in each line, eg:
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
using namespace std;
int main(void)
{
ifstream f;
string line, word;
int lines = 0, words = 0;
f.open("file.txt");
while (getline(f, line))
{
++lines;
std::istringstream iss(line);
while (iss >> word) {
++words;
}
}
f.close();
cout << "Number of words in file : " << words << endl;
cout << "Numbers of lines in the file : " << line << endl;
}
It is because you do not check in what state is stream that f.get(a) returns.
I have created this function to return the number of lines in a text file:
int numberOfLinesInFile(string dataFile){
ifstream inputFile;
inputFile.open("data.txt");
int numberOfLines, data;
while(!inputFile.eof()){
inputFile >> data;
numberOfLines++;
}
cout << "Number of lines: " << numberOfLines << endl;
inputFile.close();
return numberOfLines;
}
An interesting thing that I noticed is that the function counts the number of the non-blank lines (and an additional blank line at the end) correctly, but the additional blank lines are not being counted. For example, if this is my file:
234
453
657
then the function returns 4, since there are four lines in the text file. However, if my file contains more than one blank line at the end:
234
453
657
the function returns 4 again.
Why is this happening? Does this have to do with the eof instruction?
EDIT:
Okay, thank you #MikeCAT for making me understand where the problem was. Based on #MikeCAT's answer, I changed the while loop as:
while(!inputFile.eof()){
if(inputFile >> data)
numberOfLines++;
else{
cout << "Error while reading line " << numberOfLines + 1 << "!" << endl;
}
}
I was incrementing numberOfLines without making sure that the line was actually read.
Firstly, you are doing numberOfLines++; after inputFile >> data; without checking if the reading is successful. This is bad and lead to an extra counting.
Secondly, inputFile >> data; reads integer and skip whitespace characters before integers.
To avoid these problems, you should use std::getline() to count lines.
Also don't forget to initialize numberOfLines.
int numberOfLinesInFile(string dataFile){
ifstream inputFile;
inputFile.open("data.txt");
int numberOfLines = 0;
std::string data;
while(std::getline(inputFile, data)){
numberOfLines++;
}
cout << "Number of lines: " << numberOfLines << endl;
inputFile.close();
return numberOfLines;
}
I'm just learning about text file input/output. I have outputted a file which contains a header and 10 rows of data underneath it.
I now want to read this back to the main function. This works for me if I leave out the header in the text file, but if I leave the header in, I get an infinite loop.
How can I skip the 1st line (the header line) in reading this data back, or if possible, read back the header as well as the data?
Here is what I have so far:
void fileRead(int x2[], double y2[], int& n, char filename)
{
ifstream fin ("pendulum.txt"); // fin is an input file stream
if(!fin) //same as fin.fail()
{
cerr << "Failure to open pendulum.txt for input" << endl;
exit(1);
}
int j = 0, dummy = 0; //index of the first value j and dummy value
while(!fin.eof()) //loop while not end of file
{
fin >> dummy >> x2[j] >> y2[j];
cout << setw(5) << fixed << j
<< setw(12) << scientific << x2[j] << " "
<< setw(12) << y2[j] << endl; //print a copy on screen
j += 1;
}
fin.close(); //close the input file
}
You can first read the header of the file then the real contents you want as follows:
string line;
getline(fin, line);//just skip the line contents if you do not want header
while (fin >> dummy >> x2[j] >> y2[j] )
{ //^^if you do not always have a dummy at the beginning of line
//you can remove dummy when you read the rest of the file
//do something
}
Your best bet would be to use
fin.ignore(10000,'\n');
http://www.cplusplus.com/reference/istream/istream/ignore/
This will ignore the first 10000 character in the file, or ignore the characters until a newline is reached. The 10000 is fairly arbitrary and should be a number that will always be longer than the maximum line length.
man, this gentleman over there helped me quite a lot. You see, everyone says to use getline(); to skip one line, but the problem is that sometimes you dont want to store anything in a buffer, so ignore() makes much more sense to me. Well so I would like to back up our fella's answer by adding that, you could use " numeric_limits::max()" which will make it have no limit, it will ignore until it finds the delimiter...
`
#include <iostream>
#include <fstream>
#include <limits>
using std::streamsize;
int main() {
ifstream fin ("pendulum.txt");
fin.ignore(numeric_limits<streamsize>::max(),'\n');
}
`
http://www.cplusplus.com/reference/limits/numeric_limits/
I have a program which reads integers from a text file and skips non-integers and strange symbols. Then text file looks like:
# Matrix A // this line should be skipped because it contains # symbol
1 1 2
1 1$ 2.1 // this line should be skipped because it contains 2.1 and $
3 4 5
I have to print out the matrix without strange symbols and non-integers line. That is the output should be:
1 1 2
3 4 5
My code
ifstream matrixAFile("a.txt", ios::in); // open file a.txt
if (!matrixAFile)
{
cerr << "Error: File could not be opened !!!" << endl;
exit(1);
}
int i, j, k;
while (matrixAFile >> i >> j >> k)
{
cout << i << ' ' << j << ' ' << k;
cout << endl;
}
But it fails when it gets the first # symbol. Anyone helps please ?
Your problem is with this code.
int i, j, k;
while (matrixAFile >> i >> j >> k)
The assignment is "Find out if the line contains integers"
But your code is saying "I already know that the line contains integers"
If you are set to three integers per row, I suggest this pattern:
#include <fstream>
#include <sstream>
#include <string>
std::ifstream infile("matrix.txt");
for (std::string line; std::getline(infile, line); )
{
int a, b, c;
if (!(std::istringstream(line) >> a >> b >> c))
{
std::cerr << "Skipping unparsable line '" << line << "'\n";
continue;
}
std::cout << a << ' ' << b << ' ' << c << std::endl;
}
If the number of numbers per line is variable, you could use a skip condition like this:
line.find_first_not_of(" 0123456789") != std::string::npos
Of course, this fails at the # character: The # isn't an integer and, thus, reading it as an integer fails. What you could do is try to read three integers. If this fails and you haven't reached EOF (i.e., matrixAFile.eof() yields false, you can clear() the error flags, and ignore() everything up to a newline. The error recovery would look something like this:
matrixAFile.clear();
matrixAFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Note, that you need to bail out if you failed because eof() is true.
Since it's an assignment, I'm not giving full answer.
Read the data line by line to a string(call it str),
Split str into substrings,
In each substring, check if it's convertible to integer value.
Another trick is to read a line, then check that every char is between 0-9. It works if you don't need to consider negative numbers.
I think I'd just read a line at a time as a string. I'd copy the string to the output as long as it contains only digits, white-space and (possibly) -.
when running the following code, the amount of lines will read on less then there actually is (if the input file is main itself, or otherwise)
why is this and how can i change that fact (besides for just adding 1)?
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
// open text file for input
string file_name;
cout << "please enter file name: ";
cin >> file_name;
// associate the input file stream with a text file
ifstream infile(file_name.c_str());
// error checking for a valid filename
if ( !infile ) {
cerr << "Unable to open file "
<< file_name << " -- quitting!\n";
return( -1 );
}
else cout << "\n";
// some data structures to perform the function
vector<string> lines_of_text;
string textline;
// read in text file, line by line
while (getline( infile, textline, '\n' )) {
// add the new element to the vector
lines_of_text.push_back( textline );
// print the 'back' vector element - see the STL documentation
cout << "line read: " << lines_of_text.back() << "\n";
}
cout<<lines_of_text.size();
return 0;
}
The code you have is sound. Here's a small test case that might help:
void read_lines(std::istream& input) {
using namespace std;
vector<string> lines;
for (string line; getline(input, line);) {
lines.push_back(line);
cout << "read: " << lines.back() << '\n';
}
cout << "size: " << lines.size() << '\n';
}
int main() {
{
std::istringstream ss ("abc\n\n");
read_lines(ss);
}
std::cout << "---\n";
{
std::istringstream ss ("abc\n123\n");
read_lines(ss);
}
std::cout << "---\n";
{
std::istringstream ss ("abc\n123"); // last line missing newline
read_lines(ss);
}
return 0;
}
Output:
read: abc
read:
size: 2
---
read: abc
read: 123
size: 2
---
read: abc
read: 123
size: 2
I think I have tracked down the source of your problem. In Code::Blocks, a completely empty file will report that there is 1 line in it (the current one) in the gizmo on the status bar at the bottom of the IDE. This means that were you actually to enter a line of text, it would be line 1. In other words, Code::Blocks will normally over-report the number of actual lines in a file. You should never depend on CB, or any other IDE, to find out info on files - that's not what they are for.
Well, if the last line of your file is just '\n', you don't push it into the vector. If you want it to be there, change the loop to:
while (getline( infile, textline, '\n' ).gcount() > 0)
{
if (infile.fail()) break; //An error occurred, break or do something else
// add the new element to the vector
lines_of_text.push_back( textline );
// print the 'back' vector element - see the STL documentation
cout << "line read: " << lines_of_text.back() << "\n";
}
Use the gcount() member to check how many characters were read in the last read - this will return 1 if it only read a delimiter character.
Ok so here is an explanation that you will hopefully understand. Your code should work fine if the file we're talking about doesn't end with newline. But what if it does? Let's say it looks like this:
"line 1"
"line 2"
""
Or as a sequence of characters:
line 1\nline 2\n
This file has THREE lines -- the last one being empty but it's there. After calling getline twice, you've read all the characters from the file. The third call to getline will say oops, end of file, sorry no more characters so you'll see only two lines of text.