I think I probably have to use an fstream object but i'm not sure how. Essentially I want to read in a file into a byte buffer, modify it, then rewrite these bytes to a file. So I just need to know how to do byte i/o. Thanks
+1
A:
#include <fstream>
std::ifstream fileBufferHere(inputFileName, std::ios::in|std::ios::binary);;
std::ofstream outputBufferHere(outputFilename, std::ios::binary|std::ios::out);
char *m_content = new char[1024];
if(fileBufferHere.is_open())
{
fileBufferHere.seekg(0, std::ios::beg);
fileBufferHere.getline(m_content, 1024);
}
/*MODIFY HERE*/
outputBufferHer.write(content, sizeof(content));
outputBufferHere.close();
fileBufferHer.close();
From memory i think this is how it goes...
Craig
2010-02-18 02:37:01
Is there a reason to dynamically allocate anything here?
GMan
2010-02-18 02:38:41
Not really i guess...i can fix that
Craig
2010-02-18 02:39:58
i would usually make them dynamic because i wouldn't put them all in one function likfe
Craig
2010-02-18 02:41:53
You still need to delete m_content after using new. C++ doesn't have garbage collection.Also 1024 bytes should fit on the stack quite easily so just use `char m_content[1024];`
Axel Gneiting
2010-02-18 02:42:27
^Yes don't forget that :D.
Craig
2010-02-18 02:43:35
There's also no reason to declare and construct outputBufferHere so early.
On Freund
2010-02-18 06:42:37
Also, outputbuffer should be type `ofstream` not `ifstream`.
Potatoswatter
2010-02-18 06:50:27
And this only works for files that are at most 1K. Anything larger and the data will be truncated.
R Samuel Klatchko
2010-02-18 06:56:49
@potatoswatter, yeah sorry didn't get much time to write it, was at work tryggn to hammer it out.@Freund, your just being pedantic, it is better to declare it at the beginning of the function i.m.o, because it looks messy declaring it half way through.@Klatchko, once again it is an example, i'm not doing his work for him. I'm sure the guy is clever enough to realise is it 1024 bytes reading in only
Craig
2010-02-19 00:23:39
A:
While doing file I/O, you will have to read the file in a loop checking for end of file and error conditions. You can use the above code like this
while (fileBufferHere.good()) {
filebufferHere.getline(m_content, 1024)
/* Do your work */
}
Tanuj
2010-02-18 06:35:45
+1
A:
If you are dealing with a small file size, I recommend that reading the whole file is easier. Then work with the buffer and write the whole block out again. These show you how to read the block - assuming you fill in the open input/output file from above reply
// open the file stream
.....
// use seek to find the length, the you can create a buffer of that size
input.seekg (0, ios::end);
int length = input.tellg();
input.seekg (0, ios::beg);
buffer = new char [length];
input.read (buffer,length);
// do something with the buffer here
............
// write it back out, assuming you now have allocated a new buffer
output.write(newBuffer, sizeof(newBuffer));
delete buffer;
delete newBuffer;
// close the file
..........
Fadrian Sudaman
2010-02-19 00:36:40
All depend on the modification type. Would be easier if it is just one to one replacement of one or more bytes in the stream. However, if the alteration add more bytes to it, it can cause memory overflow exception - which is a common pitfall.
Fadrian Sudaman
2010-02-19 00:49:31