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: -
int line_count(0);
-
string s;
-
-
int word_count(0);
-
-
in >> line_count;
-
out << line_count;
-
-
for(int i=0; i<line_count; ++i)
-
{
-
...
-
getline(in, s);
-
...
-
}
-
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.
7 2669
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...
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
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.
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: -
int line_count(0);
-
string s;
-
-
int word_count(0);
-
-
in >> line_count;
-
out << line_count;
-
-
for(int i=0; i<line_count; ++i)
-
{
-
...
-
getline(in, s);
-
...
-
}
-
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: -
while (!words.eof())
-
{
-
getline(in, s);
-
... do whatever with s ...
-
}
-
Edit: Wow, 3 posts in front of me, oops.
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
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!
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).
Sign in to post your reply or Sign up for a free account.
Similar topics
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>...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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;
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |