473,508 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to read a file line by line?

Should i use the function getline?
the length of a line is unknown.
how to use it?
can someone show me a example?

Oct 17 '05 #1
6 17042
jtan...@hotmail.com wrote:
Should i use the function getline?
the length of a line is unknown.
how to use it?
can someone show me a example?


Yes, use getline, but use it with a std::string:

#include <string>
#include <fstream>
using namespace std;

int main()
{
ifstream file( "file.txt" );
// Do error checking here

string str;
while( getline( file, str ) )
{
// ...
}
return 0;
}

Cheers! --M

Oct 17 '05 #2
mlimber wrote:
jtan...@hotmail.com wrote:
Should i use the function getline?
the length of a line is unknown.
how to use it?
can someone show me a example?

Yes, use getline, but use it with a std::string:

#include <string>
#include <fstream>
using namespace std;

int main()
{
ifstream file( "file.txt" );
// Do error checking here

string str;
while( getline( file, str ) )
{
// ...
}
return 0;
}

does it work even if a line contains a space ?
Cheers! --M

Oct 17 '05 #3
pi***@tin.it wrote:
mlimber wrote:
jtan...@hotmail.com wrote:
Should i use the function getline?
the length of a line is unknown.
how to use it?
can someone show me a example?

Yes, use getline, but use it with a std::string:


[snip code example]
does it work even if a line contains a space ?


Yes. See here: http://www.cppreference.com/cppstring/getline.html

Kristo

Oct 17 '05 #4
When I compile this program, an error happened:
d:\c++work\gltest.cpp(20) : fatal error C1010: unexpected end of file
while looking for precompiled header directive

what does this error mean?
can some one give me help? Thank u!

Oct 18 '05 #5
jt*****@hotmail.com wrote:
When I compile this program, an error happened:
d:\c++work\gltest.cpp(20) : fatal error C1010: unexpected end of file
while looking for precompiled header directive

what does this error mean?
can some one give me help? Thank u!


It means you told the compiler that you were using a precompiled header,
but then you didn't include one in your source file.

If you don't know what a precompiled header is then I would find the
right way to turn that feature off in your compiler. Compiler specific
questions are off topic here you should ask in a group appropriate to
your compiler if you can't figure out how to do this.

john
Oct 18 '05 #6
<jt*****@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
When I compile this program, an error happened:
d:\c++work\gltest.cpp(20) : fatal error C1010: unexpected end of file
while looking for precompiled header directive

what does this error mean?
can some one give me help? Thank u!


It means you are using a Microsoft compiler and have precompiled headers
turned on. I always turn them off myself. This is OS (compiler) even
specific but I'll answer for Microsoft Visual C++ .net 2003: Project -> ...
Properties -> C/C++ -> Precompiled Headers -> Create/Use Precompiled Header
Change that to Not Using Precompiled Header

Oct 19 '05 #7

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

Similar topics

40
61166
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
3
3587
by: juli jul | last post by:
Hello, I have a text file and I have to check if the line I read contains some word,the problem is how to read it in a loop and check because if I am doing something like this: ...
1
3643
by: kaiser | last post by:
Hello. I am trying to write a program that continues to read the last line of a txt file until a stop button is clicked. while (ContinueLoop == true ) { while ((LastLine = file.ReadLine()) !=...
35
11335
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
6
3539
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
11
11351
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb ...
3
1218
by: George | last post by:
There is a file name file.txt with content below: 11 21 31 41 41 31 21 11 31 21 11 41 How can I read the file line by line, and then print the first three...
6
1991
by: karodegaurav | last post by:
hi, This is gaurav karode. i want a little code help.I am making an application in which i have to show a msg box and the content of msgbox will be from a text file. so basiclly i have to read...
2
2834
by: deenar | last post by:
Hi I'm having problems with Java in reading the next line in a text file to execute to find a solution. My program is read the data from the text file and excute it and provide the answer on the...
0
1819
by: leeamiin | last post by:
Hi, i need help with bellow file format, i work for telecom company and i'm the developer, what i need help with is to read file with bellow format, as you can see the file has { and , as...
0
7114
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
7321
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
7377
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
7034
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
5623
agi2029
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,...
0
4702
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...
0
3191
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
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
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 ...

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.