Thread: having a bit of trouble...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    36

    having a bit of trouble...

    I have run-time errors with this program and can't figure out why...basically this is program takes in files and options as its arguments, then it reads in lines, the number based on the option, of this file and prints it on the screen. I don't know why it crashes (error message i get is Segmentation fault(core dumpeD)

    here is the code...
    Code:
    #include <stdio.h>
    #include <string.h>
    #define BUFSIZE 1000
    
    int main (int argc, char**argv)
    {
            FILE *myfile;
            char line[BUFSIZE];
            int which_arg, which_char;
            char filename[BUFSIZE];
            int option = 1;
            int counter = 0;
            int numOption = 0;
    
            for (which_arg = 1; which_arg < argc; ++which_arg)
            {
                    if(argv[which_arg][0] == '-')
                    {
                            if((argv[which_arg][1] < 0) && (argv[which_arg] > 9))
                            {
                                    option = argv[which_arg][1];
                                    numOption++;
                                    if(argv[which_arg][2])
                                    {
                                            printf("You have entered an incorrect option, 1-9 only");
                                    }
                            }
                            else
                            {
                                    printf("You have entered an incorrect option.");
                            }
                            continue;
                    }
                    printf ("\nFile Argument %d is \"%s\". ", which_arg, argv[which_arg]+numOption);
                    for (which_char=0; argv[which_arg][which_char]; ++which_char)
                    {
                            filename[which_char] = argv[which_arg][which_char];
                    }
                    filename[which_char] = '\0';
                    myfile = fopen (filename, "r");
                    if (! myfile)
                            printf ("\nFile \'%s\' not found.\n", filename);
                    else
                    {
                            while(counter < option)
                            {
                                    fgets (line, BUFSIZE, myfile);
                                    printf ("Line number %s reads as follows:\n%s", counter+1 , line);
                                    counter++;
                            }
                    }
                    option = 1;
                    counter = 0;
            }
            return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if((argv[which_arg][1] < 0) && (argv[which_arg] > 9))
    If you're testing for digits, enclose the 0 and 9 each in single quotes, or use the function isdigit to test them.

    Also, you say 1-9 are valid, but you check for 0 also. So what if they enter 0?


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

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    thanks, I put in the single quotes...but i still get the Segmentation fault(core dumped) error

    any other ideas?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    I've decided to replace the if((argv[which_arg][1] < 0) && (argv[which_arg] > 9)) part with the isDigit for the argument...however the complier complains that it's never heard of isDigit...what file should i include for that?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    isdigit
    Code:
    printf ("Line number %s reads as follows:\n%s", counter+1 , line);
    Code:
    printf ("Line number %d reads as follows:\n%s", counter+1 , line);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    ah nice thanks for that

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    This is the updated version of the code after some corrections thanks to you guys...ok so the core dumps stopped, but I can't get the options to work...i do a.out -3 test.txt and it still reads only the first line....any ideas?

    Code:
    #include <stdio.h>
    #include <string.h>
    #define BUFSIZE 1000
    
    int main (int argc, char**argv)
    {
            FILE *myfile;
            char line[BUFSIZE];
            int which_arg, which_char;
            char filename[BUFSIZE];
            int option = 1;
            int counter = 0;
            int numOption = 0;
    
            for (which_arg = 1; which_arg < argc; ++which_arg)
            {
                    if(argv[which_arg][0] == '-')
                    {
                            if((argv[which_arg][1] <= '0') && (argv[which_arg] > '9'))
                            {
                                    option = argv[which_arg][1];
                                    numOption++;
                                    if(argv[which_arg][2])
                                    {
                                            printf("You have entered an incorrect option, 1-9 only\n");
                                    }
                            }
                            else
                            {
                                    printf("You have entered an incorrect option.\n");
                            }
                            continue;
                    }
                    printf ("\nFile Argument %d is \"%s\". ", which_arg, argv[which_arg]+numOption);
                    for (which_char=0; argv[which_arg][which_char]; ++which_char)
                    {
                            filename[which_char] = argv[which_arg][which_char];
                    }
                    filename[which_char] = '\0';
                    myfile = fopen (filename, "r");
                    if (! myfile)
                            printf ("\nFile \'%s\' not found.\n", filename);
                    else
                    {
                            while(counter < option)
                            {
                                    fgets (line, BUFSIZE, myfile);
                                    printf ("Line number %d reads as follows:\n%s", counter+1 , line);
                                    counter++;
                            }
                    }
                    option = 1;
                    counter = 0;
            }
            return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    actually, it doesn't even recognize the option

    when i do -7

    ithe error message You have entered an incorrect option comes up...

    thanks for all the help guys in advance

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    lol ok... i figured out that i have the signs in the comparison reversed...heres the updated code

    Code:
    #include <stdio.h>
    #include <string.h>
    #define BUFSIZE 1000
    
    int main (int argc, char**argv)
    {
            FILE *myfile;
            char line[BUFSIZE];
            int which_arg, which_char;
            char filename[BUFSIZE];
            int option = 1;
            int counter = 0;
    
            for (which_arg = 1; which_arg < argc; ++which_arg)
            {
                    if(argv[which_arg][0] == '-')
                    {
                            if((argv[which_arg][1] >= '0') && (argv[which_arg][1] < '9'))
                            {
                                    option = argv[which_arg][1];
                                    if(argv[which_arg][2])
                                    {
                                            printf("You have entered an incorrect option, 1-9 only\n");
                                    }
                                    printf("%d", option);
                            }
                            else
                            {
                                    printf("You have entered an incorrect option.\n");
                            }
                            continue;
                    }
                    printf ("\nFile Argument %d is \"%s\". ", which_arg, argv[which_arg]);
                    for (which_char=0; argv[which_arg][which_char]; ++which_char)
                    {
                            filename[which_char] = argv[which_arg][which_char];
                    }
                    filename[which_char] = '\0';
                    myfile = fopen (filename, "r");
                    if (! myfile)
                            printf ("\nFile \'%s\' not found.\n", filename);
                    else
                    {
                            while(counter < option)
                            {
                                    fgets (line, BUFSIZE, myfile);
                                    printf ("Line number %d reads as follows:\n%s", counter+1 , line);
                                    counter++;
                            }
                    }
                    option = 1;
                    counter = 0;
            }
            return 0;
    }
    Ok now the problem is, that the program doesn't know when to stop when there is no more text in the file....any ideas how to prevent this?

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    nm i got it, it was giving me ASCII values...just had to subtract 48. Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  2. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  3. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM