views:

42

answers:

1

Hi,

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[] = { 0x00000001,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

A: 

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!

pmg
Thanks,but I really dont understand what it meant when you say use library functions?
Viks
The Standard Library defines many functions declared in a few headers documented by your compiler documentation. For example, `abs` is declared in `<stdlib.h>`; `sqrt` and many other useful functions are declared in `<math.h>` ( http://opengroup.org/onlinepubs/007908775/xsh/math.h.html )
pmg