Archived

This topic is now archived and is closed to further replies.

Viajero

Simple compressing and file operations in C

Recommended Posts

Could someone please show me how FILE is defined and how fopen() reserves memory for it? I can only find prototypes in the stdio.h header, but I guess the actual libraries are already compiled and are included in the linking process. (how I couldn''t even find "typedef FILE" from anywhere beats me..) Also if I save text and integers into a file, what kind of routines could I use to compress the data or just make it look really cryptic (when opened in a standard file editor)? I know how to count in hexa decimal (00-FF), but how do I save/load normal data to/from that form..

Share this post


Link to post
Share on other sites
You really don''t need to know what''s behind a FILE pointer - but if you insist, look for the _iobuf structure. If you want to know what fopen does examine the sources for the C runtime. MS ships them with MSVC and sometimes you can find them on the www too. A good query to try at google is "CRT SRC".

Compression can be a hairy beast. If you want a simple method to disguise your text, google on XOR encryption.

Share this post


Link to post
Share on other sites
You could use zlib for compression, and its usage is similar to that of fopen, fclose, fread, fwrite, etc, if you are planning to encrypt the data you should do that afterwords (encrypted data is usually hard to compress), although xor encryption shouldn''t destroy any of the patterns in the data if your key is only a byte so you might be able to get away with it.

Share this post


Link to post
Share on other sites
Thanks. The reason why I wanted to know how the file functions etc operate, is that I thought it would be easy to make my own functions, but apparently it's not. For example I thought fopen() "mallocates" the file and returns an address to a memory block and that a file would be just an array of structures. I might aswell just cope with the existing functions. Right now I'm pondering how I could load a file to memory (assuming fopen() returns a pointer pointing "directly" to the file on the hard drive. it would seem so to me, since fputc() modifies the file at its own)

[edited by - viajero on January 19, 2003 8:48:46 AM]

Share this post


Link to post
Share on other sites
Oh, did you mean like save the variables as binary data rather than as text?


  
#include <stdio.h>

int main(void)
{
int number;
FILE *file;

// open file for writing

if(!(file = fopen("asdf.bin", "wb")))
{
printf("Error opening the file.");
return 0;
}

number = 123;

// write number to file

if(fwrite(&number, sizeof(number), 1, file) != 1)
{
printf("Error writing to file.");
return 0;
}

// close file

fclose(file);

// open file for reading

if(!(file = fopen("asdf.bin", "rb")))
{
printf("Error opening the file.");
return 0;
}

// set number to 0 so i can show the number is actually being read from the file.

number = 0;

// read number from file

if(fread(&number, sizeof(number), 1, file) != 1)
{
printf("Error reading from file.");
return 0;
}

// close file

fclose(file);

// print the number we read.

printf("The number is: %d", number);

return 0;
}


That is c, you would probably want to use fstream in c++.

Share this post


Link to post
Share on other sites