0
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
#include<string.h>
#include<vector>
using namespace std;

void main()
{
    string read;
    string name;
    vector<string> myvec;
    const char * word;
    ifstream in;
    in.open("w.txt");
    ofstream out;

        getline(in,read);
        word = strtok(&read[0],",");

        while(word != NULL)
        {
            name = word;
            cout<<"Word: "<<word<<endl;
            name = name + ".txt";
            myvec.push_back(name);
            out.open(name);
            cout<<"Name: "<<name<<endl;
            word = strtok(NULL,",");
            out.close();
        }
    in.close();
    system("pause");
}

The problem I am having is when i run it in ubuntu terminal it throws an error, which generally says that out.open() will not take string name.

I have to create different files taking input from a file. So is there a way to resolve the issue?

Saqib Khan
  • 53
  • 2
  • 7
  • 2
    [Use a C++11 (or greater) toolchain on your Ubuntu box](http://en.cppreference.com/w/cpp/io/basic_ofstream/open), or just pass `name.c_str()` rather than `name`. – WhozCraig Sep 16 '16 at 15:46
  • Have you tried `out.open(name.c_str())`? – ichramm Sep 16 '16 at 15:48

1 Answers1

0

In C++03, ofstream::open() takes a const char*, not a std::string; see http://www.cplusplus.com/reference/fstream/ofstream/open/. If you can use C++11 (gcc -std=c++11), you can pass a std::string.

rainer
  • 6,769
  • 3
  • 23
  • 37
  • Is it same for both windows and linux? – Saqib Khan Sep 16 '16 at 15:50
  • Yes, if your compiler is standard compliant (which I guess most common compilers will be in this case). – rainer Sep 16 '16 at 15:51
  • this code worked fine on visual studio but not in linux terminal – Saqib Khan Sep 16 '16 at 15:54
  • I'm guessing that your Visual Studio version will default to C++11, while `gcc` usually defaults to C++03 (although 6.0 and above seem to [default to C++14](https://gcc.gnu.org/gcc-6/changes.html)). However, you won't be able to use `conio.h` in Linux, see https://stackoverflow.com/questions/8792317/why-cant-i-find-conio-h-on-linux. – rainer Sep 16 '16 at 15:56
  • ok thank you. I will just pass const char *. – Saqib Khan Sep 16 '16 at 15:58