a. In line 24 and 25 the program is reading from the command line a specified filename. What should i do if i have mistyped the filename (ie the specified filename can't be found. then what will happen? )
b. How can i improve the program to check that the filename exists before continuing its operation (specified in line 26). what should be its pseudocode?
c. using awhile loop instead of an if ... else statement.how to loop until a correct filename is entered
Code:
1. #include <stdio.h> 2. #include <string.h> 3. #include <sys/types.h> 4. #include <sys/stat.h> 5. #include <fcntl.h> 6. #include <sys/file> 7. #include <unistd.h> 8. #include <stdlib.h> 9. #include <time.h> 10. typedef struct { 11. char name[256]; 12. u_short mode; 13. short user_id; 14. short group_id; 15. off_t size; 16. char time_last_modified[50]; 17. } file_data; 18. int main(){ 19. char file_name[2]; 20. struct stat file_info; 21. file_data file_dat; 22. pritf("Enter file name: "); 23. scanf("%d", file_nam); 24. stat(file_name, &file_info); 25. strcpy(file_dat.name,file_name); 26. file_dat.mod = file_info.st_mode; 27. file_dat.user_id = file_info.st_uid; 28. file_dat.group_id = file_info.st_gid; 29. file_dat.size = file_info.st_size; 30. strcpy(file_dat.time_last_modified,ctime(&file_info.st_mtime)); 31. printf("File name: %s \n", file_dat.name); 32. printf("File mode: %o \n", file_dat.mode); 33. printf("File user id: %d \n", file_dat.user_id); 34. printf("File group id: %d \n", file_dat.group_id); 35. printf("File size: %d \n", file_dat.size); 36. printf("File last modified: %s", file_dat.time_last_modified);



4Likes
LinkBack URL
About LinkBacks


