• Advertisement

Archived

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

I'm having trouble with file writing in DOS

This topic is 6546 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

I''m using Borland C++ and I''m writing a paint type program in DOS, I added file saving and loading support(very primitive) but it works for the most part, it loads up the picture just fine, but when it saves apparently it changes some of the picture, and it''s the same changes every time, but I use a for loop to store the picture and the rest of the picture is fine. Here''s the important stuff : unsigned char* bkgrnd; ... bkgrnd = (unsigned char*)malloc(64000); memset(bkgrnd,BLACK,64000); ... char name[20]; printf("\nUnder what name? : "); scanf("%s",name); FILE *infile,*outfile; infile = fopen(name,"r"); if(infile==NULL){ outfile = fopen(name,"w"); fseek(outfile,0,SEEK_SET); for(long x=0;x<64000;x++) //What actually writes to file fputc(bkgrnd[x],outfile); // fclose(outfile); Please, if you have any idea what could be wrong, please tell me.

Share this post


Link to post
Share on other sites
Advertisement
Hi!

Try opening your file in binary mode:
outfile = fopen(name,"w+b");
and read it in binary too: outfile = fopen(name,"r+b");
I guess you are using text files right now and it gives you problems due the carriage chars...

you can read it just like this:

unsigned char *imgaux;

imgaux=(byte *)farmalloc(tx*ty); //Size X,Y
fread(imgaux,tx*ty,1,todo); //read all the file

Ah! dont use fputc to write to the file, instead use fwrite

Hope it helps..

Share this post


Link to post
Share on other sites
You could also use fprintf for writing to the file. That has worked fine for me, but i am not sure about binary files...

- Daniel
http://sw.mtx.net/daniel/

Share this post


Link to post
Share on other sites
Wow, thanks Habus, that worked, I thought it may have been due to text file formats, but I wasn''t sure how to open it in binary format, thanks again!!

Share this post


Link to post
Share on other sites

  • Advertisement