Problem with lengh of string

R

ricky

I am reading the lines form the file and 'len' is returning the length
of each line.Now the problem is if i am not wrong windows uses carriage
return followed by a line feed to mark the end of the line. so for each
line 'len' should return be two extra characters ex. if i have file
gillian
dave
robby
so for gillian --it should return len 9(including \r\n according to
windows)
but the following function is returning 8. Is there anything wrong the
way i am doing it.

#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;
int len;
while(getline(in,line))
{
len=line.length();
cout<<line<<endl;
cout<<len<<endl;
}
}

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, "raka");

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

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

return 0;

}
 
V

Victor Bazarov

ricky said:
I am reading the lines form the file and 'len' is returning the length
of each line.Now the problem is if i am not wrong windows uses
carriage return followed by a line feed to mark the end of the line.
so for each line 'len' should return be two extra characters ex. if i
have file gillian
dave
robby
so for gillian --it should return len 9(including \r\n according to
windows)
but the following function is returning 8. Is there anything wrong the
way i am doing it.

You're opening your file with default mode, which is "text". You should
try opening it in "binary" mode. RTFM about those.

V
 
E

Eric Pruneau

ricky said:
I am reading the lines form the file and 'len' is returning the length
of each line.Now the problem is if i am not wrong windows uses carriage
return followed by a line feed to mark the end of the line. so for each
line 'len' should return be two extra characters ex. if i have file
gillian
dave
robby
so for gillian --it should return len 9(including \r\n according to
windows)
but the following function is returning 8. Is there anything wrong the
way i am doing it.

getline extract characters until the delimiter is read.
Now the delimiter can be:
1. user specified (when using the optional third parameter of getline)
2. \n if no user specified parameter are used
3. end of file

the delimiter is extracted but NOT stored in the string


Eric
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top