command line input problem

This is a discussion on command line input problem within the C Programming forums, part of the General Programming Boards category; I am getting a "memory could not be written" when gets for file-out name. When a user only enters file-in ...

  1. #1
    Unregistered
    Guest

    Question command line input problem

    I am getting a "memory could not be written" when gets for file-out name. When a user only enters file-in name, the program prompts for output file name. When user enters it, it gets the string and puts it in argv[2] and this is where I get the error.



    void
    main(int argc, char *argv[])
    {
    char fileNameIn[20];
    char fileNameOut[20];

    if(argc == 1) // only the program name is input
    {
    printf("\n Usage: pgm10 input_fileName output_fileName\n");
    exit(1);
    }

    if (argc == 2) // two strings input(pgm10 and input
    // fileName); output fileName is missing
    {
    fflush(stdin);
    printf("\n Please enter output file name: ");
    gets(argv[3]);
    }

    printf("file name in = %s\n", argv[2]);
    printf("file name out = %s\n", argv[3]);

    }

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    if (argc == 2)

    gets(argv[3]);
    Not sure if I read your code right, hard to read like it is use code tags.
    if argc is 2, then there isnt a argv[3], the highest argv will be argv[1](remember array elements start at 0).
    if you try and assign data to argv[3] you are trying to access an area of memory that hasn't been initialised, so you get a memory error.
    I think thats your problem or part of it. hope that helps some.
    Last edited by C_Coder; 12-24-2001 at 05:16 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Unregistered
    Guest

    Question

    Thanks for your help. If the user only inputs program name and filein name (argc =2), I had to prompt him for fileout name and store it in a different variable, not argv[2]).

    This is what I had to do. If you can think of better way, please let me know.

    void
    main(int argc, char *argv[])
    {
    char fileNameIn[20];
    char fileNameOut[20] = { "\0" };


    if(argc == 1) // 0 & 1; only the program name is input
    {
    printf("\n Usage: pgm10 input_fileName output_fileName\n");
    exit(1);
    }

    if (argc == 2) // two strings input(pgm10 and input
    // fileName); output fileName is missing
    {
    printf("\n Please enter output file name: ");
    fflush(stdin);
    gets(fileNameOut);
    }
    strcpy (fileNameIn, argv[1]);

    printf(" argc = %d\n", argc);
    printf(" Program name = %s\n", argv[0]);
    printf(" file name in = %s\n", fileNameIn);

    if (argv[2] != NULL)
    printf(" file name out = %s\n", argv[2]);
    else
    printf(" file name out = %s\n", fileNameOut);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 09:38 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 10:34 PM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 03:44 PM

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