Solved

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

Posted on 1997-06-25
3
219 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 50 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 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