There are a lot of tutorials and stuff about how to write data to a .txt, but none of them really helped me.
I made a program that is supposed to convert integers to pixelcolors.
For that, I created an Array:
int Pixel[XMAX][YMAX];
XMAX and YMAX are the picture width and stand for the postion of the pixel in the picture.
They are defaulted to a value of 40, means that the color is saved as f.e.
Pixel[5][9]=6;
in this case, 6 represents the color value i want to save.
To save these pictures, i want to write the values to a .txt binary.
(I need those .txt's to actually convert values into a real picture.)
My problem is that I can't get the values into a .txt in a way that the program can read them out again one by one and save them to their postion in the integer "Pixel" after the program has been closed (the value is deleted from it).
Can anybody please give me an example for that code?
PeterK
P.S.: Sorry for bad grammar and vocab, I'm german ;-)
That's the problem, I guess.
I wasn't able to write any code regarding this, because I didn't know what to start off with as a command.
I only have code for all other things in my program...
Cool, thanks, but the part about binary files only explanes how to read them and put them into a memblock, right?
My problem is that I first need to output the integers, and then read them in one by one so they are memorized together with their coordinates.
For the input I tought of some kind of code like this:
for (X; X<XMAX; X++)
{
for (Y; Y<YMAX; Y++)
Pixel= (some integer the read value from the binary is stored in*);
}
*I don't quite know how to code this, too. :(
Hope you understand it, the code is probably filled with syntax mistakes (can't concentrate any more right now,I'm too tired ;-) )
But still this is only for input. I need to be able to output, first.
For the output, is it OK to use something like this?
(guess I didn't get the syntax right again)
...
int outputint; //the integer sent to the save file
ofstream savefile;
savefile.open("Save.txt");
for (X;X<XMAX;X++)
{
for (y;y<ymax;y++)
outputint=Pixel[x][y];
savefile<<outputint;
savefile<<endl;
}
savefile.close();
Can this code work so I can recover every single integer afterwards?
Anyway, thanks for your help :)
Operators << and >> are used with text files. If you want a binary file, use read() and write() methods. Not that there is anything wrong with text files..
To write elements of an array to the file, you'd write
1 2 3 4
ofstream fout ("file.sth", ios::bin);
for (int i = 0; i < XMAX; i++)
for (int j = 0; j < YMAX; j++)
fout.write ((char*)&Pixel[i][j], sizeof(int) );
But since arrays are contiguous in memory, you can just
Wait, what works? Now that I think about it, (char*)Pixel, shouldn't work. It should be (char*)Pixel[0] or (char*)&Pixel[0][0]. (char*) &Pixel[I][J] is good only if you already have I and J set to 0.
Well, everything works.
It saves the files and correctly loads them again.
There is a bug that sometimes duplicates the "picture",
but thats not connected to the save and load part in any way.