Like Tree4Likes
  • 2 Post By manasij7479
  • 1 Post By stahta01
  • 1 Post By Salem

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

This is a discussion on Error in couple of lines of code and dont know how to add while loop?? within the C Programming forums, part of the General Programming Boards category; a. In line 24 and 25 the program is reading from the command line a specified filename. What should i ...

  1. #1
    AG1
    AG1 is offline
    Registered User
    Join Date
    May 2012
    Posts
    12

    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 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);

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    antred and mariostg like this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    AG1
    AG1 is offline
    Registered User
    Join Date
    May 2012
    Posts
    12
    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
    Location
    Germany
    Posts
    228
    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
    AG1
    AG1 is offline
    Registered User
    Join Date
    May 2012
    Posts
    12
    Code:
    #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); 
    strcpy(file_dat.name,file_name); 
    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; 
    strcpy(file_dat.time_last_modified,ctime(&file_info.st_mtime)); 
    printf("File name: %s \n", file_dat.name); 
    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
    AG1
    AG1 is offline
    Registered User
    Join Date
    May 2012
    Posts
    12
    Can you help me now please?

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    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
    Posts
    2,552
    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.


    Code:
    -------------- 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.
    Salem likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > 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
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21