How To read/write C array stored in text file created through bin2C utility
I am creating a application, input to which is a C file having an array ( which is created with bin2C.exe ) , the code segment of the C file is:-
unsigned int MyArray[] = { 0开发者_运维知识库x00000001,0x00000002,0x00000005 ...};
now I need to read this array as a text file and story its value in an int array. Then I need to modify that array and put it back to text file so that the final output would look like:-
unsigned int MyArray[] = { 0x39481212,0x33943121,0x3941212 ...};
Please let me know how can I do this in C/VisualC++ as my application is in VC++ with MFC?
Regards, Vikas
input
open the file (fopen
) in text mode and read the lines (fgets
and sscanf
)
store array
you cannot have an array with unspecified size. You must either use a size limit and leave some elements unused, or use malloc
and friends and manage storage for the array manually
modify
use +
, -
, *
and other operators along with sqrt
, abs
, sin
and other functions available in the Standard Library to massage the data (you can create some functions of your own too)
put it back
It's better to write to a new file and if everything went ok, delete the old file and rename the new one ... so open the new file (fopen
with "w" for mode parameter) at the beginning when opening the input; if the input is not the array line write (fputs
) it back directly, otherwise write the 'massaged' line; read and write all the other lines.
(C++?) with MFC
The above is for C
. It might work for C++
with or without MFC
Regards
Have fun!
I think the best way was to read and use fprintf/sprintf functions to get and put the data as int from file.
精彩评论