Advertisement

I'm having trouble with file writing in DOS

Started by March 16, 2000 08:59 PM
2 comments, last by BraveZeus 24 years, 11 months ago
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.
Chris
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..
"Old programmers don't die,they just terminate and stay resident."
Advertisement
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/
- DanielMy homepage
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!!
Chris

This topic is closed to new replies.

Advertisement