noobie binary questions

This is a discussion on noobie binary questions within the C Programming forums, part of the General Programming Boards category; this is probably a really lame question for all you brainiacks but thanks to assistance from people on this forum ...

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    Smile noobie binary questions

    this is probably a really lame question for all you brainiacks
    but thanks to assistance from people on this forum i've started
    to experiment in accessing files in binary. But i dont understand
    what the numbers are that i get from using fread(). What do these
    long numbers represent??

    for example if i use this code on a jpg file......

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    main()
    {
        int r,i;                              /*r holds what i get from fread*/
        FILE *ptr;
    
        ptr=fopen("c:\\test.jpg","rb");
        
        if(ptr==0){
        printf("didnt work");
        getchar();}
    
        else
        printf("it worked fine\n\n");
    
        for (i=0;i<3;i++){
    
        while(fread(&r,sizeof(r),1,ptr)!=1)
             {
                  printf("Write error occurred\n");
                  getchar();
                  exit(1);
              }
     
        printf("%i\n", r);
     }
        fclose(ptr);
        getchar();
        
        
        return 0;
    }

    .....i get -520103681
    1179258880
    16795209

    what do these mean?? What are they?
    And if i change the integer r to a long, printf will not display anything!
    can anybody tell me why!?!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    What did you think reading 3 integers from a jpeg file would do? It read some binary information from the header and stuffed it in an int. Probably nothing meaningful. %i is the integer formatting flag for printf, you need to add the length specifier l so %li or %ld.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  2. best STL method to implement a binary tree
    By MatthewDoucette in forum C++ Programming
    Replies: 8
    Last Post: 06-16-2006, 08:08 AM
  3. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 12:43 AM
  4. Binary Trees
    By wvu2005 in forum C Programming
    Replies: 7
    Last Post: 10-15-2005, 05:59 PM
  5. Reading/Writing in Binary
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2001, 10:32 PM

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