Solved

read a line from a text file

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

786 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