Solved

read a line from a text file

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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.

706 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

18 Experts available now in Live!

Get 1:1 Help Now