473,387 Members | 1,721 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,387 software developers and data experts.

Why is first line of file not read in properly?

19
I am using this function to get data from a file

Expand|Select|Wrap|Line Numbers
  1. void get()
  2. {
  3.        string line;
  4.        ifstream myfile;
  5.        myfile.open ("jty.txt");
  6.  
  7.    while (getline(myfile, line))
  8.    {  
  9.           myfile >> one[i].a >> one[i].b >> one[i].c;
  10.          i++;
  11.    }
  12.  
  13.  
  14.    myfile.close();
  15. }
where 'one' is a struct with three int variables a, b, c.

The file is of the form

1 12 14
2 1 1
3 1 1
4 13 1
6 1 12
6 1 04


When I output this data, the output is of the form


2 1 1
3 1 1
4 13 1
6 1 12
6 1 04


That is the first line is not read properly, but the other lines come out fine. Could anyone please let me know the problem?
Feb 8 '11 #1

✓ answered by Oralloy

Perhaps because your loop is top driven using getline?

That means that the first pass through, the first line of your file is consumed before the value extractions (line 9) are done.

Cheers!
Oralloy

7 5340
Rabbit
12,516 Expert Mod 8TB
It probably has to do with your indexing. What's i before you start to copy the data into the array? Where's the code where you output the data?
Feb 8 '11 #2
Oralloy
988 Expert 512MB
When you run your output loop, are you starting from index 1 or index 0?

Cheers!
Oralloy
Feb 8 '11 #3
kino
19
Thanks for the replies!

There seems to be nothing wrong with the indexing since I initialized i as zero before copying the data with the code above. And in my output code I am just using a for loop going from 0 to maximum. The problem occurs somewhere in the copying.
Feb 8 '11 #4
Rabbit
12,516 Expert Mod 8TB
In the loop, print the variable line before the copy and then print the variable one after the copy and see what you get. Also print i while you're at it just to be sure.
Feb 8 '11 #5
kino
19
Yep, I tried that, but it just copies the second row instead of the first. However, I just replaced getline(myfile, line)) with ( !myfile.eof() ) in the while loop and this gives the right output. Is there anything wrong in the way I use getline then?
Feb 8 '11 #6
Oralloy
988 Expert 512MB
Perhaps because your loop is top driven using getline?

That means that the first pass through, the first line of your file is consumed before the value extractions (line 9) are done.

Cheers!
Oralloy
Feb 8 '11 #7
kino
19
That must be it. I tried 'do while' and now it works perfectly. Thanks a lot! :)
And thanks once again to both of you for all the replies.
Feb 8 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jaap de Bergen | last post by:
Hello people, I'm optimizing my script because my server is under heavy load in the evening. I have a question which methode is more efficient for my server: ==================...
2
by: Boris Boutillier | last post by:
HI all, I came across a strange behaviour with read method, and I'm not sure if this is a filesystem problem or a misuse of this method. I do the simple following thing : f = open...
17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
3
by: Tony Murphy | last post by:
Strange problem, I read a file into a string using ifstream, ostringstream and string and end part of the file is missing (file size ~9.5kb, ~9k read). its a html file. using windows nt 4 ...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
6
by: r.z. | last post by:
This piece of code crashes my app: basic_ifstream<TCHARfile(levelfilepath, ios_base::binary); unsigned int name_length; file.read( (TCHAR*)&name_length, sizeof(unsigned int) ); //crashes here -...
2
by: js | last post by:
Hi list. I'm writing a tail -f like program in python and I found file.read() doesn't work as I think it should. Here's the code illustrating my problem. ### #!/usr/bin/env python import...
2
by: Prateek | last post by:
I have a wierd sort of problem. I'm writing a bunch of sets to a file (each element is a fixed length string). I was originally using the built-in sets type but due to a processing issue, I had...
2
by: frank knuckles | last post by:
After exporting an Access table to Excel, I would like to make the Excel file Read-Only. Is it possible to do this with VBA from within Access? Thanks. FK
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?
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
Oralloy
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,...
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...

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.