Thread: atoi question

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    atoi question

    hello,
    I have been attempting to use atoi to bring in a filename, and pass this name into a another function for it to work with......this is in main:



    int main (int argc, char *argv[])
    {

    cout<<"Please enter filename\n";

    if (argc != 1){

    cout<<"You must enter a filename\n"; }

    char filename;

    cin >> filename;

    filename = atoi(argv[1]);

    //I want this function to take this filename and use it in another
    //function:

    filereader(filename);

    }


    //this function is in a .h file , and in the function before I had to manually type in the filename, but now I want to be able to use a command argument to type in the name:

    void filereader (char infile){

    char *filein = infile; }

    I'm getting quite a few errors, first of being an implicit conversion, which does make sense but I am not quite sure what approach to use?? any help will be appreciated. thanks

  2. #2
    Unregistered
    Guest
    You are using atoi function? Why do you want to convert the filename to an integer?
    Code:
    #include <cstring>
    #include <iostream>
    
    void filereader (char *infile){
      // You could already write:
      ifstream input(infile);
      // or in C:
      FILE *input(infile);
      char filein[80];
      strcpy(filein,infile);
    }
    
    int  main (int argc, char *argv[])
    {
      cout<<"Please enter filename"<<endl;
      if (argc != 1){
        cout<<"You must enter a filename"<<endl; 
      }
      char filename[80];
      cin >> filename; 
      filereader(filename);
    }
    char character; means that you define just a one character
    char chararray[80] means what you want.
    You could also use C++ strings.

  3. #3
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    > if (argc != 1){
    > cout<<"You must enter a filename\n"; }
    This should probably be:
    Code:
    if (argc <2){
        couy<<"You must enter a filename"<<endl;
        //exit program
        return EXIT_FAILURE;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 11:55 PM
  2. atoi related question
    By The Gweech in forum C++ Programming
    Replies: 2
    Last Post: 10-01-2003, 11:14 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM