Solved

read a line from a text file

Posted on 2002-07-28
4
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 67
fatal error: vector: No such file or directory   Visual Studio 2015 for Arduino 4 105
C++ Code Issue 4 38
visual C++ 1 15
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

730 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