tags:

views:

116

answers:

3

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
Is there a reason to dynamically allocate anything here?
GMan
Not really i guess...i can fix that
Craig
i would usually make them dynamic because i wouldn't put them all in one function likfe
Craig
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
^Yes don't forget that :D.
Craig
There's also no reason to declare and construct outputBufferHere so early.
On Freund
Also, outputbuffer should be type `ofstream` not `ifstream`.
Potatoswatter
And this only works for files that are at most 1K. Anything larger and the data will be truncated.
R Samuel Klatchko
@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
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
+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
Or should could just alter buffer and write it back out.
Brendan Long
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