如何从c 中的文本文件中读取空格字符
发布时间:2020-12-16 09:47:34 所属栏目:百科 来源:网络整理
导读:在c中,ascii字符具有默认值.喜欢 !值为33,“,”的值也为44,依此类推. 在我的文本文件“hehe.txt”中. ;!,. #include iostream#include fstreamint main() { std::ifstream file("hehe.txt"); if(file.eof()) return 0; char ascii; while(fileascii) { std
在c中,ascii字符具有默认值.喜欢 !值为33,“,”的值也为44,依此类推.
在我的文本文件“hehe.txt”中. ;!,. #include <iostream> #include <fstream> 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,' ')){ for(char ascii : line){ std::cout<<(int)ascii << " "; } } system("pause"); return 0; 正如dornhege所说,还有可能是: while(file >> std::noskipws >> ascii){ std::cout << (int) ascii << "n"; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |