Thread: String problem

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    32

    String problem

    I have a web interface that uses dos to start my program and sends it 2 parameters(string author, string fileName) to my main. My c++ program is not using the new standards (requirement) and does not therefore use the string class. My problem is:

    1. how do I declare these
    2. how can I access the information in them

    Code:
    #include <iomanip.h>
    
    const int MAX = 50;
    char author[MAX];
    char fileName[MAX];
    
    int main(char author[], char fileName[])
    {
        cout << author <<endl;
        cout << fileName <<endl;
       return 0;
    }
    The cout's are just outputting garbage...how and what can I do?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The main function has specific arguments that it takes based on what is passed into the command line. So your web interface must pass the information as strings to the command line (with quotes if there are spaces in the string). Then use code similar to the code below to process each command line argument. Remember that argc[0] is always the executable name, and the bold part must always be the same (basically).
    Code:
    #include <iostream.h>
    
    const int MAX = 50;
    char author[MAX];
    char fileName[MAX];
    
    int main(int argc, char *argv[])
    {
        // argv[0] is the executable name
        if (argv >= 2)
            strncpy(author, argc[1], MAX);
        if (argv >= 3)
            strncpy(fileName, argc[2], MAX);
    
        // Or ...
        for (int i = 1; i < argc; i++)
        {
            cout << argc[i] << endl;
        }
    }

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    jlou, I'm sure that you know what you are doing and that it is just a simple mistake on your part, but for the OP's benefit, I just wanted to fix the typos that may cause confusion in your program.

    To clarify: argc is the argument count and argv is the argument vector; therefore, the code above should actually be:

    Code:
    #include <iostream.h>
    
    const int MAX = 50;
    char author[MAX];
    char fileName[MAX];
    
    int main(int argc, char *argv[])
    {
        // argv[0] is the executable name
        if (argc >= 2)
            strncpy(author, argv[1], MAX);
        if (argc >= 3)
            strncpy(fileName, argv[2], MAX);
    
        // Or ...
        for (int i = 1; i < argc; i++)
        {
            cout << argv[i] << endl;
        }
    }
    -tf

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    [edit]Ooopps...froggy has just replied BUT[/edit]


    Im still confused... I MUST have (2 char array parameters - one for the authors name...the other has some file's path)
    Code:
    > int main(int argc, char *argv[])
    Do you mean char argc??? and if so, is it considered as just a variable with one character in it only???



    Code:
    >  *argv[]
    Does this have something like this contained within itself(where each line is due to a space):
    c:\my
    documents\web
    interface\generate.exe

    OR is it just c:\my documents\web interface\generate.exe



    Code:
    [I]    for (int i = 1; i < argc; i++)
        {
            cout << argc << endl;
        }
    In the loop above, argc is not an array (though I think it should be) so how does this print as though it was an array??



    Code:
        for (int i = 0; i < argv; i++)
            cout << argv[i] << endl;
    Would the above code be correct for printing argv (I dont think its right)


    your web interface must pass the information as strings to the command line (with quotes if there are spaces in the string)
    .

    Finaly, you say to put quotes if there are spaces, I think on many occassions this would be true. Would I therefore write main's parameters as:


    Code:
    int main(char "argc", char "*argv[]")
    Thank you
    Last edited by Jules; 05-18-2004 at 04:13 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int main(char "argc", char "*argv[]")
    No, he means

    myprog "this is arg1 with spaces, and its in quote" "this is arg2"
    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.

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    Sorry.....so does this mean int main(char "argc", char "*argv[]")
    is wrong. I didn't get it.


    I need to be able open a file using argv.... would I do this as

    iFile.open(argv, ios::nocreate|ios::app) and how would I a cout of what is in argv??
    Last edited by Jules; 05-18-2004 at 04:39 PM.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Thanks thefroggy, I think I need a nap.

    ----------

    Jules, argc is the number of strings on the command line and argv is an array that holds those strings.

    So, if you had this on your command line:

    myprog.exe Twain huckleberry.txt

    Then argc would be 3, because there are three separate strings there. Since argv is an array whose index starts at zero, then argv has three values:

    argc: 3
    argv[0]: myprog.exe
    argv[1]: Twain
    argv[2]: huckleberry.txt

    That is how you get the information. So no, argc is not supposed to be a char, because it is a number that tells you how many strings were on your command line. This is the general way that C and C++ programs use the command line, so it has to be able to hold any number of arguments. Just because you only want to use two arguments doesn't mean that main should take two parameters, it means that argv should hold three strings (one for the exe and two for the arguments).

    So if you wanted to open the filename, and you assume that the filename was the second argument (like huckleberry.txt in the example above), then you would use argv[2] inside your iFile.open() code and when you cout it.

    You should read up on command line arguments, your book or other online tutorials will probably have lots if onfo on it. Here is another example that is more complicated. If you can understand it, that would help:

    Command Line: a.out "Mark Twain" Huckleberry Finn data.dat 1821

    argc: 6
    argv[0]: a.out
    argv[1]: Mark Twain
    argv[2]: Huckleberry
    argv[3]: Finn
    argv[4]: data.dat
    argv[5]: 1821

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    jlou...I can not thank you enough wow...what an explanation, very clear and precise. FANTASTIC.

    Thankyou very much. Now I get it

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Whew!! I thought I screwed you up big time with the typos in the first post. Glad I made up for it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM