Passing in argv[] as a parameter

So I call argv[] in my main to take in input from the terminal but I can't seem to pass it to another function correctly.


For my main function I have:

1
2
3
4
5
6
7
8
int main(int argc, char *argv[]){

  // if(argc == 1){}  

  // if(argc == 2){
    readInFile(argc, argv[]);
    // }
}


for my function I have the call as:
 
void readInFile(int argc, char *argv[]){


the errors I am getting are:

lab06.cpp: In function ‘void readInFile(int, char**)’:
lab06.cpp:22:35: error: invalid user-defined conversion from ‘std::basic_istream<char>’ to ‘const char*’ [-fpermissive]
printw(getline(myfile, line));
^
In file included from /usr/include/c++/4.8.2/ios:44:0,
from /usr/include/c++/4.8.2/ostream:38,
from /usr/include/c++/4.8.2/iostream:39,
from lab06.cpp:4:
/usr/include/c++/4.8.2/bits/basic_ios.h:115:7: note: candidate is: std::basic_ios<_CharT, _Traits>::operator void*() const [with _CharT = char; _Traits = std::char_traits<char>] <near match>
operator void*() const
^
/usr/include/c++/4.8.2/bits/basic_ios.h:115:7: note: no known conversion for implicit ‘this’ parameter from ‘void*’ to ‘const char*’
lab06.cpp: In function ‘int main(int, char**)’:
lab06.cpp:32:27: error: expected primary-expression before ‘]’ token
readInFile(argc, argv[]);



readInFile(argc, argv);

All your other errors are not trelated t code you have shown.
Oh, that was a dumb mistake haha. Sorry I'm trying to learn c++ syntax coming from Java. Here's my other function
1
2
3
4
5
6
7
8
9
10
11
void readInFile(int argc, char *argv[]){
  string line;
  fstream myfile (argv[1]);
  if(myfile.is_open()){
  }else{
    cout<<"There was an error opening this file."<<endl;
  }
  while(getline(myfile, line)){
      printw(getline(myfile, line));
  }
}
printw(getline(myfile, line)); What does your printw function takes?
I'll be honest, that was something I found in the book that I was pretty skeptical of, that was more of me just trying to make stuff work. I want to be able to take in a text file specified in the terminal and just print the text to the ncurses screen.
I just realized this isn't going to work anyways, I just looked at my project again and I have to use low-level I/O to display stuff
What does it takes? How is it defined? Right now you are passing it an std::istream&/ If it needs something else, pass what it really needs.
So I changed it to attempt to open the file using low level I/O, but I still l have some errors.
Now the function is:
1
2
3
4
5
6
7
8
9
10
11
 
void readInFile(int argc, char *argv[]){
  int fd;
  char filename[] = argv;
  fd = open(filename, O_RDWR);
  if(fd != -1){
    cout<<"File opened successfully"<<endl;
  }else{
    cout<<"File could not be opened"<<endl;
  }
}


The errors I'm getting are:
lab06.cpp: In function ‘void readInFile(int, char**)’:
lab06.cpp:18:27: error: initializer fails to determine size of ‘filename’
char filename[] = argv[0];
^
lab06.cpp:18:27: error: array must be initialized with a brace-enclosed initializer
Last edited on
So I changed it to attempt to open the file using low level I/O
Why? Your code was almost fine aside from one error.

argv has the type of char**, and filename has incomplete type: (a) you must provide size of array explicitely, (b) You are trying to assign array of strings to a single string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

void read_infile( const char* path )
{
    std::ifstream file(path) ;
    if(file) std::cout << file.rdbuf() ;
    else std::cerr << "failed to open file " << std::quoted(path) << '\n' ;
}

int main( int argc, char* argv[] )
{
    if( argc == 2 ) read_infile( argv[1] ) ;
    else { /* ... */ } ;
}
Topic archived. No new replies allowed.