Passing arguments to function...

This is a discussion on Passing arguments to function... within the C++ Programming forums, part of the General Programming Boards category; i'm using the arguments to main and then passing them to another function to open a file with the argument ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Question Passing arguments to function...

    i'm using the arguments to main and then passing them to another function to open a file with the argument as the file name...this is the code:
    Code:
    int main(int argc, char* argv[])
    {
         char *fname;
         fname = argv[1];
         readFile(fname);
    }
    this is the readFile function:
    Code:
    void readFile(char *file)
    {
         ifstream fin (file);
    
    }
    it doesn't work...what am i doing wrong? i've only given the pertinent code...

    Regards,

    Farooq
    Last edited by alvifarooq; 09-24-2004 at 11:24 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    You need an actual ifstream variable. You should also make sure to check that you have the correct number of command line arguments:

    Code:
    #include <fstream>
    #include <iostream>
    
    void readFile(char *filename)
    {
        std::ifstream input(filename);
        // Do stuff with input file stream
    }
    
    int main(int argc,char *argv[])
    {
        if( argc != 2 )
        {
            std::cerr << "Error: Incorrect number of command-line arguments." << std::endl;
            return 1;
        }
        else readFile(argv[1]);
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    still doesn't work...it hangs on execution...didn't i do pretty much the same thing in my code?

    thanks

    Farooq

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    #1) place a cout << fname << endl; after the line fname = argv[1]; to be sure you have valid information in fname.

    #2) you need to declare an ifstream object in order to associate with a file using the () operator, say maybe.

    ifstream fin(file)

    rather than

    ifstream(file);

    then be sure fin opened file before trying to use it.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ok i'm sorry about that ive changed the original post...that is how i've done it in my original code...ok 2 things...

    1) does argv[1] indicate the first or second argument to main...i mean when i run my program (astar) i want to write:

    astar inputfile

    where it is understood that inputfile is the name of the file...so is that argv[0] or argv[1]...i tried argv[0] and i get a segmentation error...argv[1] doesn't work as it hangs...

    2) if i hardcode the filename it always works...e.g. if i write:

    Code:
    ifstream fin ("inputfile.txt");
    this always works...i just don't want to hardcode the name of the file...

    Regards,

    Farooq

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    argv[1] should be inputfile in your example. check it out by running the program with the command line arguments you have listed. comment out the call to readfile() and put a cout << argv[1] << endl; in the program to prove it. Put in a cout << fname << endl; after you assign argv[1] to fname to be sure you have the same information int fname.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    You'll need to type in astar inputfile.txt and not astar inputfile when you run the program if you want to mimic that hardcoded ifstream fin("inputfile.txt"); piece of code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ya it works when i use:

    astar inputfile.txt

    is there someway where i can just make it: astar inputfile

    i really don't know if it will be a text file or what...in fact the assignment question just says it will be run as:

    astar inputfile

    Regards,

    Farooq

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you could provide code to append the .txt string on to argv[1].

    you could ask user to enter extension type of the file and append it to the end of argv[1].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Passing arguments to another function
    By Wiretron in forum C Programming
    Replies: 2
    Last Post: 12-24-2006, 04:57 AM
  5. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 05:16 PM

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