Thread: Help using argc and argv[]

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    14

    Help using argc and argv[]

    Hi all, I'm getting a MS visual C++ Runtime Library error when I run this I just want it to print the error when no arguments are put forward or just the return key is pressed after calling the program on the cmd line. Have I misunderstood what cerr is? whats up with the Runtime error?

    Many thanks in advance,
    hewlettuser.

    code:

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	char ch;		
    	fstream file;		
    
    
    	if(argc == 1 || argv[1] == "\n")
    	{
    		cerr << "stats: No filename specified"
    		<<endl << endl;
    			return 1;	//Return code
    	}
    return 0;

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    You can't use == to compare two C-style strings. For == to work, one of the strings must be a C++ string. To compare C-style strings, use strcmp (from <cstring>). Alternatively, make one of the strings a C++ string. Or, since you're really only comparing a single character, you could use == to compare the character.
    Code:
    // compare as C-strings (include <cstring>)
    if (strcmp(argv[1], "\n") == 0)
    
    // compare as C++-strings (include <string>)
    if (argv[1] == string("\n"))
    //or
    if (string(argv[1]) == "\n")
    //or
    if (string(argv[1]) == string("\n"))
    
    // compare a single character
    if (argv[1][0] == '\n')
    // this is not exactly the same since it's possible that the next character
    // is not '\0', so to be exactly the same it would need to be
    if (argv[1][0] == '\n' && argv[1][1] == '\0')
    However, it's unusual (though not impossible) that a command-line string will consist solely of a newline character.

    It's best to just assume it's a good filename if the parameter is present, then try opening the file and check for errors there.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        if (argc != 2)
        {
            cerr << "Usage: stats filename\n";
            return 1;
        }
    
        fstream f(argv[1]);
        if (!f) {
            cerr << "stats: Can't open file " << argv[1] << '\n';
            return 1;
        }
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    14
    Thanks algorism, that helped a great deal. I thought I'd post the following here instead of making a new thread as its related:

    I'm confused as to why an extra character is returned in the while loop. My example: I have a .txt file which contains the words "Lets go to shop". However the program prints "Lets go to shopp" with two "p" 's when I open it using the program. Can anyone please explain this?

    Here's the part of the code which concerns it:

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
        char ch;        //character read
        fstream file;        //Declare a file
    
    
        file.open(argv[1], ios::in);        //Open input file
    
    
        while (!file.eof())        //End of file?
        {
            file.get(ch);
            cout << ch;
        }
        return 0;
    }
    Thanks again in advance
    Attached Images Attached Images Help using argc and argv[]-capture-png 
    Last edited by hewlettuser; 02-27-2017 at 02:53 AM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The last character is being printed twice because you're not checking for the end of the file properly. The end-of-file condition is detected by the get method not the eof method, i.e., the eof method does not "look ahead" to see the end of the file; instead it can only report that the end of the file has already been hit by an input method like get. In your code when get hits the end of the file you still print out ch, which contains the character that was read during the previous iteration.

    It's best not to use the eof method and instead control the loop with the function you use to read from the file:
    Code:
        while (file.get(ch))
        {
            cout << ch;
        }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,164
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com
    It's because eof is a state based on the result of the previous input operation.

    That is, if your file has 10 characters, and you read 10 characters, then eof will still be false.
    It's only after you attempt to read the 11th character that the read will fail, and eof will become true.
    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
    Feb 2017
    Posts
    14
    Thank-you for all your amazing feedback, its helped me a great deal, cheers.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    I would even recommend if you do any changes to the arguments that you create a vector of strings and modify that. This way the arguments are effectively constant in argv.
    Code:
    #include <vector>
    #include <string>
    
    std::vector<std::string> args(argv, argv + argc);
    Last edited by whiteflags; 03-18-2017 at 11:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv and argc
    By antros48 in forum C Programming
    Replies: 19
    Last Post: 09-30-2011, 08:26 AM
  2. how to use argc and argv
    By Salahuddin in forum C Programming
    Replies: 19
    Last Post: 09-09-2011, 03:53 AM
  3. argc, **argv in C++.
    By apacz in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2007, 12:09 AM
  4. argc and argv
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 05:21 PM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM

Tags for this Thread