Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 129
Safe conversion? 4 86
Can Microsoft Visual C++ 2005 Distributable be removed form my Exchange server? 3 67
COM server issue 2 26
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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…

738 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