在C 中写入输入和输出文件
Writing to input and output files in c++
我无法使我的代码编译,因为它一直告诉我"错误:呼叫无匹配的函数" 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());
由于filename
是std::string
,并且fstream::open
将const char* filename
作为参数。
调用string:c_str
从std::string
返回const char*
。
C 11不需要此,因为fstream::open
也超载以服用std::string
。用-std=c++11
标志编译以启用C 11。
ps:为什么不参加std :: fstream课程参加std :: string?(pre-c 1)
相关文章:
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- VI工作室代码错误无法打开输出文件主.exe
- 在代码块中查找输出文件
- 将未签名的字符写入输出文件
- 为什么我的代码不写入输出文件?
- 完成搜索任务时为空输出文件
- 如何将地图<列表<string>、矢量<string>>输出到输出文件?
- 无法在代码块 IDE 中打开输出文件.exe
- C++:更改 IF 语句中的输出文件
- 编译 gsoap 输出文件 (onvif) 时出现问题
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- while 循环中的流输出文件() 通常生成空文件
- 如何使用 Docker 访问输出文件
- 提升进程间共享内存删除、权限和输出文件
- 查找素数,然后将质数放入输出文件中
- GDB / C++ - 根据循环条件将多个变量记录到输出文件中
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 我的输出文件未显示解决方案
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- C++,我正在尝试创建一个有效的.bmp输出文件