Jump to content
  • Advertisement

Archived

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

hello_there

fwrite()

This topic is 5555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can someone tell me how to use fwrite(). and also why is the second parameter "rb"?. I have this.in c++; FILE *filep; int filel; char *buffer;//loaded from another file filep = fopen("file.txt","rb"); fseek(filep, 0, SEEK_END); filel = ftell(filep); fseek(filep, 0, SEEK_SET); fwrite();//not sure what goes here fclose(filep);

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
the second param in fopen is the options for how you want to open it. "r" means read-only. dunno what the "b" means.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
fwrite(bufferPtr, elementSize, elementCount, filePtr);

bufferptr is pointer to data you want to save, elementSize
is the size in bytes of one element (eg. sizeof(int)),
elementCount is how many items you want to write and filePtr
is the FILE * pointer.

The "RB" means you open file in "read binary" form. So if you
want to use fwrite(), you need to use "WB", which is "write binary". RB is used when you read files.

Share this post


Link to post
Share on other sites
This is how i load and save the data. it''s no working can you show me why please?

FILE *fileptr;
FILE *filep;
int filelen;
int filel;
char *buffer;

fileptr = fopen("file1.txt","rb");

fseek(fileptr, 0, SEEK_END);
filelen = ftell(fileptr);
fseek(fileptr, 0, SEEK_SET);

buffer = (char*)malloc(filelen + 1);
fread(buffer, sizeof(char),filelen,fileptr);

fclose(fileptr);

filep = fopen("file2.txt","wb");

fseek(filep, 0, SEEK_END);
filel = ftell(filep);
fseek(filep, 0, SEEK_SET);

fwrite(buffer, sizeof(char),filel,filep);

fclose(filep);

Share this post


Link to post
Share on other sites
If you are writing a program for Windows I suggest using "CreateFile" instead:

void* hFile = CreateFile(sFilePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

http://msdn.microsoft.com/library/en-us/fileio/base/file_management_functions.asp

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!