a lil' help using fread()

This is a discussion on a lil' help using fread() within the C Programming forums, part of the General Programming Boards category; Can someone please help me with the usage of fread? like errm, explain why my small test code don't quite ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    51

    a lil' help using fread()

    Can someone please help me with the usage of fread?

    like errm, explain why my small test code don't quite work.

    It's just reading a series of numbers(int) from a file.

    Code:
    int main(int argc, char **argv)
    {
            FILE *fp;
            int *number = (int *)malloc(sizeof(int));
            int n;
    
            fp = fopen(argv[1], "r");
    
            while((n = fread(&number, sizeof(int *), 1, fp)) > 0);
                    printf("n: %d -- %d\n", n, number);
    
            return 0;
    }
    many thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    while((n = fread(&number, sizeof(int *), 1, fp)) > 0);
    Remove the red & sign - you already have a pointer, you don't want to write the number into your pointer, but into what your pointer points to (what you got back from malloc).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    51
    Code:
    int main(int argc, char **argv)
    {
            FILE *fp;
            int *number = malloc(sizeof(int));
            int n;
    
            fp = fopen(argv[1], "r");
    
            while((n = fread(number, sizeof(int *), 1, fp)) > 0);
                    printf("n: %d -- %d\n", n, number);
    
            return 0;
    }
    ~
    this is the new version, but gcc kept complaining about the printf statement
    Code:
    read.c: In function ‘main’:
    read.c:13: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
    and it didn't print out the write stuff in the file...

    thx!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what do you think that means? Perhaps you need a *number in your printf?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    51
    yea, i did that, but, somehow it still doesn't give the right output....
    Code:
      printf("n: %d -- %d\n", n, *number);
    is it due to me using 'vi' and typing numbers in a file rather than using fwrite() to write numbers to it....
    that's why fread() doesn't seem to work right??

    thx

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by wuzzo87 View Post
    yea, i did that, but, somehow it still doesn't give the right output....
    Code:
      printf("n: %d -- %d\n", n, *number);
    is it due to me using 'vi' and typing numbers in a file rather than using fwrite() to write numbers to it....
    that's why fread() doesn't seem to work right??

    thx
    Well, it's probably showing exactly what you entered. Try this:
    enter 1111, then see if the number you get is 825307441.

    fread reads a binary format, not text-format, so your data content will not be (easily) typed in for example vi.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    51
    Hey thanks a lot for the help!!!!
    certainly is useful

    I got it to work already

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > sizeof(int *)
    If you're reading an int, use sizeof(int)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    > sizeof(int *)
    If you're reading an int, use sizeof(int)
    Good catch - it would work until you put it on a system that hasn't got the same size pointers as integers, such as a 64-bit OS. Subtle bug indeed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. using fread on stdin
    By nadroj in forum C Programming
    Replies: 29
    Last Post: 10-23-2008, 02:03 PM
  3. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 04:30 AM
  4. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  5. fread item_size limit
    By nvoigt in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2005, 08:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21