2016-09-16 8 views
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"); 
} 

내가 겪고있는 문제는 우분투 터미널에서 실행할 때 일반적으로 out.open()이 문자열을 가져 오지 않는다고 말하는 오류가 발생한다는 것입니다. 이름.C++에서 변수에 이름을 전달하여 파일을 여는 방법

파일에서 다른 파일을 작성해야합니다. 문제를 해결할 수있는 방법이 있습니까?

+2

[우분투 상자에서 C++ 11 (또는 그 이상) 툴체인 사용] (http://en.cppreference.com/w/cpp/io/basic_ofstream/open) 또는'name.c_str)'보다는'name'을 사용하십시오. – WhozCraig

+0

'out.open (name.c_str())'시도해 보셨습니까? –

답변

0

C++ 03에서 ofstream::open()std::string이 아니라 const char*을 취합니다. http://www.cplusplus.com/reference/fstream/ofstream/open/을 참조하십시오. C++ 11 (gcc -std=c++11)을 사용할 수 있다면 std::string을 전달할 수 있습니다.

+0

Windows와 Linux 모두에서 동일합니까? –

+0

예, 컴파일러가 표준을 따르는 경우 (이 경우 가장 일반적인 컴파일러가 될 것입니다). – rainer

+0

이 코드는 Visual Studio에서 잘 작동하지만 리눅스 터미널에는 없습니다 –