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<cha r> 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::ostringstr eam 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::ostringstr eam osErr;
osErr << "Failure opening output file " << oss.str();
throw std::ios_base:: failure( osErr.str() );
}
++archive;
}
}
else
{
std::ostringstr eam 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? 3 2416
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_ifst ream::readsome( char_type *s, streamsize n);
TB
TB wrote: Look at:
streamsize std::basic_ifst ream::readsome( char_type *s, streamsize n);
TB
Thank you - don't know why I haven't come across that function before.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 parameters?
A function like file("/path/to/file",100,230) which would return lines 100
through 230 as an array.
|
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
|
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 "test.txt"?
|
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 = tmpfile();
/* write into tmpFile */
...
|
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 do is double click the data file and my exe will launch.
How do you register an exe with a file extension
Once this is done, and you double click the file with the file extension, how does the exe know which file was clicked? is the path...
| |
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?
|
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 I read the bytes.
Determining the number of bytes available in a
file is done in 5 steps:
1. Use tell(aFile) to get the current position.
|
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 would die in the attempt. Thus, when I got up
from the track after collapsing at the finish line, I figured I was
dead.
http://shenrilaa.googlepages.com/
|
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)
{
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |