简体   繁体   中英

fread and fwrite have difference result

I am using fread to read from a file and then use fwrite to write to anothe file. but content of the new file is slightly different from the content of the old file (the new file have a lot more character), do you know what can cause this and how to fix it?

to read

file= fopen(locationHolder[x], "r");    
chunkToSend.chunkcontent.data = malloc(20000);
chunkToSend.chunkcontent.len = fread(chunkToSend.chunkcontent.data , 1, 20000, file);

to write

file =  fopen(filelocation, "w"); 
fwrite(responseChunk->chunkcontent.data, 1, responseChunk->chunkcontent.len, file);

this is the file I am reading:

†+ƒCÃÅ«*u`‘Ÿ‘Ž÷-U‰ÎVøÈ  ýÇ}‡3²SûYè³…öeŠ
5êèMØ“—­~K¾ëb–܆º[ö¿äWÔM)
ø˜i ¯’#\×b!¥^b€÷ûƒãq.]…©\.u7بøöÔv´cgÒD–o_ÊûG}^ªmÞŸ}ÛåŒ}?Ž…WíëH
?̓>—”*î`ÔÿïÖtàNÕ$Cú¾@Ž›¿Žíohy5ÅE5qز9¡_S雉+þ¦¥²‘*Jíf†
#¸«vÌÏ  s¼üEÃe+vLrF¯Gå3šú¦Ì×Mçó
<f¥#ÇyEBŠ1iÉŒ6pZa›MSÅîl:ôšËħ   
Jþ}ÜÍÃÑ1˜´+$f̆\Ø$±)sæâ¼üõÅ‘Ï™Àd–ÚDM*Ì
R¯èÕ¼£C†}—
¸mݼž„ª1øL5E‰_ð7âHö:´¡`¼E[Ö<BJüR’½Ê€“%8p
LÄ] ¡û2ì––û}ðCÓîŽBáæeÎøëåZ§ "
[Òºèf¯Œÿ»'L"g‚æ°C«G§¸IŸ‰ˆÚ‘_´G’.ÂÝp¯K%RÕlÍâ3÷À+\9rØ‚/˱V¨?Ù}ܨ›`l½î~RŽbŒ—w˜[£¦2™¾RÿÁeÎPbeö‘¿±¤N#Oåãº&
Êc9OËbÿÊ3ÚV⛩²TPdx»ÉbèÁW8×MAà¥ó]D
å™QØè’¿=Ÿ±­$BºÀŒdž\3âZ³‰×…׉…¹dؾíÂÂÎA^ǦèÕ`±³ï
<µ¡QGÙY—-p÷›AÂ+pžD‘©ûÊÄÉ÷¢y×çÃt…ˆæîYiGÇÊAðÁô¡4YWoâ=ÊÁ+¤·•Ï)ùïéÚTVî±0ME¡{Èÿ‚.G°Ágª¯‚É9&­ê¡ÑUIÃĤb†§JâY&_SæþJãi›©'È›XA’M[dY&²AbîAëfG½€oN(oM+CzíµÚ

this is the file I wrote:

†+ƒCÃÅ«*u`‘Ÿ‘Ž÷-U‰ÎVøÈ  ýÇ}‡3²SûYè³…öeŠ
5êèMØ“—­~K¾ëb–܆º[ö¿äWÔM)
ø˜i ¯’#\×b!¥^b€÷ûƒãq.]…©\.u7بøöÔv´cgÒD–o_ÊûG}^ªmÞŸ}O…ªœ¦ÛåŒ}?Ž…WíëH
?̓>—”*î`ÔÿïÖtàNÕköR,E·$Cú¾@Ž›¿Žíohy5ÅE5qز9¡_S雉+þ¦¥²‘*Jíf†
#¸«vÌÏ  s¼üEÃe+vLrßÞ¾"ò1%áoÌû/êF¯Gå3šú¦Ì×Mçó
<f¥#ÇyEBŠ1iÉŒ6pZa›MSÅîl:ôšËħ   
Jþ}ÜÍÃÑ1˜´+$f̆\Ø$±)sæâ¼üõÅ‘Ï™Àd–ÚDM*Ì
R¯èÕ¼£C†}—
¸mݼž„ª1øL5E‰_ð7âHö:´¡`¼E[Ö<BJüR’½Ê€“%8p
LÄ] ¡û2ì––û}ðCÓîŽBáæeÎøëåZ§ "
[Òºèf¯Œÿ»'L"g‚æ°C«G§¸IŸ‰ˆÚ‘_´G’.§b[•’KÛ0I¼û´)•Á
é_“Ä*ÂÝp¯K%RÕlÍâ3÷À+\9rØ‚/˱V¨?Ù}ܨ›`l½î~RŽbŒ—w˜[£¦2™¾RÿÁeÎPbeö‘¿±¤N#Oåãº&
Êc9OËbÿÊ3ÚV⛩²TPdx»ÉbèÁW8×MAà¥ó]D
å™QØè’¿=Ÿ±­$BºÀŒdž\3âZ³‰×…׉…¹dؾíÂÂÎA^ǦèÕ`±³ï
<µ¡QGÙY—-p÷›AÂ+pžD‘©ûÊÄÉ÷¢y×çÃt…ˆæîYiGÇÊAðÁô¡4YWoâ=ÊåÓñš‰Ö_”]½ÅÛô8ª<‡·gœ¡C`øm¼Á+¤·•Ï)ùïéÚTVî±0ME¡{Èÿ‚.G°Ágª¯‚É9&­©pöïMâF³°Ä}à–+iä0;ˆ7Y|Å4u¹SFŸwê¡ÑUIÃĤb†§JâY&_SæþJãi›©'È›XA’M[dY&²AbîAë[‚þ%\6¿ÿ=î…Èq‚ÌÉ$é“o
àƒž“ªÔ~ºÐ¹ÿÔ_löfG½€oN(o§ƒþ7Í
Âý0«F\!¡Ä¹\ÙoÑ,¼W™´CC#mà]

**------never mind, figured out the problem. If you open the same file on window and linux, they still look different. I open the old file on windows while I open the new file linux.

If you want to read and write the files as binary on a Windows system, you'll need to specify that when you open them for reading:

file=fopen("source", "rb");

and for writing:

file=fopen("target", "wb");

Also, don't forget to check to see if the opens succeeded before using the filehandle. ( file != NULL )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM