473,569 Members | 2,490 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 17055
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\glte st.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\glte st.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*****@hotmai l.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
When I compile this program, an error happened:
d:\c++work\glte st.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
61173
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: while(((line=file.ReadLine())!=null)&&(line.IndexOf("abc")==-1)) I will read additional line which doesn't consit abc ,how to avoid this? Thanks a lot!
1
3649
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()) != null) { LastLine = file.ReadLine(); }//while readline
35
11385
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", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger...
6
3541
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 I'm avoiding the use of fscanf and fgets to read in lines. I don't want to have to specify a temporary char* buffer to read in each line, and then...
11
11355
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 fileinput don't load the file into memory first how do i use fileinput module to read a specific line from a file? for line in...
3
1221
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 columns(separed by tab)? Thanks for any idea.
6
1995
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 the first line of text file and show it into a msg box. i tried different things but unable to do that. ican read from the text file but how to show...
2
2847
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 screen.I've complied my program but can't get the program to read the next line in the text file. Please could someone help!!!Below is part of the...
0
1828
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 separation delimiter. each line of bellow data is a cell together are one row value CallEventRecord ::= moCallRecord : { recordType moCallRecord,...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, 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. ...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
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...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
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...
0
3656
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
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.