Thread: Capturing file stat information

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

    Capturing file stat information

    I am trying to complete a uni assignment - which is to capture the stat information of a file, save the stat info to a file - and then later retreive the information and compare it the the current stat info - to see if there have been any changes to the original file stat info.

    So far I have the following:

    #include <sys/type.h>
    #include <sys/stat.h>
    #include <stdio.h>

    main()
    {
    struct stat file_info;
    FILE *fp;

    fp = fopen("myfile", "w");

    stat("testfile", &file_info);
    fprintf(fp, %d\n", file_info);

    fclose(fp);
    exit(0);
    }

    I think there are problems with the fprintf statement - I am not sure about the %d (as I guess the info is not all integers) - but an not sure what to put there in order to capture the entire stat information into myfile.

    I would appreciate any help...

    Many thanks
    Sue

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Depends if you want to be able to read the data file with a text editor when you're done

    If you want to read it, do this
    fprintf(fp, "%d\n", file_info.st_size);
    fprintf(fp, "%d\n", file_info.st_mode);
    // etc, for all the members of the structure


    Or if you just want to save the raw info, try
    fp = fopen("myfile", "wb"); // "b" for binary
    fwrite( &file_info, sizeof(file_info), 1, fp );

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

    More on stat

    Thanks Salem,

    I would like to save the info as binary - and then later read the file - and display the contents to screen - just to verify I have picked up something at all!

    I have tried to use fread(&file_info, sizeof(file_info),1,fp) to read my captured info - but the compiler advises that it doesn't know the size of file_info - can anyone suggest how I would determine this - without hard coding the size?

    Thanks
    Sue

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Try using sizeof(struct stat). This should work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Error when loading 3ds file
    By The Wazaa in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2006, 08:27 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM