我有一個問題。我正在用C++編寫程序,我想訪問一些文件,所以我使用fstream類。那麼,這裏都可以,但是當我編譯程序時,它會崩潰並給我一個錯誤。爲什麼我的fstream訪問不能編譯?
我把這裏我連接文件中的代碼:
#include <iostream>
#include <fstream>
#include <map>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <string.h>
using namespace std;
fstream file1, file2;
void access();
int main(){
access();
file1.close();
file2.close();
}
void access(){
file1("C:\\data\\file1.dat",ios::in | ios::out | ios::binary);
cout << "Opening File1..." << endl;
file2("C:\\data\\file2.dat",ios::in | ios::out | ios::binary);
cout << "Opening File2..." << endl;
}
,它給我下面的錯誤:
no match for call to '(std::fstream {aka std::basic_fstream<char>}) (const char [20], std::_Ios_Openmode)'
所以我試圖把這樣的方法獲得:
void access(){
string f1 = "C:\\data\\file1.dat";
string f2 = "C:\\data\\file2.dat";
file1(f1.c_str(),ios::in | ios::out | ios::binary);
cout << "Opening File1..." << endl;
file2(f2.c_str(),ios::in | ios::out | ios::binary);
cout << "Opening File2..." << endl;
}
但它也給我一個錯誤。其如下:
no match for call to '(std::fstream {aka std::basic_fstream<char>}) (const char*, std::_Ios_Openmode)'
我不知道會發生什麼,任何幫助將不勝感激。
非常感謝。