reading file into an array in c

I have a matlab data file that i open with fopen "r" using fread. to array but doesn't work
int *fp;
fp=&array;
fseek(input,0L, SEEK_END);
lFileLen = ftell(input);
rewind(input);
fp = calloc(lFileLen+1, sizeof(int));

for(i=0;i<lFileLen;i++)
{
fread(fp, lFileLen,1, input);
}

Comments

  • [color=Blue]Issues:

    1. Before reading you must return file pointer back to original position - you moved it to the very end to use ftell(). Just before the FOR() loop you have to perform another seek operation to the beginning of the file.

    2. Using fread() assumes that file is open as BINARY file and not as TEXT file. In your case - reading binary integer values - you need a BINARY type of file opening. Did you do that? The TEXT type of operations will remove 0x0A bytes from the data stream and your integers may get corrupted.[/color]
  • sorry, didn't put all in. The values being read are integers of the like 1.4000000e-01 matlab output from FFT chirp example1 - save('data.txt','-ascii');

    what function would you suggest
  • [color=Blue]I am not sure what MATLAB is, however, if you use fread() - then file should have been open with following code:[/color]
    [code]
    FILE* file = fopen ("YOUR_FILE_NAME_HERE", "rb");
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories