How to access stat() struct

I have used fwrite() to send the stat() struct to another file. There will be several files appened together in that file. When extracting that other file how do I go about accessing the stat struct? Or do I have to take the data I want from the stat struct fwrite() to other file and then parse it? Any help would be greatly appreciated.

Here is what I have done with the stat struct -
1
2
3
4
5
6
7
8
42                         fp = fopen(argv[i],"r");
 43                         in_File = (inFile *) malloc(sizeof(inFile));
 44                         printf("%s\n%ld\n","this is size - ",sb.st_size);
 45                         in_File->Contents = "test";
 46                         printf("%s",argv[i]);
 47                         fwrite(&sb,sizeof(sb),1,stdout);
 48                         printf("%s\n",in_File->Contents);

I write to standard out and it is redirected to other file.
Last edited on
closed account (2UD8vCM9)
Can you post the related code? You're talking about saving a stat struct, but I don't see a stat struct in the code you posted.
You're right, sorry.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
8 struct stat sb;
  9 typedef struct{
 10         char *Name;
 11         char *Contents;
 12 }inFile;
 13 
 14 inFile *in_File;
 15 int numArgs;
 16 FILE *fp;
 17 char test[75];
 18 char tar[3];
 19 
 20 int main(int argc, char **argv, char **envp){
 21                         //Read in first few chars
 22                         fp = fopen(argv[1],"r");
 23                         stat(argv[1],&sb);
 24                         printf("%s\n%ld\n","this is size - ",sb.st_size);
 25                         in_File = (inFile *) malloc(sizeof(inFile));
 26                         fwrite(&sb,sizeof(sb),1,stdout);
 27                         fseek(fp,70, SEEK_SET);
 28                         fread(&test,75,1,fp);
 29                         printf("%s",test);
 30         
 31         if(1==1){
 32                 printf("%s\n","this and that yay");
 33 }else{  
 34         //Error check argv arrays.
 35         if(argv[1] == NULL){
 36                 printf("%s\n","usage: ./my_tar option [files . . .]\n\t\toption = -c | -x "); 
 37                 exit(1);
 38         }
 39         
 40         //Check files to see if they are regular.
 41         for(int i = 1; i < argc; i++){
 42                 if(stat(argv[i],&sb) != -1  && (S_ISREG(sb.st_mode))){
 43                         fp = fopen(argv[i],"r");
 44                         in_File = (inFile *) malloc(sizeof(inFile));
 45                         printf("%s\n%ld\n","this is size - ",sb.st_size);
 46                         in_File->Contents = "test";
 47                         printf("%s",argv[i]);
 48                         fwrite(&sb,sizeof(sb),1,stdout);
 49                         printf("%s\n",in_File->Contents);
Topic archived. No new replies allowed.