Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help with command line arguements in C++

  • 05-02-2009 2:04am
    #1
    Registered Users, Registered Users 2 Posts: 7,262 ✭✭✭


    Assignment due tomorrow, and of course im leaving it till last second :)

    I'm trying to pass the name of a file I want to read from and a file to write to through the command line but I'm having no luck. My code works fine if i dont pass in anything.

    It doesnt behave correctly no matter what is passed in. It does create a file, but it uses the first argument

    I'm adding the arguments in using Visual studios project properties. Just to rule out me doing something stupid arguments are separated by spaces right?

    here is my source: problem areas highlighted in red
    #include "stdafx.h"
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    [COLOR="red"]void main (int argc, char *argv[])[/COLOR]
    {
    	string userInput = "";
    	string data[99];
    	int i = 0;
    	int arraySize = 0 ;
    
    	ifstream infile; 
    [COLOR="Red"]	if (argv[0] != NULL)
    	{
    		infile.open(argv[0]);
    	}
    	if (!(infile.is_open()))[/COLOR]
    	{
    		cout << "Invalid file name passed in" << endl;
    		do
    		{
    			cout << "Enter the name of the file to be sorted. (NOTE: Remember the file extension):";
    			getline(cin, userInput);
    			cout << endl;
    			infile.open(userInput.c_str());
    			if (!(infile.is_open()))
    			{
    				cout << "Invalid file name" << endl;
    			}
    		}while(!(infile.is_open()));
    	}
    	
    
    	for (i = 0; (! infile.eof() ); i++)
    	{
    		getline(infile, data[i]);
    		arraySize += 1;
    		
    	}
    
    	infile.close();
    
    	sort( data, data + arraySize );
    
    
    	ofstream outfile;
    	[COLOR="red"]if (argv[1] != NULL)
    	{
    		outfile.open(argv[1]);
    	}
    	if (!(outfile.is_open()))[/COLOR]
    	{
    		cout << "Invalid file name passed in" << endl;
    		do
    		{
    			cout << "Enter a name for the sorted file. (NOTE: Remember the file extension):";
    			getline(cin, userInput);
    			cout << endl;
    			outfile.open(userInput.c_str());
    			if (!(outfile.is_open()))
    			{
    				cout << "Invalid file name" << endl;
    			}
    		}while(!(outfile.is_open()));
    	}
    	for (i = 0; i < arraySize; i++)
    	{
    		outfile << data[i];
    		if (arraySize != i+1){
    		outfile << endl;
    		}
    	}
    	
    	outfile.close();
    	
    	return;
    }
    

    Any advice would be great

    Thanks

    PS sorry for the lack of comments I'm in the middle of doing it now :)


Comments

  • Registered Users, Registered Users 2 Posts: 11,198 ✭✭✭✭Crash


    argv[0] contains the name of the program, iirc. argv[1] will contain the name of the file you specify.


  • Registered Users, Registered Users 2 Posts: 7,262 ✭✭✭witnessmenow


    Works perfect ,Thank you so much man! now i can finally go to sleep :)

    EDIT: Oh for the love god. Now not putting command line arguemnts isnt working!! It makes no sense to me what so ever!

    when it gets to the sort line:
    Unhandled exception at 0x7855d815 in OOP-lab2.exe: 0xC0000005: Access violation reading location 0x445c3a65.
    

    but it only happens if i enter the filename manually,

    The thing is its reading the file fine because i can cout it fine.


  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    Looks like you're reading past the end of an array.

    If you pass no command line arguments, what should the application do? I'd imagine from my understanding just quit gracefully.

    Are you using the argc variable to determine the number of args or are you just using values in argv. Normally argc is easier to use as a guard against determining this.

    eg.
    if (argc != 2) {
    // ERROR we expect 2 args, so report error and gracefully quit.
    exit(-1);
    }


  • Hosted Moderators Posts: 7,486 ✭✭✭Red Alert


    Technically you'll be doing an out-of-bounds access if there's no second parameter. You could use getopt() but I think the Visual studio compilers won't do it.


  • Registered Users, Registered Users 2 Posts: 195 ✭✭caffrey


    have you tried using getopt? it is very handy for command line options. I suggest if you will be using command line options over the forseeable future you should get this licked and then you wont ever have problems. well thats how it worked out for me.


  • Advertisement
Advertisement