473,511 Members | 14,990 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 1484

"mark" <ma*********@yahoo.com> wrote in message
news:5e**************************@posting.google.c om...
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.txt");
std::string str;

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

Regards,
Sumit.
Jul 22 '05 #2

"mark" <ma*********@yahoo.com> wrote in message
news:5e**************************@posting.google.c om...
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(ifs, s);

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

ifs.close();

return 0;
}
Jul 22 '05 #3

"mark" <ma*********@yahoo.com> wrote in message
news:5e**************************@posting.google.c om...
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...
5
1878
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...
1
2146
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...
2
2735
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...
11
1619
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...
2
4743
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...
5
756
by: Dic4000 | last post by:
ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË? Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷. #include<iostream> #include<conio.h> #include<fstream> using namespace std;
5
18753
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...
5
1295
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...
1
1870
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
7237
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
7349
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,...
1
7074
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...
0
5659
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,...
1
5063
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...
0
4734
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...
0
3219
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...
0
1572
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 ...
0
445
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...

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.