File read and write while reading the file line by line - c++

Program:
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
FILE *f;
char* line;
size_t ln=100;
char* s;
line=new char[100];
s=new char[100];
cout<<"input key"<<endl;
cin>>s;
f=fopen("parvin.txt","r");
if(f==NULL)
{
cout<<" no file TO read so creating for writing "<<endl;
//return 0;
f=fopen("parvin.txt","w");
fputs(s,f);
fputc('\n',f);
}
else
{
while(! feof(f))
{
fgets(line,100,f);
cout<<line<<endl;
//if(!strncmp(line,s,strlen(line)-1))
if(strcmp(line,s)== 0 )
{
cout<<"duplicate found"<<endl;
fclose(f);
return 0;
}
}
fclose(f);
f=fopen("parvin.txt","a+");
fputs(s,f);
fputc('\n',f);
}
fclose(f);
}
Here the above program where I like to read an input string and write it into file provided the string is not present already in file.
take input string
open file in read mode.
if it is first time entry file will not be there if file pointer return NULL, create a file to write mode and write the
inputted string.
if file already there then read file line by line and compare with input string if match with any line then return and close.
other wise open the same file in write mode and write the inputted string.
But it is not working properly..
strcmp not executing properly.... with the duplicate entry also it
dont go into that loop of "duplicae found" .
please if anyone can help ...

The fgets:
fgets(line,100,f);
consumes the newline character from f and stores it in line. But s doesn't contain the newline character. So, the strcmp returns a non-zero number as the strings(s and f) are different.
Strip the newline character by using
line[strcspn(line, "\n")] = '\0';
just after the fgets. The strcspn function, in your case, returns the number of characters until a \n in line. If \n is not found in line, it returns the length of the string line(strlen(line)).
Also, read Why is while ( !feof (file) ) always wrong?. Replace
while(!feof(f))
with
while(fgets(line,100,f)) //Same as `while(fgets(line,100,f) != NULL)`
and don't forget to remove the fgets from the body of the loop to fix this issue.

Use
while(fgets(line,100,f)!=NULL)

Related

Unexpected output after reading from a binary file in turbo c++

After I have written into a file when reading it I get unexpected output.
The code I wrote is :
#include<fstream.h>
#include<conio.h>
#include<string.h>
struct test
{
char que[100];
char ans[20];
};
int main()
{
test s, d;
clrscr();
ofstream out("test.dat", ios::binary | ios::app);
ifstream in("test.dat", ios::binary | ios::in);
strcpy(s.que, "2.How many ways the letters of the word abas be arranged to form words with or without meaning");
strcpy(s.ans, "180");
out.write((char*) &s, sizeof(test));
while(!in.eof())
{
in.getline((char*) &d, sizeof(test));
cout << d.que << '\n' << d.ans;
}
getch();
return 0;
}
The output that I get is :
2.How many ways the letters of the word abas be arranged to form words with or w
ithout meaning
180
180
This is the output I get along with some arbitrary characters in between.
What have I done wrong? Why is the string that I stored in s.ans written into s.que as well?
I got this to work with the following changes:
Add out.close() after writing to the file to flush the output buffer.
Replace getline with read, to retrieve the bytes as write wrote them. getline might give different results under some conditions.
Move the read statement into the while condition:
while(in.read((char *) &d, sizeof(test)))
With the read statement in the body of the loop in.eof() will not return true immediately after reading the last test object so the loop will then execute one last time.

How to read content of the file and save it to string type variable? Why there is empty space?

This is how I get the name of the file from the command line and open a file and save the content of the file line by line to a string. All the procedures works fine except three empty spaces at the beginning of the file. Is anyone can say why these empty spaces occurred and how can I ignore them?
string filename = "input.txt";
char *a=new char[filename.size()+1];
a[filename.size()]=0;
memcpy(a,filename.c_str(),filename.size());
ifstream fin(a);
if(!fin.good()){
cout<<" = File does not exist ->> No File for reading\n";
exit(1);
}
string s;
while(!fin.eof()){
string tmp;
getline(fin,tmp);
s.append(tmp);
if(s[s.size()-1] == '.')
{
//Do nothing
}
else
{
s.append(" ");
}
cout<<s<<endl;
The most probable cause is that your file is encoded in something else than ASCII. It contains a bunch of unprintable bytes and the string you on the screen is the result of your terminal interpreting those bytes. To confirm this, print the size of s after the reading is done. It should be larger than the number of characters you see on the screen.
Other issues:
string filename = "input.txt";
char *a=new char[filename.size()+1];
a[filename.size()]=0;
memcpy(a,filename.c_str(),filename.size());
ifstream fin(a);
is quite an overzealous way to go about it. Just write ifstream fin(a.c_str());, or simply ifstream fin(a); in C++11.
Next,
while(!fin.eof()){
is almost surely a bug. eof() does not tell if you the next read will succeed, only whether the last one reached eof or not. Using it this way will tipically result in last line seemingly being read twice.
Always, always, check for success of a read operation before you use the result. That's idiomatically done by putting getline in the loop condition: while (getline(fin, tmp))

Cannot read binary file with bracket characters in C++

I have function which reads a File & checks its contents.
The file contains some binary content along with non alphabet characters like (), =, divided by symbol, etc.
The function which does the reading is:
int FindMyWord(const char *fileName)
{
ifstream myFile (fileName);
if(!myFile)
return false;
string wordSearch = "MyWord";
string line;
int result = 0;
while(getline(myFile, line))
{
if(line.find(wordSearch) != string::npos)
result++;
}
//if(!myFile.eof() || !myFile)
if(!myFile)
printf("Problem Reading the File: %s\n", (const char *)fileName);
myFile.close();
return result;
}
I am having these 2 problems:
If a line contains binary characters then it is not reading the complete line, just reading the first word (atleast that's what I am observing by opening the file in VS2010).
When it encounters the character ( for the beginning of a line the while loop is terminated & the printf() is printed.
If string::getline() cannot read such characters then what is the solution?
Thank You.
UPDATE: The Image of some of the binary data in the file:
A text input stream should not fail on a bracket character.
If you actually need a binary stream, use ifstream(filename, std::ios::binary)
Have a read through the std::getline docs at cppreference.com. You should check the failbit on the stream if you have any odd behaviour.

getline() reads an extra line

ifstream file("file.txt");
if(file.fail())
{
cout<<"Could not open the file";
exit(1);
}
else
{
while(file)
{
file.getline(line[l],80);
cout<<line[l++]<<"\n";
}
}
I am using a two dimensional character array to keep the text (more than one line) read from a file to count the number of lines and words in the file but the problem is that getline always reads an extra line.
Your code as I'm writing this:
ifstream file("file.txt");
if(file.fail())
{
cout<<"Could not open the file";
exit(1);
}
else
{
while(file)
{
file.getline(line[l],80);
cout<<line[l++]<<"\n";
}
}
The first time getline fails, you still increment the line counter and output the (non-existing) line.
Always check for an error.
extra advice: use std::string from the <string> header, and use its getline function.
cheers & hth.
The problem is when you're at the end of the file the test on file will still succeed because you have not yet read past the end of file. So you need to test the return from getline() as well.
Since you need to test the return from getline() to see if it succeeded, you may as well put it right in the while loop:
while (file.getline(line[l], 80))
cout << line[l++] << "\n";
This way you don't need a separate test on file and getline().
This will solve your problem:
ifstream file("file.txt");
if(!file.good())
{
cout<<"Could not open the file";
exit(1);
}
else
{
while(file)
{
file.getline(line[l],80);
if(!file.eof())
cout<<line[l++]<<"\n";
}
}
Its more robust
Does the file end with a newline? If it does, the EOF flag will not be triggered until one extra loop passes. For example, if the file is
abc\n
def\n
Then the loop will be run 3 times, the first time it will get abc, the second time it will get def and the third time it will get nothing. That's probably why you see an additional line.
Try checking the failbit on the stream AFTER the getline.
Only do the cout if file.good() is true. The extra line you're seeing comes from the last call to file.getline() which reads past the end of the file.

getline() returns empty line in Eclipse but working properly in Dev C++

Here is my code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main() {
string line;
ifstream inputFile;
inputFile.open("input.txt");
do {
getline(inputFile, line);
cout << line << endl;
} while (line != "0");
return 0;
}
input.txt content:
5 9 2 9 3
8 2 8 2 1
0
In Enclipse, it goes to infinite-loop. I'm using MinGW 5.1.6 + Eclipse CDT.
I tried many things but I couldn't find the problem.
Since you are on windows try:
} while (line != "0\r");
The last line is stored as "0\r\n". The \n is used as the line delimiter by getline so the actual line read will be "0\r"
or
you can convert the dos format file to UNIX format using command
dos2unix input.txt
Now your original program should work. The command will change the \r\n at the end of the line to \n
Also you should always do error checking after you try to open a file, something like:
inputFile.open("input.txt");
if(! inputFile.is_open()) {
cerr<< "Error opening file";
exit(1);
}
It will create an infinite loop if no line contains exactly 0. For example 0\n is not the same thing as 0. My guess is that that is your problem.
EDIT: To elaborate, getline should be discarding the newline. Perhaps the newline encoding of your file wrong (i.e. windows vs. unix).
Your main problem is working directory.
Because you are specifying a file using a relative path it searches for the file from the current working directory. The working directory can be specified by your dev environment. (Note: The working directory is not necessarily the same directory where the executable lives (this is a common assumption among beginners but only holds in very special circumstances)).
Though you have a special end of input marker "0" you should also check that the getline() is not failing (as it could error out for other reasons (including beady formatted input). As such it is usually best to check the condition of the file as you read it.
int main()
{
string line;
ifstream inputFile;
inputFile.open("input.txt");
while((getline(inputfile, line)) && (line != "0"))
{
// loop only entered if getline() worked and line !="0"
// In the original an infinite loop is entered when bad input results in EOF being hit.
cout << line << endl;
}
if (inputfile)
{
cout << line << endl; // If you really really really want to print the "0"
// Personally I think doing anything with the termination
// sequence is a mistake but added here to satisfy comments.
}
return 0;
}