473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with lengh of string

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;

}

Aug 2 '06 #1
2 1326
ricky wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 2 '06 #2

"ricky" <ri*******@yahoo.co.ina écrit dans le message de news:
11*********************@h48g2000cwc.googlegroups.c om...
>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
Aug 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Thomas | last post by:
Hi! How can i get the optimal richtext width size, when depending on the variable text lengh. The text can be colored or with some differnt fontsizes. I dont want use scrollbars. Is there a...
6
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query...
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
7
by: Ankit Aneja | last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx some .aspx pages are in root ,some in folder named admin and some in folder named user aspx pages which are in user...
4
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
5
by: $hiv..... | last post by:
hi, How to use Zero lengh Arrays.. I searched but could not get much information regarding this where Do i get links abt this
2
by: Moham12345 | last post by:
Hi, Im struggling here ... is it possible to check for a string pattern like a valid url in a bigger string. And then return the matching pattern string? so if i had a string like : "This...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.