我只是想从文件中读取每个字符并在屏幕上打印它们.为了测试,我尝试在打印字符之前首先在控制台屏幕上打印ascii值.
我试图阅读的文件内容如下:
assign1_2.cpp:33:20: error: cannot convert 'std::string
{aka std::basic_string<char>}' to 'const char*' for argument '1'
to 'int atoi(const char*)'
Run Code Online (Sandbox Code Playgroud)
我使用下面的代码
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
void CountLetters(string filename);
int main()
{
CountLetters("count.txt");
}
void CountLetters(string filename)
{
cout << filename << endl;
ifstream in;
in.open(filename.c_str(), ios::in);
vector<char> letter;
char temp;
while (!in.eof())
{
cout << in.get() << endl;
}
in.close();
}
Run Code Online (Sandbox Code Playgroud)
运行这些代码后,我在控制台屏幕的末尾看到"-1".有人请解释一下?谢谢
c++ ×1