Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do you read a line from a file to a string?

Posted on 1997-06-25
3
Medium Priority
?
230 Views
Last Modified: 2011-04-14
I'm trying to read a file with a loop and put each line in a cstring. Here's my code:

string messageBuffer;
while(!messageFile.eof())
 { messageBuffer = "";
   getline(messageFile, messageBuffer);
   if(messageBuffer == "")
    { messageBuffer = "\n"; }
   sendToServer(s, messageBuffer, NO);
 }
This works fine for most lines, and doesn't save the \n to the cstring, just like it's supposed to, but when it gets to a blank line, where the \n is the only thing on the line, it totally skips it and goes on to the next line. If my file's like this:

Test 1

Test 2

-and I read it with this loop, it ends up being like:

Test 1
Test 2

-I tried using the cstring function:
messageBuffer.read_line(messageFile);
instead of:
getline(messageFile, messageBuffer);

-it gives the exact same results, which makes me think that the cstring class won't let it return just nothing, which what it should do if a \n is stripped off a line w/ just \n.

I tried this exact same loop using char messageBuffer[100] and char functions instead, and it works perfectly.

Also, tabs seem to be stripped out when they're saved to a string as well. Does anyone know what's wrong? Thanks.
0
Comment
Question by:DarkNova
3 Comments
 

Author Comment

by:DarkNova
ID: 1164128
Edited text of question
0
 
LVL 1

Expert Comment

by:strobert
ID: 1164129
I don't have the source code to read_line handy at the moment, but it looks like it is behaving like a stream >> operator to a string which eats whitespace.

use fscanf or a combo of fgets and sscanf to do formatted input reading.
0
 
LVL 1

Accepted Solution

by:
mann061997 earned 200 total points
ID: 1164130
getline is defined as:

template<class E, class Ti, class T, class A>
    basic_istream<E, Ti>& getline(
        basic_istream <E, Ti>& is,
        basic_string<E, T, A>& str,
        E delim = Ti::newline());

Since you only supplied the first 2 parameters the delimiter
is set to the default value newline. The delimiter is not
appended to the string when it is encountered.

So you could either
- test for messageBuffer.IsEmpty() && !messageFile.eof() and
  generate a \n in these cases
- use char functions as you already mentioned
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

916 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