By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

counting lines in text file

P: n/a
hi im having trouble counting lines in a text file, i have the
following code

int node1, node2, i;
char name[2];
float value;

ifstream fin;
fin.open(OpenDialog1->FileName.c_str());
i=1;
// while (!fin.eof())
for(;;)
{
fin>>name>>node1>>node2>>value;

switch(i){
case 1:
Label1->Caption = value;
break;
case 2:
Label2->Caption = value;
break;
case 3:
Label3->Caption = value;
break;
case 4:
Label4->Caption = value;
break;
case 5:
Label5->Caption = value;
break;
}
i++;
if (fin.eof()) break;
}

im using i to increment each time the loop goes round thus counting the
no. of lines. however the line ' fin>>name>>node1>>node2>>value;' keeps
setting i to 0 so it never counts it. How do i fix this, or is there
another way to do this?

thx

Dec 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 13 Dec 2005 17:23:21 -0800 in comp.lang.c++, an********@gmail.com
wrote,
int node1, node2, i;
int i = 0;
// while (!fin.eof())
for(;;)
{

fin>>name>>node1>>node2>>value;
Avoid looping on !eof(). Make that
while (fin>>name>>node1>>node2>>value) { ...
however the line ' fin>>name>>node1>>node2>>value;' keeps setting i to 0


I don't think so. However, i appears to be uninitialized.

Dec 14 '05 #2

P: n/a
an********@gmail.com wrote:

char name[2];
[snip]

im using i to increment each time the loop goes round thus counting the
no. of lines. however the line ' fin>>name>>node1>>node2>>value;' keeps
setting i to 0 so it never counts it. How do i fix this, or is there
another way to do this?


What is stored in 'name'?

Note that name has only room for a size-2 string.
That is: one lettter plus the terminating '\0'

Other then that:
Your use of eof is wrong, but that does not explain the
magical change of 'i'.

--
Karl Heinz Buchegger
kb******@gascad.at
Dec 14 '05 #3

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:43***************@gascad.at...
an********@gmail.com wrote:

char name[2];


[snip]

im using i to increment each time the loop goes round thus counting the
no. of lines. however the line ' fin>>name>>node1>>node2>>value;' keeps
setting i to 0 so it never counts it. How do i fix this, or is there
another way to do this?


What is stored in 'name'?

Note that name has only room for a size-2 string.
That is: one lettter plus the terminating '\0'

Other then that:
Your use of eof is wrong, but that does not explain the
magical change of 'i'.


IMHO that magical change of i could only be achieved by aliasing or
completely broken optimization of the compiler. However, from the code
posted I doubt that very much. I'm not sure what the overall objective of
the OP is, but if it is only counting lines I'd suggest the following code,
which does not care about the format and contents at all:

int CountLines( std::ifstream& In )
{
return static_cast<int>( std::count( std::istreambuf_iterator<char>( In),
std::istreambuf_iterator<char>(), '\n' ) );
}

Cheers
Chris
Dec 14 '05 #4

P: n/a
Chris Theis wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:43***************@gascad.at...
an********@gmail.com wrote:

char name[2];


[snip]

im using i to increment each time the loop goes round thus counting the
no. of lines. however the line ' fin>>name>>node1>>node2>>value;' keeps
setting i to 0 so it never counts it. How do i fix this, or is there
another way to do this?


What is stored in 'name'?

Note that name has only room for a size-2 string.
That is: one lettter plus the terminating '\0'

Other then that:
Your use of eof is wrong, but that does not explain the
magical change of 'i'.


IMHO that magical change of i could only be achieved by aliasing or
completely broken optimization of the compiler.


Well. My personal guess is, that his file format looks like this

AB 1 2 2.0
CD 3 4 5.0

since the first column in his file contains 2 characters and
the variable receiving that string is defined as

char name[2];

the input operation is overflowing the array. And since
i is immediatly defined before that array, the overflow
happens such that the terminating '\0' is 'resetting' i in
each input operation. For this the compiler might have
arranged things on the stack such that i has a higher
address on the CPU stack but is immediatly following
name in memory. A lot of compilers would do it that way.

But this is just my personal guess and of course is
completely implementation dependent. Nevertheless I
think it is a good theory and to verify it, the exact
file format would be needed.

--
Karl Heinz Buchegger
kb******@gascad.at
Dec 14 '05 #5

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:43***************@gascad.at...
[SNIP]
But this is just my personal guess and of course is
completely implementation dependent. Nevertheless I
think it is a good theory and to verify it, the exact
file format would be needed.


Yes, that could easily be. I recently saw an implementation that internally
padded an extra byte for character arrays in order to be on the safe side
for such behavior. But as you said, this is completely implementation
dependent.

Chris
Dec 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.