?
Solved

read a line from a text file

Posted on 2002-07-28
4
Medium Priority
?
312 Views
Last Modified: 2012-08-13
I want to read a line from a text file - without having to worry about the possible maximum line legth.

Since ifstream's getline() has the format istream& getline (char* s, streamsize n );  it doesn't really seem to have advanced any from fgtes() - I still need to know & reserve sapce for the possible longest line.

As I am using Borland's C++ Builder, I would like to get the line & stick it straight into an AnsiString, which will worry about space for me.

Any other, non-BC, solutions also welcome (preferably not involving reading char by char)
0
Comment
Question by:A_student
4 Comments
 
LVL 3

Accepted Solution

by:
elcapitan earned 264 total points
ID: 7183363
use std::getline()
included in <string>

e.g.

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

ifstream infile;
string strLine;

infile.open("myfile.txt");
getline(infile,strLine);

infile.close()



--EC--


0
 
LVL 2

Expert Comment

by:christophm
ID: 7184188
- Know max line len in (txt) file without going through the file.
- Dynamically allocate space for the longest line before reading the line.

I don't know how you are going to pull that off.  How about get the filesize and allocate enough space for the whole file, then even if the entire file is just one (txt) line it would fit in the buffer.  I think I've gone up to a couple of meg, maybe 10Meg, with this technique.

get the filesize
allocate 'filesize' space
binary read the entire file into the buffer

good luck chris-m
0
 
LVL 30

Expert Comment

by:Axter
ID: 7185665
christophm,
>>I don't know how you are going to pull that off.

elcapitan has already posted a method that does exactly that.  The global std::getline function.
0
 

Author Comment

by:A_student
ID: 7187534
I didn't have time to try it yet, but I have no doubt that it works. Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question