help with file output (fstream)....

M

mark

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();
 
S

Sumit Rajan

mark said:
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.
 
S

SaltPeter

mark said:
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;
}
 
J

John Harrison

mark said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,473
Latest member
pioneertraining

Latest Threads

Top