🔒 Locked
fstream question
Advertisement
Original Post
I am having trouble getting the following sample code to output in the desired format. I want it to copy from one file to another without using the member function 'get()'
SAMPLE INPUT
Bob Dillon 73 43 43 64 85
CODE
char next;
while ( fin >> next )
{
fout << next << " ";
}
OUTPUT will be
B o b D i l l o n 7 3 4 3 4 3 6 4 8 5
I was under the understanding that when using 'fin >> next' or anything related to this style; all spaces and line breaks are ignored and that the only way to copy spaces and linebreaks is to use the member function 'get()' But it appears I am wrong.
So how do I achieve what I want? Which is to take input from one file using a different method other than 'get()' and outputting it to an output file formatted exactly as it is seen in the input file.
nick
[edited by - scarface-cd on April 21, 2002 3:25:36 PM]
I'm not 100% clear on what you are asking. Do you want the output to include or exclude the whitespace from the input file? The following code will make an exact copy of the file contents in input.txt and write it to output.txt:
If you want to ignore whitespace, then use an istream_iterator rather than an istreambuf_iterator.
Edit: fscking source boxes.
[C++ FAQ Lite | ACCU | Boost | Stroustrup on Learning C++]
[edited by - SabreMan on April 21, 2002 3:30:59 PM]
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
ifstream is("input.txt");
ofstream os("output.txt");
copy(istreambuf_iterator<char>(is),
istreambuf_iterator<char>(),
ostream_iterator<char>(os));
}
If you want to ignore whitespace, then use an istream_iterator rather than an istreambuf_iterator.
Edit: fscking source boxes.
[C++ FAQ Lite | ACCU | Boost | Stroustrup on Learning C++]
[edited by - SabreMan on April 21, 2002 3:30:59 PM]
I would like to copy the input file verbatim.
thank you for your response sabreman.
Your code will work, however I would like to see it done using the library
thank you for your response sabreman.
Your code will work, however I would like to see it done using the library
quote:
Original post by scarface-cD
Your code will work, however I would like to see it done using the library
Using which library?
[C++ FAQ Lite | ACCU | Boost | Stroustrup on Learning C++]
sorry 
fstream
fstream
I want to avoid get() because when I process the input file, I am discarding the first two elements. (i.e. the first and last name) the remaining elements (int) I then have to average (double).
quote:
Original post by scarface-cD
Your code will work, however I would like to see it done using the library <fstream>
His code uses the library. istream_iterator and ostream_iterator are generic iterator types for stream I/O (in this case instatiated directly from the file streams is and os), and copy is a generic algorithm that takes the supplied iterators and replicates sequence data in the output iterator.
You may not consider it "using the library", but it''s actually the most efficient form of using the library in terms of efficiency per lines of code.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
How about the following snippet ?
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
ifstream ifs( "whatever" );
stringstream ss;
string data;
ss << ifs.rdbuf();
// read the whole file into ss
data = ss.str();
// strings are reference counted, this is fast
[source]
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
quote:
strings are reference counted, this is fast
Mine aren''t (you''ll find that most, if not all, multithreaded string libraries are not reference-counted). The standard doesn''t require any such thing.
#include <fstream>
int main(void)
{
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
// the default buffer size for many implementations is small
// this will tend to speed up such implementations significantly
// testing will reveal the best value to use
const streamsize bufferSize = 4096;
char inBuffer[bufferSize];
char outBuffer[bufferSize];
fin.rdbuf()->pubsetbuf(inbuffer, bufferSize);
fout.rdbuf()->pubsetbuf(outbuffer, bufferSize);
fout << fin.rdbuf();
}
Fewest lines of code?
That should work. Didn''t appear to do so in my short test on MSVC6.0, but that doesn''t say very much.
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
ifstream is("input.txt");
ofstream os("output.txt");
os << is.rdbuf();
That should work. Didn''t appear to do so in my short test on MSVC6.0, but that doesn''t say very much.
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
thank you everyone I appreciate all of your help, i will be applying a few of the suggestions.
Is there any way of knowing the optimal buffer size? Short of experimentation, of course. Technically the disk access should be buffered at both ends anyway, so I assume the stream buffer size only really helps with regards to the library''s implementational methods...
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
quote:
Original post by Kylotan
Is there any way of knowing the optimal buffer size? Short of experimentation, of course.
And this experimentation should be carried out once there is a performance problem identified, not before. There''s no point obfuscating intent without a good reason.
[C++ FAQ Lite | ACCU | Boost | Stroustrup on Learning C++]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement