在C 中写入输入和输出文件

Writing to input and output files in c++

本文关键字:输出 文件 输入      更新时间:2023-10-16

我无法使我的代码编译,因为它一直告诉我"错误:呼叫无匹配的函数" 16上的"呼叫"。有建议吗?我想读取文件并将所有元音写入输出文件。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
    string filename;    // to hold the file name
    ifstream inputfile; // input to a file

    // Get the file name
    cout << "Enter a file name: ";
    cin >> filename;
    // Open the file
    inputfile.open(filename); // LINE 16
    char vowel; // to store the vowels
    ofstream outputfile; // to write to the file
    // open file
    outputfile.open("vowels_.txt");
    while(inputfile.get(vowel)){
        //If the char is a vowel or newline, write to output file.
        if((vowel == 'a')||(vowel == 'A')||(vowel =='e')||(vowel =='E')||(vowel =='i')||(vowel =='I')||(vowel =='o')||(vowel =='O')||(vowel =='u')||(vowel =='U')||(vowel =='n') && !inputfile.eof())
            outputfile.put(vowel);
    }
    inputfile.close();
    outputfile.close();

}

更改以下内容:

inputfile.open(filename);

inputfile.open(filename.c_str());

由于filenamestd::string,并且fstream::openconst char* filename作为参数。

调用string:c_strstd::string返回const char*


C 11不需要此,因为fstream::open也超载以服用std::string。用-std=c++11标志编译以启用C 11。


ps:为什么不参加std :: fstream课程参加std :: string?(pre-c 1)