| | |
Error in File write of dat file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 66
Reputation:
Solved Threads: 0
Consider d_type=POINT_LONG in the following program.
and data.Long=0xA;
In the following program i am getting correct output in constDataArea[0],constDataArea[1],constDataArea[2], constDataArea[3]
which is oxA, 0x0, 0x0, 0x0.
But not able to write the same into ambi.dat file. Some junk value is inserting in the front.
and writing 5 bytes instead of 4 bytes.
Output file(ambi.dat)is as follows
-----------------
0D 0A 00 00 00
-----------------
Expected output in dat file is
---------------
0A 00 00 00
--------------
What is the error in this code. plz give me suggestion
and data.Long=0xA;
In the following program i am getting correct output in constDataArea[0],constDataArea[1],constDataArea[2], constDataArea[3]
which is oxA, 0x0, 0x0, 0x0.
But not able to write the same into ambi.dat file. Some junk value is inserting in the front.
and writing 5 bytes instead of 4 bytes.
Output file(ambi.dat)is as follows
-----------------
0D 0A 00 00 00
-----------------
Expected output in dat file is
---------------
0A 00 00 00
--------------
What is the error in this code. plz give me suggestion
c Syntax (Toggle Plain Text)
char *constDataArea; const size_t BUFSIZE = 1024*1024; constDataArea=(char *)malloc(BUFSIZE); size_t datasize=0, nextpos=0, freepos=0; for(int nv_data=0,indx=0;nv_data<nonVoltleData;nv_data++) { switch(d_type) { case POINT_BOOL: data.Bool=(char)tmpbuf[index+2+nv_data+indx]; fprintf(out_fp,"\nBool Data1:%x\n", data.Bool); datasize=1; nextpos=freepos+datasize; memcpy(constDataArea+freepos,(char *)&data.Bool, datasize); /*data.bool から ConstDataArea にコピー*/ freepos=nextpos; break; case POINT_CHAR: data.Char=(char)tmpbuf[index+2+nv_data+indx]; fprintf(out_fp,"\nChar Data1:%x\n", data.Char); datasize=sizeof(data.Char); nextpos=freepos+datasize; memcpy(constDataArea+freepos,(char *)&data.Char, datasize); /*data.Char から ConstDataArea にコピー*/ freepos=nextpos; break; case POINT_SHORT: data.Short=(short)tmpbuf[index+2+nv_data+indx]; fprintf(out_fp,"\nShort Data1:%x\n", data.Short); datasize=sizeof(data.Short); if(freepos % sizeof(data.Short)) { freepos+= sizeof(data.Short)-freepos % sizeof(data.Short); } nextpos = freepos + datasize; fprintf(out_fp,"S:ConstArea:%d\tfreepos:%d\n", constDataArea, freepos); memcpy(constDataArea+freepos,(char *)&data.Short,datasize); /*data.Short から ConstDataArea にコピー*/ freepos=nextpos; break; case POINT_LONG: data.Long=(long)tmpbuf[index+2+nv_data+indx]; fprintf(out_fp,"\nLong Data1:%x\n", data.Long); datasize=sizeof(data.Long); if(freepos % sizeof(data.Long)) { freepos+= sizeof(data.Long)-freepos % sizeof(data.Long); } nextpos=freepos+datasize; memcpy(constDataArea+freepos,(char *)&data.Long, datasize); /*data.Long から ConstDataArea にコピー*/ freepos=nextpos; break; case POINT_FLOAT: data.Float=*(float*)&tmpbuf[index+2+nv_data+indx]; fprintf(out_fp,"\nFloat Data1:%f\n", data.Float); datasize=sizeof(data.Float); if(freepos % sizeof(data.Float)) { freepos+= sizeof(data.Float)-freepos % sizeof(data.Float); } nextpos=freepos+datasize; memcpy(constDataArea+freepos,(char *)&data.Float, datasize); /*data.Float から ConstDataArea にコピー*/ freepos=nextpos; break; case POINT_DOUBLE: data.Double = *(double*)&tmpbuf[index+2+nv_data+indx]; fprintf(out_fp,"\nDouble Data1:%lf(0x%0x)\n", data.Double, data.Double); datasize=sizeof(data.Double); if(freepos % sizeof(data.Double)) { freepos+= sizeof(data.Double)-freepos % sizeof(data.Double); } nextpos=freepos+datasize; memcpy(constDataArea+freepos,(char *)&data.Double, datasize); /*data.Double から ConstDataArea にコピー*/ freepos=nextpos; break; } } FILE *f1; f1=fopen("c:\\ambi.dat", "w"); fwrite(constDataArea, 1,4, f1); fclose(f1); printf("\nconst area:0x%x\t0x%x\t0x%x\t0x%x\n\n",constDataArea[0],constDataArea[1],constDataArea[2], constDataArea[3]);
Last edited by ambarisha.kn; Nov 5th, 2008 at 12:59 am.
Ambarish
•
•
Join Date: Jun 2008
Posts: 66
Reputation:
Solved Threads: 0
Its so simple, we have to write "wb" in fwrite instead of "w" , while writing binary file.
I did changes as above and i got output..
c Syntax (Toggle Plain Text)
FILE *f1; f1=fopen("c:\\ambi.dat", "wb"); fwrite(constDataArea, 1,4, f1); fclose(f1);
I did changes as above and i got output..
Last edited by ambarisha.kn; Nov 5th, 2008 at 10:54 pm.
Ambarish
![]() |
Similar Threads
- Double linked list - possible memory error (C)
- how do I have txt file be read by the compiler? (C++)
- error in guessing game (Python)
- Custom Class - File I/O (C++)
- Please help, im new at this :/ (C)
- Help with Java program writing (Java)
- 15 error(s), 0 warning(s) (C++)
- Read/write to same file > once, Help (C++)
- Multithreading problem (C)
- data file help (C)
Other Threads in the C Forum
- Previous Thread: how to read data file
- Next Thread: warning C4715???
| Thread Tools | Search this Thread |
#include adobe ansi api append array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework frequency function getlasterror givemetehcodez global grade gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab windows.h windowsapi





