【发布时间】:2020-04-17 02:40:39
【问题描述】:
下面是我失败的尝试,终端读取 0 作为“lengthCapture”。我试过用谷歌搜索答案,但并不高兴。
我的文本文件标题为“Boogiedy.txt”,内容为“Boogiedy, Boogiedy, Boooooo”
终端显示:
Boogiedy, Boogiedy, Boooooo
27
0
我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string boogiedy;
int lengthCapture = boogiedy.length();
ifstream inputFile ("boogiedy.txt");
if(inputFile.is_open())
{
while (getline(inputFile,boogiedy))
{
cout <<boogiedy<< '\n';
cout <<boogiedy.length() << endl;
}
}
else
cout << "file is not open" << '\n';
inputFile.close();
cout << lengthCapture << endl;
}
【问题讨论】:
标签: c++ variables capture string-length