fread() returns invalid integer - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

fread() returns invalid integer

guidj0sguidj0s Posts: 2Member
Hey guys. Need some help. I'm using fread() to read an integer from a file. This integer will be later used to generate a 2d matrix from additional information within that file.

The problem is, whenever i try to fread(), the function returns an invalid (and very big) value. Here's my line:


[code]
function readmatrix(char *arq, double **matrix){

FILE *arqmat;
int ordem;

if (!(arqmat = fopen(arq,"r"))){
printf("

cant access file.

");
return 0;
}
else{
if(!fread(&ordem,sizeof(ordem),1,arqmat)){
printf("

Error trying to read the order.

");
return 0;
}
else{
printf("

Order obtained: %d",ordem);
}
... [allocate matrix, read the rest of the file and store it, return matrix pointer]
}
}
[/code]

I've omitted irrelevant variables within the function that are used later.

Now, when I try to read a plain text file containing JUST the integer "5", it returns 0 and prints "Error trying to read the order."

If I try to read a plain text file containing "5 5 5 5 5", "ordem" receives " 540352565".

I'm runnin linux (gcc), if that makes any difference.

Just as a test, I read the same file with fgets() and it worked just as expected, so I don't think it's related to the OS.


Thanks in advance.

Comments

  • LundinLundin Posts: 3,711Member
    Try to open it with "rb" (read binary).
  • PruyquePruyque Posts: 149Member
    Hello,

    fread() reads binary data (even if the file is opened in 'text'-mode)
    the number 540352565 in hex is 0x20352035, the 0x20 is the ascii code of a space and 0x35 is the ascii code of a '5' (they are in reversed order because you're using a little-endian machine, in case you're wondering). So what fread does is read the ascii codes from the file and builds an int from it, expecting binary data. This should explain the behavior when reading the '5 5 5' file. The same happends when reading the file with a single '5', but only one byte can be read (or two if it is followed by a newline) and fread should fail if it reads less than sizeof(ordem) bytes, which is 4 (in your case).

    Have a look at the fscanf-functions, these can read textfiles and put the read data into an int in the way you want.
    It would look something like this:
    [code]
    fscanf(arqmat, "%i", &ordem);
    [/code]

    I hope this has made it a little bit more clear.
    Happy coding!

    : Hey guys. Need some help. I'm using fread() to read an integer from
    : a file. This integer will be later used to generate a 2d matrix from
    : additional information within that file.
    :
    : The problem is, whenever i try to fread(), the function returns an
    : invalid (and very big) value. Here's my line:
    :
    :
    : [code]:
    : function readmatrix(char *arq, double **matrix){
    :
    : FILE *arqmat;
    : int ordem;
    :
    : if (!(arqmat = fopen(arq,"r"))){
    : printf("

    cant access file.

    ");
    : return 0;
    : }
    : else{
    : if(!fread(&ordem,sizeof(ordem),1,arqmat)){
    : printf("

    Error trying to read the order.

    ");
    : return 0;
    : }
    : else{
    : printf("

    Order obtained: %d",ordem);
    : }
    : ... [allocate matrix, read the rest of the file and store it, return matrix pointer]
    : }
    : }
    : [/code]:
    :
    : I've omitted irrelevant variables within the function that are used
    : later.
    :
    : Now, when I try to read a plain text file containing JUST the
    : integer "5", it returns 0 and prints "Error trying to read the
    : order."
    :
    : If I try to read a plain text file containing "5 5 5 5 5", "ordem"
    : receives " 540352565".
    :
    : I'm runnin linux (gcc), if that makes any difference.
    :
    : Just as a test, I read the same file with fgets() and it worked
    : just as expected, so I don't think it's related to the OS.
    :
    :
    : Thanks in advance.
    :
  • guidj0sguidj0s Posts: 2Member
    Beautiful answer. Got it 100%.

    Thanks guys.
Sign In or Register to comment.