473,669 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with file output (fstream)....

The problem with this code snippet is that only outputs the first word
in the file "test". The file has atleast one line with 4 words in it,
only one word is output. I have tried increasing the array size as
well, still only the first word is output ?
what is the error ?

file "test" contains "Line one here"
output ---> "line"
char str[20];
ifstream b_file("test");
b_file>>str;
cout<<str;
b_file.close();
Jul 22 '05 #1
3 1493

"mark" <ma*********@ya hoo.com> wrote in message
news:5e******** *************** ***@posting.goo gle.com...
The problem with this code snippet is that only outputs the first word
in the file "test". The file has atleast one line with 4 words in it,
only one word is output. I have tried increasing the array size as
well, still only the first word is output ?
what is the error ?

file "test" contains "Line one here"
output ---> "line"
char str[20];
ifstream b_file("test");
b_file>>str;
cout<<str;
b_file.close();


That is what it is supposed to do. To handle it line-by-line, look up
std::getline(). For instance, the following would open a file and list it,
line-by-line:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::ifstream is("somefile.tx t");
std::string str;

while (std::getline(i s, str)) {
std::cout << str << '\n';
}
}

Regards,
Sumit.
Jul 22 '05 #2

"mark" <ma*********@ya hoo.com> wrote in message
news:5e******** *************** ***@posting.goo gle.com...
The problem with this code snippet is that only outputs the first word
in the file "test". The file has atleast one line with 4 words in it,
only one word is output. I have tried increasing the array size as
well, still only the first word is output ?
what is the error ?

file "test" contains "Line one here"
output ---> "line"
char str[20];
ifstream b_file("test");
b_file>>str;
cout<<str;
b_file.close();


Post code that includes main() and shows what your include directives are at
the very least. Your ifstream is correctly reading the first "word" up to
the first space encountered in the stream. Try using std::getline instead:

#include <iostream>
#include <string>
#include <fstream>

int main()
{
std::ifstream ifs;
ifs.open("test" , std::ios::in);
if(!ifs)
{
std::cout << "can't open input file stream\n";
return -1;
}

std::string s;
std::getline(if s, s);

std::cout << s << std::endl;

ifs.close();

return 0;
}
Jul 22 '05 #3

"mark" <ma*********@ya hoo.com> wrote in message
news:5e******** *************** ***@posting.goo gle.com...
The problem with this code snippet is that only outputs the first word
in the file "test". The file has atleast one line with 4 words in it,
only one word is output.
That's how it should work.

I have tried increasing the array size as well, still only the first word is output ?
Arrays are passed to functions as pointers, so there is no way that any
function can know the size of an array passed to it. So increasing the size
of the array could not possibly make any difference.

This is an important point. You have declared a char array, but there is no
way at all using >> to stop the input if the char array isn't big enough.
Never, ever use >> on a char array. Most of the security flaws in Windows
are caused by this and similar errors. Ever heard of a buffer overrun? Well,
you just wrote one.

Fortunately C++ has a good solution, instead of using char arrays use a
string instead. When you input to a string it grows to the size that is
needed.

#include <string>

std::string word;
cin >> word; // no overrun possible
what is the error ?


RTFM I think. You seem to think that >> should read a line, but it doesn't,
it reads a word. Use getline instead.

john
Jul 22 '05 #4

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

Similar topics

4
354
by: Revman | last post by:
I'm having problems opening and reading from a file to test a Class. My diver main.cpp is fairly short but with a longer file open function // Project #4 -- Main/driver program #include "daytime.h" #include <string> #include <fstream>
5
1885
by: Danny Anderson | last post by:
Hola- I didn't get any responses on a previous post, so I am trying to reword my problem and post compile-able code that exhibits the behavior I am describing. On the second iteration of the loop below, the file opened is the default (which in this case is ".csv", which makes a 'hidden' file for Linux folk). The step that prompts for a file name step is skipped completely. Why? Is this a cin issue or does it have something to do with...
1
2172
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include <string>
2
2749
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include <string>
11
1639
by: Kush | last post by:
Hello All, I am having problem writing some data to file and I do not understand my mistake / misconception. I am trying to write some simple text to file but the text is rather output to screen! Below is my entire code from my problem, but I just want to focus on the method "write_percentage_per_side_to_file(ofstream &os)". From my understanding I am passing an ofstream object which I declared in main(
2
4751
by: jjcp | last post by:
I would like to say thanks in advance for insight anyone can shed on this for me. Long story short from time to time I need to us C++ to take a list of file and make an index out of them in html. for this I use fstream both for input and output. So, i have a simple file that I just change few things on and I get the output formatted to what I want. So I installed FC6 on my home system and get out my handy usb drive because i needed...
5
756
by: Dic4000 | last post by:
ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË? Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷. #include<iostream> #include<conio.h> #include<fstream> using namespace std;
5
18767
by: sheriff | last post by:
Dear friends, im a newbee for this forum and c++ im doing my MSc in Simulation Tech in mech. Engineering. My knowledge of c++ is very little which I had during my UG studies Long long ago .I am now forced to do some programming as a small part of my thesis work. Here goes my task and question. I want to read the text file and jus find the displacement old value and replace them with new value and write them in another file.........(In my...
5
1304
intOwnsVoid
by: intOwnsVoid | last post by:
I'm making a program that is supposed to store circles in a file, each circle should have an id and a radius. after i Add some circles to the file i need to open it again and search for a specific Id to display it. Here's what I've done so far. Please any help would be appreciated. Circle.h #ifndef CIRCLE_H #define CIRCLE_H
1
1876
intOwnsVoid
by: intOwnsVoid | last post by:
This program compiles but it's not searching properly. Circle.h #ifndef CIRCLE_H #define CIRCLE_H #include <iostream> #include <fstream> using namespace std;
0
8465
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
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8895
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
8809
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...
1
8588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6210
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
4206
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...
1
2797
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.