473,789 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stream problem

Hello,

I'm wondering why this outputs garbage:

std::ofstream outFile("test.t xt");

outFile << 1.0f << std::endl;
outFile << filename << std::endl;
outFile << 2.0f << std::endl;
std::string s;
float x;
std::ifstream inFile("test.tx t");
inFile >> x;
std::cout << x << std::endl;
std::getline(in File, s, '\n');
std::cout << s << std::endl;
inFile >> x;
std::cout << x << std::endl;

and why this works:

outFile << filename << std::endl;
outFile << 1.0f << std::endl;
outFile << 2.0f << std::endl;
std::string s;
float x;
std::ifstream inFile("test.tx t");
std::getline(in File, s, '\n');
std::cout << s << std::endl;
inFile >> x;
std::cout << x << std::endl;
inFile >> x;
std::cout << x << std::endl;

I think the former doesn't work because the getline() is reading the same
line as the previous inFile >> read. How can I get it to skip to the next
line, if that is indeed the problem.
Jul 19 '05 #1
2 2801

"vsgdp" <no****@nospam. com> wrote in message
news:LMr2b.1046 3$Qy4.4180@fed1 read05...
Hello,

I'm wondering why this outputs garbage:

std::ofstream outFile("test.t xt");

outFile << 1.0f << std::endl;
outFile << filename << std::endl;
outFile << 2.0f << std::endl;
std::string s;
float x;
std::ifstream inFile("test.tx t");
inFile >> x;
std::cout << x << std::endl;
std::getline(in File, s, '\n');
std::cout << s << std::endl;
inFile >> x;
std::cout << x << std::endl;

and why this works:

outFile << filename << std::endl;
outFile << 1.0f << std::endl;
outFile << 2.0f << std::endl;
std::string s;
float x;
std::ifstream inFile("test.tx t");
std::getline(in File, s, '\n');
std::cout << s << std::endl;
inFile >> x;
std::cout << x << std::endl;
inFile >> x;
std::cout << x << std::endl;

I think the former doesn't work because the getline() is reading the same
line as the previous inFile >> read.
Not quite.

getline is doing what it is supposed to which is reading up to the next
newline. Your mistake is thinking that inFile >> x will read a newline, but
it doesn't. inFile >> x reads a number and a newline is not part of a
number. So when you have a number followed by a newline the newline is left
behind unread. Then getline comes along and reads it.
How can I get it to skip to the next
line, if that is indeed the problem.


Use getline twice?

john
Jul 19 '05 #2

"John Harrison" <jo************ *@hotmail.com> wrote in message news:bi******** ****@ID-196037.news.uni-
How can I get it to skip to the next
line, if that is indeed the problem.


Use getline twice?

or ignore.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2917
by: Sandeep | last post by:
Hi all, I am new to the ADODB.Stream I am using following code lRecordset.Open "Select * from <some table-name>" 'this query return more than 1000 records dim lstream as new ADODB.stream 'assigning the recordset data to the stream lrecordset.save lstream lStream.Position = 0
6
7359
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents written to the output stream. We are willing to pay $ to any expert (e.g. MVP) consultant who has to time to help us track down this problem. (we will discuss rates if you can prove your expertise, and problem solving approach).
0
3122
by: mario.lat_ | last post by:
Hallo to all, I have write a little script for connecting to cisco router BUT I have a problem: I have to send to router all the commands and then I have to read the output. If I send a command1 and read the output for command1 the script works well, If I sent command1 , I send command2 and then I read output for command1 and command2 it works well. If i send command1 and then I read the output for command1 then send command2 I can't...
3
6774
by: Sir Psycho | last post by:
Hi, For some reason, when i step over this code, it returns the full byte stream im expecting from the server, however when I let it run with no intervention, it only seems to grab a small chunk on the stream. What am I doing wrong? There is more code than this, but this is the problem code.
2
1480
by: Mike P2 | last post by:
I made a Stream-inheriting class that just removes the tabs (actually the 4 spaces VS prefers to use) from the beginning of lines and empty lines. At first I was having trouble with it adding a null character in the middle of the output. I think it was a null character, Firefox displays it as a '?' and the W3C validator says it's a non-sgml character '0'. I changed the code around a bit and now the only problem is that it (on some pages of...
8
3757
by: T Driver | last post by:
Anyone have any idea how I can do the following? I have a connection to an XML file on a site I do not control, getting a string representation of the xml data that I can then feed to my XmlDataSource object (CurrentXMLData): Stream newStream = myWebClient.OpenRead(myStringWebResource); TextReader newReader = new StreamReader(newStream); string newData = newReader.ReadToEnd(); CurrentXMLData.Data = newData;
33
2062
by: john | last post by:
I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions returning bool are mentioned: template <class Ch, class Tr= char_traits<Ch class basic_ios: public ios_base { public: // ... bool good() const; // next operation might succeed bool eof() const; // end of input seen
0
2062
by: Iridium | last post by:
Greetings, I am trying to get a JPG Frame from a MJPG Stream. A MJPG is basically a stream of JPGs which are splitted by a special boundary string. So I tried to get the stream, split it by the boundary strings and save the JPG binary data into a file. The problem is, I cant save the data into a proper JPG file which I then open. This is my code: using System; using System.Collections.Generic; using System.Text; using System.IO; using...
2
1955
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
I've made a post last Friday in regards to this subject, but I'm still a little lost. I've a problem regarding reading a stream. I am connected to a port that sends information using a stream. I can read the information from the stream with no problem. However, the problem comes when there is nothing to read in the stream. The stream is still open, so it just hangs on my Read(). EXA:
27
3165
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream that's causing the problem. Here's how I wrote the program originally: #include <stdio.h> #include <string.h>
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.