471,585 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

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

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::ostringstream oss;
oss << outName << std::setfill('0') << std::setw( 2 ) <<
archive << ext;
std::ofstream ofs( oss.str().c_str() );
if ( ofs )
{
ofs.write( &buf[0], bytes );
}
else
{
std::ostringstream osErr;
osErr << "Failure opening output file " << oss.str();
throw std::ios_base::failure( osErr.str() );
}
++archive;
}
}
else
{
std::ostringstream 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?

Jan 13 '06 #1
3 2287
TB
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
Jan 13 '06 #2

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.

Jan 13 '06 #3

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

Jan 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sanyi Benczik | last post: by
4 posts views Thread by Oliver Knoll | last post: by
7 posts views Thread by lovecreatesbea... | last post: by
13 posts views Thread by thomas.mertes | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.