【发布时间】:2011-12-19 08:49:39
【问题描述】:
我只是想从文件中读取每个字符并将它们打印在屏幕上。 为了测试,我尝试在打印字符之前先在控制台屏幕中打印 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*)'
我使用了下面的代码
#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();
}
运行这些代码后,我在控制台屏幕的末尾看到“-1”。 有人请解释一下吗?谢谢
【问题讨论】:
-
那么,
count.txt中有什么内容?你还不如实际上有一个-1! -
您发布的编译器错误有什么关系?
-
方法一步步调试了吗?查看所有变量,看看“-1”是出现在
cout <<中还是作为 main 的返回值。 -
count.txt 包含以下内容:assign1_2.cpp:33:20: error: cannot convert 'std::string {aka std::basic_string
}' to 'const char*' for argument '1' 到 'int atoi(const char*)' 谢谢 -
@Martin 好吧,函数返回是无效的。所以我认为 cout 与它没有任何关系?我是 C++ 的新手,所以很抱歉我不太明白你的意思。
标签: c++