Skip to main content
gamedev.net
🔒 Locked

fstream question

Started by scarface-cD Apr 21, 2002 at 2:13 PM 13 replies 850+ views
Original Post
scarface-cD
scarface-cD
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]
SabreMan
SabreMan
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:


    
#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]
scarface-cD
scarface-cD
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
alexk7
alexk7
Why do you want to avoid get() ?
alexk7
scarface-cD
scarface-cD
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).
Oluseyi
Oluseyi
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!
Fruny
Fruny
How about the following snippet ?

  
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...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
DrPizza
DrPizza
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();
}

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
DrPizza
DrPizza
Works fine in VS.NET, but, like I said, is fairly slow. Enlarging the buffers helps.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
scarface-cD
scarface-cD
thank you everyone I appreciate all of your help, i will be applying a few of the suggestions.
Kylotan
Kylotan
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 ]
SabreMan
SabreMan
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.