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