Thread: Error in couple of lines of code and dont know how to add while loop??

  1. #1
    Registered User
    Join Date
    May 2012

    Error in couple of lines of code and dont know how to add while loop??

    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 a
    while loop instead of an if ... else to loop until a correct filename is entered

    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_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",;
    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);

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Are you, by chance, aspiring to participate in the The International Obfuscated C Code Contest ?

    Seriously, format and indent the code, before posting.
    (Oh, and remove the line numbers ).
    Last edited by manasij7479; 06-17-2012 at 12:10 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    i just put the line numbers to because one can easily identify the line where the problem is..
    can you help me with this please?

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Not unless you do what manasij asked you to do. If we're going to help you, the least you can do is to make it as easy as possible for us to help you. That includes properly formatting your code.

    (Oh, and if you use the code tags properly, it'll automatically show the line numbers).

  5. #5
    Registered User
    Join Date
    May 2012
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h> 
    #include <sys/stat.h> 
    #include <fcntl.h> 
    #include <sys/file> 
    #include <unistd.h> 
    #include <stdlib.h> 
    #include <time.h> 
    typedef struct { 
    char name[256]; 
    u_short mode; 
    short user_id; 
    short group_id; 
    off_t size; 
    char time_last_modified[50]; 
    } file_data; 
    int main(){ 
    char file_name[2]; 
    struct stat file_info; 
    file_data file_dat; 
    pritf("Enter file name: "); 
    scanf("%d", file_nam); 
    stat(file_name, &file_info); 
    file_dat.mod = file_info.st_mode; 
    file_dat.user_id = file_info.st_uid; 
    file_dat.group_id = file_info.st_gid; 
    file_dat.size = file_info.st_size; 
    printf("File name: %s \n",; 
    printf("File mode: %o \n", file_dat.mode); 
    printf("File user id: %d \n", file_dat.user_id); 
    printf("File group id: %d \n", file_dat.group_id); 
    printf("File size: %d \n", file_dat.size); 
    printf("File last modified: %s", file_dat.time_last_modified); 

  6. #6
    Registered User
    Join Date
    May 2012
    Can you help me now please?

  7. #7
    Registered User
    Join Date
    Dec 2011
    You will probably start receiving help when your code is properly indented. At the moment you have colorful obfuscated code.
    As far as if a file exists, what have you read about the return value of stat function?

  8. #8
    Registered User
    Join Date
    May 2009
    Post your compiler errors/warnings.

    After fixing your headers for my system I got this.

    NOTE: Using the wrong spelling of the name for a variable or function does NOT work in [nearly] all programming languages.
    NOTE2: C is case sensitive; I did not see any case sensitive issues; but, I really did not look for them.

    -------------- Build: Debug in testc ---------------
    H:\SourceCode\Projects\TestProjects\testc\main.c: In function 'main':
    H:\SourceCode\Projects\TestProjects\testc\main.c:26:1: warning: implicit declaration of function 'pritf' [-Wimplicit-function-declaration]
    H:\SourceCode\Projects\TestProjects\testc\main.c:27:13: error: 'file_nam' undeclared (first use in this function)
    H:\SourceCode\Projects\TestProjects\testc\main.c:27:13: note: each undeclared identifier is reported only once for each function it appears in
    H:\SourceCode\Projects\TestProjects\testc\main.c:30:9: error: 'file_data' has no member named 'mod'
    H:\SourceCode\Projects\TestProjects\testc\main.c:39:1: warning: format '%d' expects argument of type 'int', but argument 2 has type 'off_t' [-Wformat]
    H:\SourceCode\Projects\TestProjects\testc\main.c:41:1: warning: control reaches end of non-void function [-Wreturn-type]
    Last edited by stahta01; 06-17-2012 at 01:59 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > char file_name[2];
    This is only useful for single character filenames.

    > pritf("Enter file name: ");
    Does this even compile?
    Can you spell printf ?

    > scanf("%d", file_nam);
    1. %d is not how you read a string
    2. everywhere else, you write file_name
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple questions/Huffman code
    By Cpro in forum C++ Programming
    Replies: 11
    Last Post: 07-02-2008, 01:18 PM
  2. i dont understand this loop and function
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 03-20-2008, 11:36 PM
  3. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  4. Math function code - couple probs, tips?
    By Captain Penguin in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2002, 09:54 AM
  5. Couple Code Snippets
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-22-2002, 02:23 AM