Ofstream file with argv

closed account (N8RzwA7f)
Hi,
I'm trying to pass a command line argument file from main to a class function and use an ofstream object to open a file (argv). I added the file to
the run command (i'm using netbeans), and I'm looking in the active directory.
This is not working? I obviously skipped huge bits of code , but it would be too long.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//main.cpp 
 ...
 if(argc != 2) {
     //error
 }
 order.newOrder(thisOrder,&argv[1]);
 ...
//newOrder.cpp
void Order::newOrder(std::vector <food*>& nOrder,char* argv[]){
    std::ofstream OutFile;
    OutFile.open(argv[1],std::ios::out | std::ios::app);
    if(!OutFile.bad()){
       OutFile <<....<< std::endl;
    ...
}


thanks :)
Ok,
On line 6 you wrote:
order.newOrder(thisOrder,&argv[1]);
and on line 9 you say:
void Order::newOrder(std::vector <food*>& nOrder,char* argv[])
Evidently you are trying to pass an array using the address operator(&). You declare Order::newOrder to take as a second parameter an array of chars. They are two incompatible types. Instead of having the function take an array of pointers, try:
line 6 order.newOrder(thisOrder,argv[1]);
and on line 9 void Order::newOrder(std::vector <food*>& nOrder,string argv)
and on line 11 OutFile.open(argv,std::ios::out | std::ios::app);
Does this answer your question?
Last edited on
closed account (N8RzwA7f)
That confuses me a bit that what I had was so wrong.
I thought that I was passing a pointer to an array of char on line 9 and
the address of the array on line 6. The same as if a pointer is passed by reference?

I tried your changes and I now got all kinds of errors?
You can't "pass a pointer by reference". Please post the errors you are getting.
Thank you!
closed account (N8RzwA7f)
no I think I meant passing by reference using pointers. It's all confusing.
I think the compiler was complaining that I tried to convert char* to string : ?


In file included from newOrder.cpp:6:0:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\fstream:713:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s,
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\fstream:713:7: note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'
Yes, that's it. Just change the argument from char* to string and you should be good to go.
Good luck!
closed account (N8RzwA7f)
Tried that (casting which is bad I hear) , and it's still not working :(
What errors are you getting?
You could use string as the parameter, but it's ok to use a char * too.
A very simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

void writefile(const char * fname)
{
    std::ofstream fout (fname, std::ios::app);
    if (!fout)
    {
        std::cout<<"Problem opening file: " << fname << "\n";
        return;
    }

    fout << "Another line\n";
}

int main(int argc, char* argv[])
{
    if (argc < 2)
        return 0;
    
    writefile(argv[1]);    
}

closed account (N8RzwA7f)
thanks it works now :)
Topic archived. No new replies allowed.