[英]Unable to read each word in a text file
我有一个包含内容的文本文件:
人工神经网络(ANN)或连接系统是受构成动物大脑的生物神经网络模糊启发的计算系统。[1] 此类系统通过考虑示例来“学习”(即逐步提高其性能)任务,通常无需进行特定于任务的编程。 例如,在图像识别中,他们可能会通过分析已被手动标记为“猫”或“没有猫”的示例图像并使用结果识别其他图像中的猫来学会识别包含猫的图像。 他们这样做时没有任何关于猫的先验知识,例如,它们有皮毛,尾巴,胡须和类似猫的脸。 相反,他们从所处理的学习材料中发展出自己的一组相关特征。
我正在使用此代码读取内容。
ifstream file("/Users/sourav/Desktop/stl/stl/stl/testdata.txt");
while (! file.eof()) {
string word;
file >> word ;
cout << word << "\n";
}
这是输出的前几行:
Artificial
neural
(ANNs)
are
vaguely
并且如果您发现内容没有正确阅读。 我看不到or connectionist systems are computing systems
。
我在读取文本文件时缺少一些字符串值。
注意:我正在使用Xcode。
ifstream file("/Users/sourav/Desktop/stl/stl/stl/dictionary.txt");
string line;
if (file.is_open()) // same as: if (myfile.good())
{
while(getline(file,line,'\r')){
transform(line.begin(), line.end(), line.begin(), ::tolower);
Dictionary.insert(line);
}
cout<<Dictionary.size()<<" words read from dictionary\n";
file.close();
为什么将dictionary.size()转换为小写时值会改变
尝试按照以下方式使用:
ifstream file("/Users/sourav/Desktop/stl/stl/stl/testdata.txt");
string word;
while(file >> word) //While there is a word to get... get it and put it in word
{
cout << word <<"\n";
}
可以从C ++文件中逐字逐字地阅读已接受问题的答案,找到更多解释。
我认为逻辑与您的逻辑之间并没有太大区别。
尽管这可能无法解释为什么它不起作用,但是您的代码可能如下所示:
ifstream file("testdata.txt");
do {
string word;
file >> word ;
if (!file.good()) break;
cout << word << "\n";
} while (!file.eof());
如果您从未尝试过阅读某些东西,那么测试eof
条件是不正确的。
此代码(以及您在逻辑上不正确的代码)可以正常工作。 因此正在发生其他事情(与xcode
不相关)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.