Solved

read a line from a text file

Posted on 2002-07-28
4
275 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 66 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now