C++ 无法打印常量字符串,但无法打印变量
我已经写了下面的代码,在盯着它看了很长时间并尝试了一系列不同的东西之后,我什么也没做。问题是,屏幕上打印的文本虽然“正确”,但不包括我每次打印的常量字符串,如C++ 无法打印常量字符串,但无法打印变量,c++,std,cout,C++,Std,Cout,我已经写了下面的代码,在盯着它看了很长时间并尝试了一系列不同的东西之后,我什么也没做。问题是,屏幕上打印的文本虽然“正确”,但不包括我每次打印的常量字符串,如“日期:或”任务: 代码如下: #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; int main() { ifstream myFile
“日期:
或”任务:
代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main() {
ifstream myFile("calend.txt");
vector<string> info;
string str;
while (getline(myFile, str, '-')) {
info.push_back(str);
if (info.size() > 1) {
cout << "date: " << info.at(0) << endl;
cout << "task: " << info.at(1) << endl;
info.clear();
}
}
myFile.close();
system("pause");
return 0;
}
最后是输出:
date: 15/05
task: checkpoint IART
18/05
date: checkpoint COMP
22/05
task: SDIS
25/05
date: apresentacao PPIN
27/05
task: IART
28/05
date: apresentacao LPOO
28/05
task: teste PPIN
01/06
date: LBAW
05/06
task: COMP
08*14/06
date: 15/05
task: checkpoint IART
18/05
如您所见,前两行打印正确,但之后它跳过常量字符串。提前感谢。您正在
std::getline()
中使用分隔符'-'
因此,从第1行:
仅15/05被读取并存储在str
中
然后,它继续读取另一个分隔符,即第1行和第2行:
15/05-checkpoint IART
^^^^^^^^^^^^^^^
18/05-checkpoint COMP
^^^^^
而且,事情就这样继续下去
这就是为什么您将其视为输出:
date: 15/05
task: checkpoint IART
18/05
date: checkpoint COMP
22/05
task: SDIS
25/05
date: apresentacao PPIN
27/05
task: IART
28/05
date: apresentacao LPOO
28/05
task: teste PPIN
01/06
date: LBAW
05/06
task: COMP
08*14/06
date: 15/05
task: checkpoint IART
18/05
这是您在控制台上的一个输出
解决方案:
读到-
,然后读到换行符,如下所示:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::ifstream myFile { "calend.txt" };
if ( !myFile.is_open() )
{
std::cerr << "Could not open file!\n";
return -1;
}
std::vector<std::string> info;
std::string date;
std::string task;
while ( getline(myFile, date, '-') && getline(myFile, task) )
{
info.push_back( date );
info.push_back( task );
if ( info.size() > 1 )
{
std::cout << "date: " << info.at(0) << '\n';
std::cout << "task: " << info.at(1) << '\n';
info.clear();
}
}
myFile.close();
return 0;
}
相关内容如下:
std::getline()
中使用分隔符'-'
因此,从第1行:
仅15/05被读取并存储在str
中
然后,它继续读取另一个分隔符,即第1行和第2行:
15/05-checkpoint IART
^^^^^^^^^^^^^^^
18/05-checkpoint COMP
^^^^^
而且,事情就这样继续下去
这就是为什么您将其视为输出:
date: 15/05
task: checkpoint IART
18/05
date: checkpoint COMP
22/05
task: SDIS
25/05
date: apresentacao PPIN
27/05
task: IART
28/05
date: apresentacao LPOO
28/05
task: teste PPIN
01/06
date: LBAW
05/06
task: COMP
08*14/06
date: 15/05
task: checkpoint IART
18/05
这是您在控制台上的一个输出
解决方案:
读到-
,然后读到换行符,如下所示:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::ifstream myFile { "calend.txt" };
if ( !myFile.is_open() )
{
std::cerr << "Could not open file!\n";
return -1;
}
std::vector<std::string> info;
std::string date;
std::string task;
while ( getline(myFile, date, '-') && getline(myFile, task) )
{
info.push_back( date );
info.push_back( task );
if ( info.size() > 1 )
{
std::cout << "date: " << info.at(0) << '\n';
std::cout << "task: " << info.at(1) << '\n';
info.clear();
}
}
myFile.close();
return 0;
}
相关内容如下:
date: 15/05
但是第二次迭代将捕获所有内容,直到下一个'-'并给出
task: checkpoint IART
18/05
从现在开始,一切都发生了变化
若要解决此问题,请尝试调用不带“-”的getline()将文件逐行读取为str,然后将str拆分为两个标记。循环的第一行说:在“-”之前给我所有内容,因此基本上让getline()跳过行分隔符,只查找“-”字符 这就是为什么它在第一次迭代时会正确拆分并打印
date: 15/05
但是第二次迭代将捕获所有内容,直到下一个'-'并给出
task: checkpoint IART
18/05
从现在开始,一切都发生了变化
要解决此问题,请尝试调用不带“-”的getline()将文件逐行读取为str,然后将str拆分为两个标记。谢谢,我完全忽略了这一点!谢谢,我完全错过了!