Solved

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

Posted on 1997-06-25
3
224 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
[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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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 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…
Suggested Courses

631 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