Read int from a file with C

Discussion in 'OT Technology' started by Seeders, Jun 3, 2008.

  1. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
    Code:
            fp = fopen("test", "r");
    
            if(fo == NULL || fp == NULL){ printf("File not found."); exit(1); }
    
            int *num_records = malloc(sizeof(int));
            fread(num_records,sizeof(int),1, fp);
    
            printf("%d\n", *num_records);
    
    in the file "test" i have simply placed an 8, this program prints out 2616 though. what am i doing wrong?

    is the 8 in "test" actually a char or something?
     
  2. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    Its probably being treated as a char. You should really look into using something besides fread(), like fscanf.
     
  3. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
    Ya ill look into fscanf.

    My assignment is to read a file that has a 32-bit int tacked on to the front that tells the program how many threads to use. How can I make a test file that doesn't treat the first int as a char?
     
  4. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    do you really need a 32-bit int to tell you how many threads to use?

    what is the range of threads you could use? 32-bit is huge for that, imo.
     
  5. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    You can't. Data from files in c always comes in as text. But, look up the function atoi(). It does what you need.
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    False. The fopen function can be given the "b" mode specifier so that it treats the file as binary.

    fopen("test", "rb");
     
  7. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    If you're required to read binary data from the file, then fscanf won't help. You can write a program that will make a test file for you, or maybe use a hex editor.
     
  8. Ameter

    Ameter Active Member

    Joined:
    Mar 31, 2005
    Messages:
    97,795
    Likes Received:
    1
    Location:
    Calgary, AB
    why are you declaring your number as a pointer? Why not just declare an int, and then do fread(&integer......

    As for the test file, if you opened it up in an editor and wrote '8', then saved and closed, what is stored depends on the editor.

    It will either be 8-bit ASCII code for '8', 16-bit unicode for '8', a combination of the previous and an end of line/carriage return, etc

    It's definitely not a single 8-bit value, but there's no way you will get an integer value of 8
     
  9. Ameter

    Ameter Active Member

    Joined:
    Mar 31, 2005
    Messages:
    97,795
    Likes Received:
    1
    Location:
    Calgary, AB
    use fwrite to create the test file, and write a number to it with that.

    Any sort of text editor will not give you what you want
     
  10. Ameter

    Ameter Active Member

    Joined:
    Mar 31, 2005
    Messages:
    97,795
    Likes Received:
    1
    Location:
    Calgary, AB
    :slap:

    you're retarded
     
  11. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    You're right that you can do it. I shouldn't have posted "You can't." And he should change the pointer to an actual int.

    But fuck all that. Just use
    Code:
    fscanf(fp, "%d", &num_records);
    
     
  12. Ameter

    Ameter Active Member

    Joined:
    Mar 31, 2005
    Messages:
    97,795
    Likes Received:
    1
    Location:
    Calgary, AB
    Yeah, that only works if you're reading in a TEXT file.

    Which is highly inefficient and can easily have bugs in reading code resulting in errors.
     
  13. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    I'm going to assume that, since its an assignment, the data file is a simple text file.
     
  14. Mike99TA

    Mike99TA I don't have anything clever to put here right now

    Joined:
    Oct 3, 2001
    Messages:
    4,553
    Likes Received:
    0
    Location:
    Greenville, SC
    Umm...yeah, I highly doubt that this basic of a problem is from an assignment that expects you to read a binary file.
     
  15. Ameter

    Ameter Active Member

    Joined:
    Mar 31, 2005
    Messages:
    97,795
    Likes Received:
    1
    Location:
    Calgary, AB
    since it's telling him to use fread, maybe it is

    text is more difficult than binary, imo
     
  16. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    Doesn't say he has to use fread.
     
  17. Ameter

    Ameter Active Member

    Joined:
    Mar 31, 2005
    Messages:
    97,795
    Likes Received:
    1
    Location:
    Calgary, AB
    no, but odds are good he's using what he was taught to use in class
     
  18. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,189
    Likes Received:
    508
    2616? Interesting. In hex, that's 0A38. 0A is a linefeed, and 38 is the ascii code for the character '8'. You're definitely reading in text.


    It seems your problem isn't the reading - that looks like it's working. The problem is your file creation, like Skinjob said.
     
  19. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    Based on your analysis of the file, its fine. It contains a digit eight character. The problem is that he's reading in char data and assigning it to an int.

    The easiest solution is to either use fscanf with a format string that specifies reading in integer data, or read in the char data and convert it with atoi().

    Also, OP, take the leading line feed out of your text file.
     
  20. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,189
    Likes Received:
    508
    Exactly. That's what I meant to say, you just said it better. His error was typing the character 8 into a text file and expecting to get back a 00 00 00 08 int value back. He needs to put binary data into the file if that's what he means to get out of it.

     

Share This Page