Thread: command line arguments options

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    command line arguments options

    Hi,
    The problems of this code are:
    Why switch(argv[1]) do not work?
    Each time I write new information to the file the previous ones are deleted.

    If you see any improvements that can be added to the code I would be grateful

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct staff{
    char name[30];
    int age;
    };
    
    int main(int argc, char *argv[])
    {
    struct staff record;
    FILE *fp;
    char pass[30];
    int i;
    
    if(!strcmp(argv[1],"-n")){
            printf("Enter apassword :");
            scanf("%s",pass);
            if(!strcmp(pass,"123"))
            {
                    fp=fopen("Staff.dat","wb");
                    for(i=1; i<=3; i++)
                    {
                            printf("Enter name, age\n");
                            fseek(fp,record.age*sizeof(struct staff),SEEK_SET);
                            scanf("%s %d",record.name,&record.age);
                            fwrite(&record,sizeof(struct staff),1,fp);
                    }
                    fclose(fp);
            }
            else printf("Error password!:");
    
    }
    
    if(!strcmp(argv[1],"-l")){
            fp=fopen("Staff.dat","rb+");
            for(i=1; i<=3; i++)
            {
                    
                    fseek(fp,record.age*sizeof(struct staff),SEEK_SET);
                    fread(&record,sizeof(struct staff),1,fp);
                    printf("%s %d\n",record.name,record.age);
            }
            fclose(fp);
    }
    
    else
            printf("Wrong command");
    
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't work because you can't use strings for cases. Switches operate on integers, not strings.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    I am sure that I read one example in C programming book uses:
    switch(grade){//grade is char
    case 'A':
    .......etc

    Ok, what about the other problems

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Switch statements are not meant to handle strings. Your use of if statements is correct here with the exception being your second condition should be an else if, not an if.

    As for it removing what's already in the file... that's what "wb" does. It truncates. You should be using "a" or "ab" in that case, if you want it to append to the end of the file there.
    Sent from my iPadŽ

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Moony
    I am sure that I read one example in C programming book uses:
    switch(grade){//grade is char
    case 'A':
    .......etc

    Ok, what about the other problems
    'A' is not a string, is it? It's a character. Characters are essentially single byte numbers.
    Sent from my iPadŽ

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Moony
    I am sure that I read one example in C programming book uses:
    switch(grade){//grade is char
    case 'A':
    So where in your C programming book did you learn that a single char is the same thing as a string?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    I tried "a","ab" but that don't work,they keep the previous values only. I tried (rb+) for both reading and writing, but still have the same problem
    Last edited by Moony; 07-02-2006 at 01:08 AM.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by quzah
    So where in your C programming book did you learn that a single char is the same thing as a string?
    I know that they are not the same but you said in your previous post that :
    Switches operate on integers, not strings.
    so I thought that it is just the case.
    My aim is to learn from you nothing more

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Look at this post and see Quzah scolding someone for not knowing a character when they see it because it looks like an integer.

    Oh and while we're at it... speaking of integers, your going to run into big problems with your program comparing argv[1], when you don't even use argc to make sure there is an argv[1].
    Last edited by SlyMaelstrom; 07-02-2006 at 01:17 AM.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    So I need to check at the beginning:
    if(argc>2)
    to make sure if there is argv[1]?

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well argv[1] is where argc == 2, so....
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Thanks a lot SlyMaelstrom

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. What's the best way to handle many program options?
    By stickmangumby in forum C Programming
    Replies: 19
    Last Post: 06-06-2007, 04:06 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM