-
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
-
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.
-
> 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;
}