Advertisement

Load files ? good, but....

Started by September 18, 2000 11:32 AM
17 comments, last by SKSlayer 24 years, 1 month ago
Last tutorial is cool cuz it explains how to load files The question is how to save files ?
    
char myName[]="SKSlayer"
int Number=10;
float Number2=12.256;
[/source]
Okay, I want my prog to output this (Of cource, which are the values above)
[source]
SKSlayer 12.256
10
    
in a text file (let''s say info.txt) How can I save a file ? An other question why load files with stdio.h and not the win32 functions ???
(you can find me on IRC : #opengl on undernet)
one word: portability =)
If you wanted to load in files like that, one way would be to add in java-script support to your program. Don''t ask me how, I just know it''s possible =)
Btw, if anybody HAS info, or knows where to get it, on using java-script scripts in your program, please reply. Thanks,
Korihor
Advertisement
try something like this:

FILE * file = fopen(outputfile, "w");

fprintf(file, "%s %f\n%i", myName, Number2, number);

fclose(file);

BTW the win32 file functions should end up calling the stdio file functions at some point, and they just make everything more complicated anyway

~DemOSh
juha@pacbell.net
Another way to do it would be to use the fstream library (standard library)


//------------------------------------
#include

fstream outPutFile("outPutFileName.txt", ios::out | ios::trunc);

outPutFile << myName << " " << Number2 << "\n" << Number;

outPutFile.close();
//-------------------------------------

Its great for text files, and input from text files is just as easy

Cel
Cel aka Razehttp://chopper2k.qgl.org
Thanks a lot guys :o)
It''s gonna help me a lot...

And, err :
How does Binary files work ?
as much as input than output ? ca I get a bit info about it ???
(you can find me on IRC : #opengl on undernet)
Binary files, in my little self-centered world, are made by writing a structure to the disk. Basically, you create a structure, fill it, and then:

fwrite(&mystruct, sizeof(STRUCT_NAME), HOW_MANY_TIMES, FILE *ptr);

I think that''s pretty dern close (perfect, actually), but if you''ve got a help file w/ that compiler of yours, just search for fwrite.

Reading is the same, sort of: just make sure the strucutre your reading to is that same as the one you wrote, and then:

fread(&mystruct, sizeof(STRUCT_NAME), HOW_MANY_TIMES, FILE *ptr);

That''s it...should be right.

"Now watch as I run away in a womanly fashion." - Batman
"Now watch as I run away in a womanly fashion." - Batman
Advertisement
Thanks for the binary info, I am gonna check out in MSDN

Now, 2 subquestion to the main question (how to write files)
-----
--1--
-----
Cel, you gave me infos about saving files with the iostream stuff (...yeah, I know fstream) ...
Function is similar to cout << "TEXT" ...
How can I load files with fstream ???
-----
--2--
-----
Is there a way to add stuff at the end of the file or do I have to reload everything everytime (yet I couldn''t get 2 files to open at the same time) and rewrite it in the new files with the new changes ?
What if there is only one line to insert, let''s say, between the first and the second ? What''s da trick
(you can find me on IRC : #opengl on undernet)
Alrighty, with fstream you can read from files by simply declaring your object as ...

ifstream InFile("myfile.txt", ios::in);
Then use it as you would the cin function...

ie
you could use it like this....
InFile >> MyVariable;

but that''s usually bad, since it will put the whole file in there (I''m pretty sure)...

it''s better if you do this...

InFile.getline(MyVariable, HowManyCharsToGet, CharToTerminateAt)

The Terminating char is by default ''\n''... This is only taken into account if the char is found before the character limit is reached.

To append to the end of a file, use this...

ofstream OutFile("myfile.txt", ios::out | ios::app);

The ios::app means append to the file (write to the file at the end... and ONLY at the end)

For writing between two lines, you might look up the seekp function call and ofstream''s ios::ate option. I think those will do it, but I''m not all that sure on how they work.

S.
Okay, thanks a lot Strylinys

Okay, I think for a lot of people, binaries are a dark difficult art (they probably looked at the one in MSDN) ... I dunno cuz I don''t know how to use them ...

If someone could put up a simple fully commented example of it, as simple as possible, about using binaries, it would help a lot of people (including me )

(you can find me on IRC : #opengl on undernet)
Ok ill try some real quick binary examples... here goes:

to read something:

    int integers[NUM_INTS];char chars[NUM_CHARS];FILE * file = fopen("filein.bin", "rb"); // The b indicates binary/*fread arguments are - first a pointer to the data, then the sizeof each individual element, the number of elements, and then a file pointer.*/fread(integers, sizeof(int), NUM_INTS, file);/*Here fread() fills the integers array with NUM_INTS items of size sizeof(int). */fread(chars, sizeof(char), NUM_CHARS, file);/*Same as above, but here each individual item has size sizeof(char).*/fclose(file);// Now write somethingfile = fopen("fileout.bin", "wb"); // wb for write binary... duhfwrite(integers, sizeof(int), NUM_INTS, file);fwrite(chars, sizeof(char), NUM_CHARS, file);/*These calls will first write out the ints we read in, and then write out the chars. The parameters to the function are the same, first a pointer to the data, then the size of each element, the number of elements, and then the file pointer.*/fclose(file);    


If i remember right, the return value should be the number of bytes read/written. So if everything went right the return value should be (sizeof(whatever) * number_of_elements)... not 100% sure bout this one though.

The data you read/write can be whatever you want, not necessarily scalar values. You could write out an array of structures or whatever. You could do this for example, not sure why but...

WNDCLASSEX wcx;

... open file ...

fread(&wcx, sizeof(WNDCLASSEX), 1, file);

... close file ...


Email me if you need any further explanations, maybe ill even write a tutorial.

~DemOSh
demosh@aewebdesign.com

This topic is closed to new replies.

Advertisement