Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > File-splitting Function: Uses FILE * - better way with ifstream?

Reply
Thread Tools

File-splitting Function: Uses FILE * - better way with ifstream?

 
 
Earl Purple
Guest
Posts: n/a
 
      01-13-2006
This is a simple function to split a file into multiple files
(archives) of a fixed size. The last one will contain any remaining
bytes.

Here is the implementation:

#include <cstdio>
#include <fstream>
#include <iomanip>
#include <vector>

void split( const std::string & inPath, const std::string& outName,
size_t bufsize,
const std::string & ext=".txt" )
{
std::vector<char> buf( bufsize, 0 );

std::FILE* fIn = std::fopen( inPath.c_str(), "r" ); // may
sometimes want "rb"
if ( fIn )
{
size_t archive=0;

while ( size_t bytes = std::fread( &buf[0], 1, bufsize, fIn ) )
{
std:stringstream oss;
oss << outName << std::setfill('0') << std::setw( 2 ) <<
archive << ext;
std:fstream ofs( oss.str().c_str() );
if ( ofs )
{
ofs.write( &buf[0], bytes );
}
else
{
std:stringstream osErr;
osErr << "Failure opening output file " << oss.str();
throw std::ios_base::failure( osErr.str() );
}
++archive;
}
}
else
{
std:stringstream osErr;
osErr << "Failure opening input file " << inPath;
throw std::ios_base::failure( osErr.str() );
}
}

It works -you can add a main() to try it. The downside is the use of
FILE *. I know there are some who will say that there is nothing wrong
with using FILE * but the purists among us would prefer to use
ifstream.

The problem is that the equivalent ifstream.read() function returns the
stream, not the number of bytes. And if it fails to read the specified
number, it will enter a fail state. Even then I won't know how many
bytes were read (Would the buffer still be written?)

Maybe there is a way to do it using filebuf instead.
Anybody got a way to replace the FILE * with streams without making the
code overly complicated?

 
Reply With Quote
 
 
 
 
TB
Guest
Posts: n/a
 
      01-13-2006
Earl Purple sade:
> This is a simple function to split a file into multiple files
> (archives) of a fixed size. The last one will contain any remaining
> bytes.
>
> Here is the implementation:

snip
>
> It works -you can add a main() to try it. The downside is the use of
> FILE *. I know there are some who will say that there is nothing wrong
> with using FILE * but the purists among us would prefer to use
> ifstream.
>
> The problem is that the equivalent ifstream.read() function returns the
> stream, not the number of bytes. And if it fails to read the specified
> number, it will enter a fail state. Even then I won't know how many
> bytes were read (Would the buffer still be written?)


Look at:

streamsize std::basic_ifstream::readsome(char_type *s, streamsize n);

TB
 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      01-13-2006

TB wrote:
>
> Look at:
>
> streamsize std::basic_ifstream::readsome(char_type *s, streamsize n);
>
> TB


Thank you - don't know why I haven't come across that function before.

 
Reply With Quote
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      01-13-2006

Earl Purple wrote:
[snip]
> The problem is that the equivalent ifstream.read() function returns the
> stream, not the number of bytes. And if it fails to read the specified
> number, it will enter a fail state. Even then I won't know how many
> bytes were read (Would the buffer still be written?)


Use ifstream.gcount() to determine how many bytes were read
with the last call to ifstream::read(...)

Stephan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 13 09-21-2011 12:17 AM
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 3 09-09-2011 10:36 PM
Re: Parsing Binary Structures; Is there a better way / What is your way? Paul Rubin Python 5 08-06-2009 08:06 AM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
What's the best way to hide the particular STL container that my class uses? Ed C++ 3 04-04-2005 01:17 PM



Advertisments