这不是一个设计问题,实际上,虽然看起来像它.(好吧,好吧,这是一个设计问题).我想知道的是为什么C++ std::fstream类不会std::string在构造函数或开放方法中使用它们.每个人都喜欢代码示例:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string filename = "testfile";
std::ifstream fin;
fin.open(filename.c_str()); // Works just fine.
fin.close();
//fin.open(filename); // Error: no such method.
//fin.close();
}
Run Code Online (Sandbox Code Playgroud)
这使我一直处理文件.当然,C++库会std::string尽可能使用?
我正在尝试读取名为的文件argv[1],但我不知道我是如何做到的.感觉它很简单,编译时我得到的错误信息是,
main.cpp: In function ‘void* ReadFile(char**, int&)’:
main.cpp:43:22: error: request for member ‘c_str’ in ‘*(argv + 8u)’, which is of non-class type ‘char*’
make: *** [main.o] Error 1
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include "movies.h"
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void *ReadFile(char*[], int&);
int size;
int main(int argc, char *argv[])
{
char * title[100];
cout << argv[1] << endl;
//strcpy(argv[1],title[100]);
//cout << title << endl;
ReadFile(argv , size);
return 0;
}
void *ReadFile(char * argv[] , int& size) …Run Code Online (Sandbox Code Playgroud)