473,378 Members | 1,609 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,378 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 2396
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) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.