0

I needed some help writing to a file using fstream. I can currently write using the fwrite method. But I wanted to cast the data type using fwrite and write using it

Below is what I am going so far:

noOfRows = 700;
noOfCols = 100
array[0] = unsigned char [noOfRows * noOfCols]

fstream fstreamFile;
fstreamFile.open ("fstreamFile.txt", fstream::out);

for(int i = 0; i < noOfRows; i++ )
{
fwrite(array[0]+(i*noOfRows), sizeOf(unsigned char), noOfCols, fwriteFile); // writes ok
fstreamFile.write((char*) array[0]+(i*noOfCols), sizeof(unsigned char)); // doesn't write
}

Can someone please tell me what I am doing incorrectly? I get no compile error, the file just is not being written into (I have created it from before hand).

c0d3rz
  • 649
  • 3
  • 15
  • 23
  • Seems to be pretty much the same as http://stackoverflow.com/questions/11159842/c-writing-int-buffer/11160247#11160247 – David Stone Jun 23 '12 at 00:45

3 Answers3

2

2 things, are you looking in the correct place for the file? It should be wherever the working directory is. You should not need to create the file before hand, it will be created. Second, make sure it is creating the file, you can do this with:

if (!fstreamFile) { cout << "errorr.." << endl; }
Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106
  • Good catch Rocky, it was writing the file to my working directory. I feel quite silly not checking there. However, it seems to be filling it with non-readable characters. I know this has to do with casting it correctly before I write it to the file. The array is of type unsigned char ... but I get this error when I try to cast it to unsigned char: error C2664: 'std::basic_ostream<_Elem,_Traits>::write' : cannot convert parameter 1 from 'unsigned char *' to 'const char *' – c0d3rz Jun 22 '12 at 20:21
  • are you filling the array somewhere? All i see in the code above is where the array is declared, not where it is filled. Also, maybe what you want is array[0][i*noOfCols] – Rocky Pulley Jun 22 '12 at 20:23
  • Yes, I am filling in the array previously. I'm quite sure that I want to access it as array[0]+(i*noOfCols). Thanks for the suggestion though .. – c0d3rz Jun 22 '12 at 20:39
1

You would tend to use iostreams to avoid casting.

Given an array of unsigned chars, you could instead just use an algorithm and an iterator to write the file.

#include <fstream>
#include <algorithm>
#include <iterator>

// your main and array declaration here

ofstream fstreamFile("fstreamFile.txt");
copy(
    array,
    array + sizeof(array),
    ostream_iterator<unsigned char>(fstreamFile)
);
wreckgar23
  • 1,025
  • 9
  • 22
1

(char*) array[0]+(i*noOfCols)

This is taking the value of the array at position 0, then casting that value to a char * (giving some random gibberish pointer), then adding i * noOfCols to that. I believe this may be what you want:

(char*) (&array[0]+(i*noOfCols))

However, stylistically, I would recommend using C++ casts instead.

reinterpret_cast<char *>(& array[0] + i * noOfCols)

I would also recommend instead of sizeof(unsigned char) using sizeof(array[0]), just to be a bit more generic.

Also, it seems like you are trying to simulate a 2-dimensional array with a 1-dimensional array. It would be much more straightforward to declare the array as unsigned char array [noOfRows][noOfCols]; and then operate on the two dimensions normally. Your method is not getting you any performance.

David Stone
  • 26,872
  • 14
  • 68
  • 84
  • Hi David: Appreciate the reply. What you say makes sense, passing by reference. I realized my mistake and wished it would have worked. But it doesn't. Also, I've made a mistake casting it as (char *). I instead meant to cast it as (unsigned char*). That being said, here is my newer code: fstreamFile.write((char *)(&array[0]+(i*noOfCols)), sizeof(array[0])) ; The errors I get after are: error C2070: 'cli::array []': illegal sizeof operand C2728: 'cli::array' : a native array cannot contain this managed type error C2955: 'cli::array' : use of class template requires template argument list – c0d3rz Jun 23 '12 at 01:03
  • Can you please elaborate on your last comment please, "Your method is not getting you any performance." ? – c0d3rz Jun 23 '12 at 01:04
  • Sorry, I meant to write: fstreamFile.write((unsigned char *)(&array[0]+(i*noOfCols)), sizeof(array[0])); I also tried fstreamFile.write((reinterpret_cast)(&array[0]+(i*noOfCols)), sizeof(array[0])); and get the same error: error C2664: 'std::basic_ostream<_Elem,_Traits>::write' : cannot convert parameter 1 from 'unsigned char *' to 'const char *' – c0d3rz Jun 23 '12 at 01:10