本文共 700 字,大约阅读时间需要 2 分钟。
在c中,ascii字符具有默认值.喜欢 !值为33,“,”的值也为44,依此类推.
在我的文本文件“hehe.txt”中. ;!,.
#include
#include
int main() {
std::ifstream file("hehe.txt");
if(file.eof())
return 0;
char ascii;
while(file>>ascii) {
std::cout << (int)ascii << " ";
}
system("pause");
}
产量为59 33 44 46.
编辑:当我运行程序时,如何防止空格被忽略为从文本文件中读取?假设我在最后一个字符后面添加了空格;!,.因此,输出必须是59 33 44 46 32.希望有人能给我一个如何做到这一点的想法.
问题是分隔符.当您使用文件>> ascii这将“吃掉”你所有的空间,因为它们被用作分隔符.
您可以使用getline然后遍历字符串中的所有字符.
std::ifstream file("../../temp.txt");
if(!file)return 0;
std::string line;
while (std::getline(file, line, '\0')){
for(char ascii : line){
std::cout<
}
}
system("pause");
return 0;
正如dornhege所说,还有可能是:
while(file >> std::noskipws >> ascii){
std::cout << (int) ascii << "\n";
}
转载地址:https://blog.csdn.net/weixin_33554514/article/details/117120825 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!