473,246 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 2390
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sugapablo | last post by:
file() works great, until you start looking at file that are very large, say 134MB. Is there any function that will return an array for each line of a file, but only for lines given as...
0
by: Al | last post by:
Small grid applet is getting better with it's size 50Kb. http://24.60.235.213/saratable/index.html http://24.60.235.213/saratable/example5.html -Al
2
by: Sanyi Benczik | last post by:
Is there a way to obtain the name of the file associated with a fstream object? For example if I have using namespace std; ofstream file("test.txt"); is there a function which evaluates to...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
1
by: john | last post by:
I have an exe console app that i wrote, which works on custom data files. I'd like to give these data files a custom extension and somehow register that file extension with my exe, so all i have to...
7
by: lovecreatesbea... | last post by:
K&R 2, sec. 5.11 says that no need to precede function and array names with address-of operators &, why?
13
by: thomas.mertes | last post by:
Hello Recently I discovered some problem. I have some C code which determines how many bytes are available in a file. Later I use this information to malloc a buffer of the correct size before...
0
by: Suganya | last post by:
The young golfer from Wales believes "Discipline is definitely learned and gets better with practice." Doctors and scientists said that breaking the four-minute mile was impossible, that one...
18
by: mamul | last post by:
Hi, Please help me. I want to create a function taking file as an argument and open that file in the browser. wfstream file_stream; void function(string filepath) {
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.