2013-04-16 2 views

답변

2

는 C++ 우리는과 같이 std::string를 사용할 수 있습니다

int main(int arg, char* args[]) { 
    // Open the file 
    std::string path("/home/docs/cs230/") ; 

    path+= args[1] ; 

    std::cout << path << std::endl ; 

    FILE *newfile = fopen(path.c_str(), "w+b"); 
} 

매트도 좋은 코멘트를 만드는 C++에서 우리는 fstream을 사용할 것입니다, 당신은 링크에서 더 많은 것을 읽을 수 있습니다.

1

이 C++이기 때문에, 나는이 제안 :

int main(int argc, char *argv[])  
// Please don't make up your own names for argc/argv, it just confuses people! 
{ 
    std::string filename = "/home/docs/cs230/"; 
    filename += argv[1]; 
    newfile = fopen(filename.c_str(), "w+b"); 
} 

[++ 완전히 C를 그것을 만들하지만 당신은 포인터 당신은 할 수 고수하려는 경우 fstream 아닌 FILE

0

를 사용한다 문자열 연결 (char *)

const char* path = "/home/docs/cs230/"; 
int size1 = sizeof(argv[1]); 
int size2 = sizeof(path); 
const char* result = new char[size1 + size2 + 2]; 
result[size1 + size2 + 1] = '\0'; 
memcpy(result, path, size1); 
memcpy(&result[ size1 ], argv[1], size2); 

권장 옵션은 아니지만 여기에는 여러 가지 가능성이 있습니다.

관련 문제