473,441 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

string getline trouble

I'm trying to use fstreams to format/encode a text file in a way that is useful for another program of mine, and I came upon a little snag...
The input file is formated as such:

int (line count)
string (line)
string (line)
string (line)
etc...

obviously, I start by reading in the integer and then proceed to use getline to try and get the strings, but the problem that I'm having is that getline won't pull the strings, and thus I'm not able to format them:

Expand|Select|Wrap|Line Numbers
  1.     int line_count(0);
  2.     string s;
  3.  
  4.     int word_count(0);
  5.  
  6.     in >> line_count;
  7.     out << line_count;
  8.  
  9.     for(int i=0; i<line_count; ++i)
  10.     {
  11.                                ...
  12.         getline(in, s);
  13.                                ...
  14.                 }
  15.  
in and out are the ifstream and ofstream, respectively. The string seems to come in as an empty string. Both outputting it and attempting to format it reveal this. It's quite likely that I'm missing/forgetting something stupidly simple, but it's been awhile since I've written anything with straight c++ code. Any insight would be appreciated.
Aug 24 '07 #1
7 2669
sicarie
4,677 Expert Mod 4TB
I believe the '>>' operator leaves a '\n' char - did you try flushing the buffer? (An easy test would be to read it in twice, see if it came through the second time...)

Edit:: I think there is an "ignore()" function that might also be easier than reading it in twice - though I wouldn't trust it for a prod program - in case there is more than one char in the buffer...
Aug 24 '07 #2
I couldn't remember how to flush the buffer, but that does seem to be what's going on here. See, I knew that it was something stupid... Thanks
Aug 24 '07 #3
sicarie
4,677 Expert Mod 4TB
I couldn't remember how to flush the buffer, but that does seem to be what's going on here. See, I knew that it was something stupid... Thanks
It's not stupid at all, just something that you learn as you encounter it, which depend on how you use different inputs in your programs.

Anyway, I like cplusplus.com as a reference - that's the fflush page on a search for stdin.
Aug 24 '07 #4
ilikepython
844 Expert 512MB
I'm trying to use fstreams to format/encode a text file in a way that is useful for another program of mine, and I came upon a little snag...
The input file is formated as such:

int (line count)
string (line)
string (line)
string (line)
etc...

obviously, I start by reading in the integer and then proceed to use getline to try and get the strings, but the problem that I'm having is that getline won't pull the strings, and thus I'm not able to format them:

Expand|Select|Wrap|Line Numbers
  1.     int line_count(0);
  2.     string s;
  3.  
  4.     int word_count(0);
  5.  
  6.     in >> line_count;
  7.     out << line_count;
  8.  
  9.     for(int i=0; i<line_count; ++i)
  10.     {
  11.                                ...
  12.         getline(in, s);
  13.                                ...
  14.                 }
  15.  
in and out are the ifstream and ofstream, respectively. The string seems to come in as an empty string. Both outputting it and attempting to format it reveal this. It's quite likely that I'm missing/forgetting something stupidly simple, but it's been awhile since I've written anything with straight c++ code. Any insight would be appreciated.
I'm not sure what the problem is, but you don't need a line number:
Expand|Select|Wrap|Line Numbers
  1. while (!words.eof())
  2. {
  3.     getline(in, s);
  4.     ... do whatever with s ...
  5. }
  6.  
Edit: Wow, 3 posts in front of me, oops.
Aug 24 '07 #5
It's not stupid at all, just something that you learn as you encounter it, which depend on how you use different inputs in your programs.

Anyway, I like cplusplus.com as a reference - that's the fflush page on a search for stdin.
I'll go check that out. Thanks again for your help
Aug 24 '07 #6
sicarie
4,677 Expert Mod 4TB
No problem - I'm not even sure if it's right, it's just a common issue. Hopefully it works, if not, post again and we'll figure it out!
Aug 24 '07 #7
No problem - I'm not even sure if it's right, it's just a common issue. Hopefully it works, if not, post again and we'll figure it out!
That's definitely what the problem was. While a second getline crashes my program, it looks like that's due to errors in my formatting functions, which I couldn't test properly before. The second getline is definitely getting the input string now, so I should be good to go (at least after I debug my formatting, which should be simple enough).
Aug 24 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: David Sobey | last post by:
Hi Sorry bout this basic prob. Got a file called file.obj. tryna read the first line from it as a string and print it to the screen. getting errors: #include "stdafx.h" #include <stdio.h>...
2
by: Roberto Dias | last post by:
Hi all, What to do for searching for more than one string occurrence in the same string (this last are line of a text). I have used getline(), to get the text lines by means of WHILE loop and...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
22
by: ineedyourluvin1 | last post by:
Hello all! I've been looking for a way to strip characters from strings such as a comma. This would be great for using a comma as a delimiter. I show you what I have right now. ...
22
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
14
by: cpisz | last post by:
I want to do some find and erase operations that i find in the string class, upon the entire text contents of a file. I made a function that will take a string (designed to hold the entire file...
3
blackstormdragon
by: blackstormdragon | last post by:
My trouble causing lines are maked within the code. #include<iostream> #include<string> using namespace std; void main() { string* head; string* tail;
2
by: John Brawley | last post by:
Please what is the functional C++ difference between a string of characters typed manually and included inside the main() function of a program... vis: string str = "123,4.56,7.8"; , ....and...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.