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(OpenDi alog1->FileName.c_str ());
i=1;
// while (!fin.eof())
for(;;)
{
fin>>name>>node 1>>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>>node 1>>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 5 7670
On 13 Dec 2005 17:23:21 -0800 in comp.lang.c++, an********@gmai l.com
wrote, int node1, node2, i;
int i = 0;
// while (!fin.eof()) for(;;) {
fin>>name>>node 1>>node2>>value ;
Avoid looping on !eof(). Make that
while (fin>>name>>nod e1>>node2>>valu e) { ...
however the line ' fin>>name>>node 1>>node2>>value ;' keeps setting i to 0
I don't think so. However, i appears to be uninitialized. an********@gmai l.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>>node 1>>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
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:43******** *******@gascad. at... an********@gmai l.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>>node 1>>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
Chris Theis wrote: "Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message news:43******** *******@gascad. at... an********@gmai l.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>>node 1>>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
"Karl Heinz Buchegger" <kb******@gasca d.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sam Lowry |
last post by:
Greetings. I am trying to do something which should elementary for
Perl, but I have only been able to find bits and pieces on it. When I
put the bits together they do not work. Maybe I am going in the wrong
direction.
I want to count several strings of text in a file (security.txt) and
output the counts with a brief description of each. ...
|
by: Alex Vinokur |
last post by:
Is there any tool to count C-program lines except comments?
Thanks,
=====================================
Alex Vinokur
mailto:alexvn@connect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
|
by: Anders K. Jacobsen [DK] |
last post by:
Hi
We have a rather large asp.net project with serveral utility projects
(written in C#). Is there at tool out there which can give an estimate of
the total amount of code lines all projects results in?
thanks in regads
Anders
|
by: j |
last post by:
Hi,
I've been trying to do line/character counts on documents that are
being uploaded. As well as the "counting" I also have to remove
certain sections from the file.
So, firstly I was working with uploaded MS WORD .doc files. Using code
like that below:
strLine = sr.ReadLine
While Not IsNothing(strLine) 'Not eof
If Trim(strLine) <>...
|
by: Peter |
last post by:
Currently I'm using the method below, is there someting more efficient?:
Imports System.IO
Public Class CountLine
Public Shared Function CountLines(ByVal FileName As String) As Integer
Dim fs As IO.FileStream
Dim sr As IO.StreamReader
Dim Result As Integer
Try
fs = New IO.FileStream( _
| |
by: Mark..... |
last post by:
Hi,
Can someone tell me the easiest way to count the number of lines in a
text file?
I can write a loop to do this but it seems cumbersome.... there must be
an easier way??
Thanks in advance
|
by: peraklo |
last post by:
Hello,
there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a...
|
by: tiredMike |
last post by:
Hi,
I want to count lines in a text file in the format below:
255.000000,148.000000,0.000000,0.000000
255.000000,229.000000,0.000000,0.000000
255.000000,215.000000,0.000000,0.000000
37.000000,0.000000,0.000000,0.000000
71.000000,0.000000,0.000000,50.000000
|
by: tolkien |
last post by:
Hi,i have this problem.I'm reading strings from a text file and i want
to know in which line of the file i am.
What i did is this:
char string;
while(!feof(text_file)){
fscanf(text_file,"%s",string);
for(i=0;string!=' \0 ';i++)
if(string==' \n ' )
lineCounter++;
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |