How to get rid of extra characters

R

ricky

Can anybody help with the function to get rid of extra characters in
the file.
I want to remove the string from the file.So i read from input file and
pass the string say "john" if found dnt write it to the ouput file but
if not found write all the line to the output file
so i read line by line
cin.get(input,line)
if(line != s)
output<<line

The problem is the input file look like
murphy
john
bridget
sarah

but if i read character by character the file would look like
m
u
r
p
h
y


j
o
h
n


s
a
r
a
h
So if pass string john and compare it line by line it won't match cause
there r two extra character in the end as u can see while reading
character by character
Can any please help to get rid of these extra characters before i do
the comparison
 
R

Rolf Magnus

ricky said:
Can anybody help with the function to get rid of extra characters in
the file.
I want to remove the string from the file.So i read from input file and
pass the string say "john" if found dnt write it to the ouput file but
if not found write all the line to the output file
so i read line by line
cin.get(input,line)

Get doesn't read lines. It reads single characters. If you want to read a
line, use getline. Best is to use the nonmeber version.
if(line != s)

How are line and s declared?
output<<line

There's a semicolon missing here.

Please, next time post a minimal, but _complete_ program that shows your
problem, not just some fragments.
 
R

ricky

#include <fstream>
#include <istream>
#include <iostream>
#include <ostream>
#include <string>

using namespace std;

void omit(istream& in, ostream& out, const string s)
{
static string line;
while(getline(in, line))
if(line != s)
out << line << '\n';

}

int main()
{
const string in_name("example.txt");
const string out_name("test.txt");

ifstream input(in_name.c_str());
ofstream output(out_name.c_str());

if(!input)
cerr << "Cannot open input\n";

if(!output)
cerr << "Cannot open output\n";

if(input && output)
omit(input, output, "murphy");

if(!input.eof())
cerr << "Error reading input\n";

if(!output)
cerr << "Error writing output\n";

return 0;

}
So when i pass murphy like this. and then compare it to all the lines
in example.txt file i have. I cannot find it even though it is there
becoz muphy has two extra invisible characters in the end in the
file(see the output from my ealier post when i read character by
character). So I want to get rid of those extra characters before i do
the comparison.
Please help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top