473,465 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem using strlen

What is worng with my code.
I can reads all the lines all right but it doesn't return me the length
of lines.
It returns 0 for each line

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

int main()
{
fstream in("example.txt");
static string line;
int len = line.length();
while(getline(in,line))
{
cout<< line <<'\n';
cout<<len<<endl;
}
return 0;
}

Aug 1 '06 #1
8 1352
ricky schrieb:
What is worng with my code.
I can reads all the lines all right but it doesn't return me the length
of lines.
It returns 0 for each line

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

int main()
{
fstream in("example.txt");
static string line;
int len = line.length();
How often does this ^^^^^ get executed ?
while(getline(in,line))
{
cout<< line <<'\n';
cout<<len<<endl;
}
return 0;
}
/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Aug 1 '06 #2
You're creating an empty string (line)....and setting its length to the
len variable. As you then put strings into your line, the len variable
is never recalculated...it remains at what it started at i.e. 0. In
other words, the len variable doesn't dynamically update itself every
time you're line changes...you need to explicitly recalculate the
string length each time...ie.

while(getline(in,line))
{
len = line.length();
cout<< line <<'\n';
cout<<len<<endl;
}

ricky wrote:
What is worng with my code.
I can reads all the lines all right but it doesn't return me the length
of lines.
It returns 0 for each line

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

int main()
{
fstream in("example.txt");
static string line;
int len = line.length();
while(getline(in,line))
{
cout<< line <<'\n';
cout<<len<<endl;
}
return 0;
}
Aug 1 '06 #3

"ricky" <ri*******@yahoo.co.ina écrit dans le message de news:
11**********************@i42g2000cwa.googlegroups. com...
What is worng with my code.
I can reads all the lines all right but it doesn't return me the length
of lines.
It returns 0 for each line

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

int main()
{
fstream in("example.txt");
static string line;
int len = line.length();
this should gives you 0
while(getline(in,line))
extract a line from the text file.. ok
{
cout<< line <<'\n';
print the line
cout<<len<<endl;
print len which is still 0 because you get the lenght before getting the
string
}
return 0;
oupss forget to close the file....
}

Aug 1 '06 #4
Great!! that worked
thanks Siam...
Siam wrote:
You're creating an empty string (line)....and setting its length to the
len variable. As you then put strings into your line, the len variable
is never recalculated...it remains at what it started at i.e. 0. In
other words, the len variable doesn't dynamically update itself every
time you're line changes...you need to explicitly recalculate the
string length each time...ie.

while(getline(in,line))
{
len = line.length();
cout<< line <<'\n';
cout<<len<<endl;
}

ricky wrote:
What is worng with my code.
I can reads all the lines all right but it doesn't return me the length
of lines.
It returns 0 for each line

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

int main()
{
fstream in("example.txt");
static string line;
int len = line.length();
while(getline(in,line))
{
cout<< line <<'\n';
cout<<len<<endl;
}
return 0;
}
Aug 1 '06 #5
Yeah it is working now.
thanks to all of u.
Eric Pruneau wrote:
"ricky" <ri*******@yahoo.co.ina écrit dans le message de news:
11**********************@i42g2000cwa.googlegroups. com...
What is worng with my code.
I can reads all the lines all right but it doesn't return me the length
of lines.
It returns 0 for each line

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

int main()
{
fstream in("example.txt");
static string line;
int len = line.length();

this should gives you 0
while(getline(in,line))

extract a line from the text file.. ok
{
cout<< line <<'\n';
print the line
cout<<len<<endl;
print len which is still 0 because you get the lenght before getting the
string
}
return 0;
oupss forget to close the file....
}
Aug 1 '06 #6
Siam wrote:
You're creating an empty string (line)
Please see my .sig below.

Brian
--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.htm>
Aug 1 '06 #7
ricky wrote:
Great!! that worked
Please see my .sig below.

Brian
--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.htm>
Aug 1 '06 #8
Eric Pruneau <ep******@infinition.comwrote:
>
"ricky" <ri*******@yahoo.co.ina ?crit dans le message de news:
11**********************@i42g2000cwa.googlegroups. com...
>What is worng with my code.
I can reads all the lines all right but it doesn't return me the length
of lines.
It returns 0 for each line

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

int main()
{
fstream in("example.txt");
[code elided]
oupss forget to close the file....
>}
It is not necessary, since fstream's destructor will take care of that
when "in" goes out of scope. That is the point of RAII.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 1 '06 #9

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

Similar topics

4
by: muser | last post by:
I'am having problem with the following: rec1len = strlen(temp1); temp1 is a character array, multi dimensional array. it has been initialised as char temp1; int rec1len. error for this...
2
by: Robert Stearns | last post by:
After working well for most of a week, my application with the mysterious php/db2 problem is now failing again. No one has worked on the php code this week. We're running DB2 8.1.5, php 4.3.6...
5
by: cpptutor2000 | last post by:
I am compiling and running the following code snippet on a Linux box - I am really puzzled by the answers. Could someone please tell me what might be wrong? void test(){ int m = 0; int n = 0;...
15
by: Materialised | last post by:
I am having a problem with the following functions, I've been racking my brains trying to figure out where I am going wrong. What I need to do is return a formatted string with the current date and...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
11
by: zaebos | last post by:
hi, i have this code which is part of a main program, to email from within the program a log file: int MailIt (char *mailserver, char *emailto, char *emailfrom, char *emailsubject, char...
3
by: giladt | last post by:
Hi, I'm a novice programmer and I've come across the following problem: I am trying to get strlen for a cosntant char array but I get back a void no matter what I try. The code looks like...
8
by: Martin the Third | last post by:
Hi, I need some help! I'm writing an infinite-precision floating point library called ipfloat (I know infinite is a misnomer - but arbitrary was taken). A quick overview: I'm storing numbers as...
3
by: FireHead | last post by:
Hello comp.lang.c Couple of years ago I created a topic as to how I can use the function fgets so that I can read a line of text from a source of data. After a long time I have finally got all...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.