Read a binary file

This is a discussion on Read a binary file within the C Programming forums, part of the General Programming Boards category; I have created a binary file containing selected stat information, and need to read that file, use the "name" field ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    Read a binary file

    I have created a binary file containing selected stat information, and need to read that file, use the "name" field from the file to collect current stat information, and then compare the selected stat information from the input file data to the current data - and display any abnomalies.

    I have written the attached - which compiles!! - but i get a segmentation fault when i try to run it.

    Can anyone suggest why??
    thanks
    Sue

    vi bilist.c
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>



    #define NAMESIZE 256
    #define TIMESIZE 50


    typedef char name_type[NAMESIZE +1];
    typedef char time_type[TIMESIZE +1];


    typedef struct
    {
    name_type name;
    u_short mode;
    short user_id;
    short group_id;
    int size;
    time_type time_last_mod;
    }file_sig;



    //main (int argc, char *argv[])
    main()
    {

    struct stat file_info;
    file_sig in_file_record;

    FILE *fp;

    //if (( fp = fopen(argv[2], "rb")) == NULL)
    // {
    // perror(argv[2]);
    // exit(1);
    //
    // }

    fp = fopen("home/spaterniti/wrkshops/b1","rb");


    // read the input file
    while (!feof(fp))
    {
    fread(&in_file_record, sizeof(file_sig), 1, fp);

    // get the new stat info for the file name just read
    // stat(file_sig.name, &file_info);

    // compare the fields
    if (in_file_record.mode == file_info.st_mode) {
    printf("the modes match");
    }

    //printf("%c\t",in_file_record.name);
    //printf("%o\t",in_file_record.mode);
    //printf("%d\t",in_file_record.user_id);
    //printf("%d\t",in_file_record.group_id);
    //printf("%d\t",in_file_record.size);
    //printf("%c\n",in_file_record.time_last_mod);

    // }
    }
    fclose(fp);
    exit(0);
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    code as requested

    Hi Salem,
    Here is the code that wrote the binary file.

    regards
    sue
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>


    #define NAMESIZE 256
    #define TIMESIZE 50

    typedef char name_type[NAMESIZE + 1];
    typedef char time_type[TIMESIZE +1];

    //typedef struct
    //{
    // char in_name[NAMESIZE];
    //} file_in;

    typedef struct
    {
    name_type name; /*File or directory name*/
    u_short mode; /*protection and file type*/
    short user_id; /*User ID of owner*/
    short group_id; /*Group ID of owner*/
    off_t size; /*file size in bytes*/
    time_type time_last_mod; /*modify time as string*/
    } file_sig;


    main(int argc, char *argv[])
    {

    struct stat file_info; /* define the stat variable*/
    file_sig out_file; /* define the output file variable*/

    name_type in_file; /* define the input file variable*/

    // initialise the input and output file names
    //in_file_name = argv[2];

    //out_list_name = argv[3];

    // The file pointers
    FILE *fp,*fp1; /*The output file*/
    //FILE *fp1; /*The input file*/


    // open the input and output files

    // if (( fp = fopen(argv[2], "r")) == NULL)
    // {
    // perror(argv[2]);
    // exit(1);
    // }

    // fp1 = fopen(argv[3], "wb");
    fp = fopen("/home/spaterniti/wrkshops/filelist", "r");
    fp1 = fopen("/home/spaterniti/wrkshops/b1", "wb");

    // if (( fp1 = fopen(argv[3], "wb")) == NULL)
    // {
    // perror(argv[3]);
    // exit(1);
    // }

    // while there are records read the text file
    // and collect the stat information for the selected file
    // and write a modified stat record to the output file

    while (!feof(fp))
    {
    // printf("%c", &in_file);

    fscanf(fp, "%c", &in_file);
    stat(in_file, &file_info);


    // create the output data fields from the stat'd data

    strcpy(out_file.name, in_file);
    //printf("%s\n", &in_file);
    //printf("%s\n", &out_file.name);

    out_file.mode = file_info.st_mode;
    out_file.user_id = file_info.st_uid;
    out_file.group_id = file_info.st_gid;
    out_file.size = file_info.st_size;
    strcpy(out_file.time_last_mod, ctime(&file_info.st_mtime));


    //stat("/home/spaterniti/wrkshops/ws01.1.c", &file_info);

    // read each record from the list file
    // and collect and store the stat information
    fwrite(&out_file, sizeof(file_sig),1,fp1);
    }


    // close the files
    fclose(fp);
    fclose(fp1);

    exit(0);
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    code as requested

    I have attached the code.

    Salem, I have read the link you suggested, but am not sure what is the relation to my code problem.

    Please advise.

    Thanks
    Sue

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > I have read the link you suggested, but am not sure what is the relation to my code problem.
    That was in relation to any code. Speficially anything that's longer than Hello World. Shoot, any code at all.

    [ c o d e ] - Starting tag

    // Comments

    [ / c o d e ] - ending tag

    Type the tags with no spcaing...

    Code:
        // Comments
    The world is waiting. I must leave you now.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    read binary file

    Thanks Salem and Shadow,

    I'll try again:

    This is the code I wrote to create the file.


    Code:
    #include <sys/types.h> 
    #include <sys/stat.h> 
    #include <stdio.h> 
    #include <string.h> 
    #include <time.h> 
    
    
    #define NAMESIZE 256 
    #define TIMESIZE 50 
    
    typedef char name_type[NAMESIZE + 1]; 
    typedef char time_type[TIMESIZE +1]; 
    
    typedef struct 
    { 
    name_type name; /*File or directory name*/ 
    u_short mode; /*protection and file type*/ 
    short user_id; /*User ID of owner*/ 
    short group_id; /*Group ID of owner*/ 
    off_t size; /*file size in bytes*/ 
    time_type time_last_mod; /*modify time as string*/ 
    } file_sig; 
    
    
    main(int argc, char *argv[]) 
    { 
    
    struct stat file_info; /* define the stat variable*/ 
    file_sig out_file; /* define the output file variable*/ 
    
    name_type in_file; /* define the input file variable*/ 
    
    
    // The file pointers 
    FILE *fp,*fp1; /*The output file*/ 
    
    // open the input and output files 
    fp = fopen("/home/spaterniti/wrkshops/filelist", "r"); 
    fp1 = fopen("/home/spaterniti/wrkshops/b1", "wb"); 
    
    
    // while there are records read the text file 
    // and collect the stat information for the selected file 
    // and write a modified stat record to the output file 
    
    while (!feof(fp)) 
    { 
    fscanf(fp, "%c", &in_file); 
    stat(in_file, &file_info); 
    
    
    // create the output data fields from the stat'd data 
    
    strcpy(out_file.name, in_file); 
    
    out_file.mode = file_info.st_mode; 
    out_file.user_id = file_info.st_uid; 
    out_file.group_id = file_info.st_gid; 
    out_file.size = file_info.st_size; 
    strcpy(out_file.time_last_mod, ctime(&file_info.st_mtime)); 
    
    // read each record from the list file 
    // and collect and store the stat information 
    fwrite(&out_file, sizeof(file_sig),1,fp1); 
    } 
    
    
    // close the files 
    fclose(fp); 
    fclose(fp1); 
    
    exit(0); 
    }
    Below is the code I am trying to get to read the file, and then re "stat" each file to collect the current data. Then compare the saved stat info with the current stat info and display any discrepancies.

    But the code (which compiles) throws a segmentation fault.

    Code:
    #include <sys/types.h> 
    #include <sys/stat.h> 
    #include <stdio.h> 
    #include <string.h> 
    #include <stdlib.h> 
    #include <time.h> 
    
    
    
    #define NAMESIZE 256 
    #define TIMESIZE 50 
    
    
    typedef char name_type[NAMESIZE +1]; 
    typedef char time_type[TIMESIZE +1]; 
    
    
    typedef struct 
    { 
    name_type name; 
    u_short mode; 
    short user_id; 
    short group_id; 
    int size; 
    time_type time_last_mod; 
    }file_sig; 
    
    main() 
    { 
    
    struct stat file_info; 
    file_sig in_file_record; 
    
    FILE *fp; 
    
    fp = fopen("home/spaterniti/wrkshops/b1","rb"); 
    
    
    // read the input file 
    while (!feof(fp)) 
    { 
    fread(&in_file_record, sizeof(file_sig), 1, fp); 
    
    // get the new stat info for the file name just read 
    stat(file_sig.name, &file_info); 
    
    // compare the fields 
    if (in_file_record.mode == file_info.st_mode) { 
    printf("the modes match"); 
    } 
    
    } 
    fclose(fp); 
    exit(0); 
    }
    hope you can give me some pointers.
    Thanks
    Sue

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    Well your first step is wrong - you're only reading single chars, not whole filenames

    while (!feof(fp))
    {
    fscanf(fp, "%c", &in_file);

    Search for my previous posts on discussions about feof


    Try this

    Code:
    while ( fscanf(fp, "%s", in_file) != EOF ) {
      stat(in_file, &file_info); 
    
      // create the output data fields from the stat'd data 
      strcpy(out_file.name, in_file); 
    
      out_file.mode = file_info.st_mode; 
      out_file.user_id = file_info.st_uid; 
      out_file.group_id = file_info.st_gid; 
      out_file.size = file_info.st_size; 
      strcpy(out_file.time_last_mod, ctime(&file_info.st_mtime)); 
    
      // read each record from the list file 
      // and collect and store the stat information 
      fwrite(&out_file, sizeof(file_sig),1,fp1); 
    }

    Similarly, the second program should be
    Code:
    while ( fread(&in_file_record, sizeof(file_sig), 1, fp) == 1 ) {
      // get the new stat info for the file name just read 
      stat(file_sig.name, &file_info); 
      // compare the fields 
      if (in_file_record.mode == file_info.st_mode) { 
        printf("the modes match"); 
      } 
    }

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    segmentation fault in reading binary file pgm

    Salem,
    thanks - I have made your suggested changes.
    The first program compiles and runs without error.

    The second program compiles - but throws a segmentation fault error when it runs.

    Can you suggest why - or where I should look?

    Many thanks

    Sue

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    more info on seg. fault

    I have managed to get some more information on my segmentation fault.

    the error message is

    home/spaterniti/wrkshops/b1: no such file or directory

    I can see the file when I do a "ls" command and the authorities are rwx for everyone.....

    I have checked the spelling and the directory paths....

    Can you suggest why??

    Thanks
    Sue

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: more info on seg. fault

    Originally posted by Sue Paterniti

    home/spaterniti/wrkshops/b1: no such file or directory
    Do you need a leading / ?
    -> /home/spaterniti/wrkshops/b1
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Read in binary file ( pointers )
    By Giant in forum C Programming
    Replies: 41
    Last Post: 06-23-2005, 04:54 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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