我不明白为什么 "cout" 没有正常工作
I can't figure out why "cout" didn't work properly
我写了一个实现文件 I/O 的代码。这段代码包含两个函数——一个用于将字符写入文本文件,另一个用于从文本文件中读取字符。
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
int writefile(const char* f)
{
int cnt = 0;
char c;
ofstream ofile;
ofile.open(f);
while (true)
{
cout << "input character:";
cin >> c;
if (cin.eof())
break;
ofile << c;
cnt++;
}
ofile.close();
return cnt;
}
int readfile(int n, const char* f)
{
int cnt = 0;
ifstream ifile;
ifile.open(f);
do
{
cout << static_cast<char>(ifile.get());
cnt++;
} while (cnt<n);
cout << endl;
ifile.close();
return cnt;
}
int main(void)
{
char ch;
int num,total,sum;
const char* filename = "test.txt";
total = writefile(filename);
cout << total<<" characters were written successfully." << endl;
cout << "how many characters?";
cin >> num;
sum = readfile(num,filename);
cout << sum << " characters were read successfully." << endl;
system("pause");
return 0;
}
问题是这样的:
cout << "how many characters?";
cin >> num;
这部分不起作用。我本来想显示用户输入的字符数,但我无法输入字符数。
我希望你们挑出问题所在。
问题是,在 writefile()
中,您读取流直到到达文件末尾。一旦你这样做了,cin
中的文件结束标志将被设置并且 cin
将不再从输入中读取。您需要调用 clear()
删除标志并继续阅读。
您需要的是紧跟在cout << "how many characters?";
之后的以下内容:
std::cin.clear();
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); // must #include <limits>
std::cin.unget(); // THIS IS IMPORTANT
第一行清除流的错误标志,设置为ios::eofbit
,因为你读到EOF。第二行忽略流中剩余的所有内容,包括分隔符(在本例中为换行符 '\n'
)或 EOF(但 EOF is not discarded),以先到者为准。 最后一行很重要,否则您的流中仍会有 EOF,因此您需要丢弃它。
在 writefile
中循环直到 cin.eof
。 eof
表示 "end of file":明确地说,这里没有更多的输入。就其本身而言,这是一种永久状态,就像玻璃杯是空的:"I'm trying to take one more sip, but nothing happens!"
你可以打电话给 cin.clear()
但这有点奇怪:就像装满用过的锡罐一样。更好的方法可能是测试空白行,例如通过测试
if (c == '\n')
现在用户可以输入字符直到完成,然后他们只需按回车键。
您可能应该将eof 视为错误。
我写了一个实现文件 I/O 的代码。这段代码包含两个函数——一个用于将字符写入文本文件,另一个用于从文本文件中读取字符。
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
int writefile(const char* f)
{
int cnt = 0;
char c;
ofstream ofile;
ofile.open(f);
while (true)
{
cout << "input character:";
cin >> c;
if (cin.eof())
break;
ofile << c;
cnt++;
}
ofile.close();
return cnt;
}
int readfile(int n, const char* f)
{
int cnt = 0;
ifstream ifile;
ifile.open(f);
do
{
cout << static_cast<char>(ifile.get());
cnt++;
} while (cnt<n);
cout << endl;
ifile.close();
return cnt;
}
int main(void)
{
char ch;
int num,total,sum;
const char* filename = "test.txt";
total = writefile(filename);
cout << total<<" characters were written successfully." << endl;
cout << "how many characters?";
cin >> num;
sum = readfile(num,filename);
cout << sum << " characters were read successfully." << endl;
system("pause");
return 0;
}
问题是这样的: cout << "how many characters?"; cin >> num;
这部分不起作用。我本来想显示用户输入的字符数,但我无法输入字符数。 我希望你们挑出问题所在。
问题是,在 writefile()
中,您读取流直到到达文件末尾。一旦你这样做了,cin
中的文件结束标志将被设置并且 cin
将不再从输入中读取。您需要调用 clear()
删除标志并继续阅读。
您需要的是紧跟在cout << "how many characters?";
之后的以下内容:
std::cin.clear();
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); // must #include <limits>
std::cin.unget(); // THIS IS IMPORTANT
第一行清除流的错误标志,设置为ios::eofbit
,因为你读到EOF。第二行忽略流中剩余的所有内容,包括分隔符(在本例中为换行符 '\n'
)或 EOF(但 EOF is not discarded),以先到者为准。 最后一行很重要,否则您的流中仍会有 EOF,因此您需要丢弃它。
在 writefile
中循环直到 cin.eof
。 eof
表示 "end of file":明确地说,这里没有更多的输入。就其本身而言,这是一种永久状态,就像玻璃杯是空的:"I'm trying to take one more sip, but nothing happens!"
你可以打电话给 cin.clear()
但这有点奇怪:就像装满用过的锡罐一样。更好的方法可能是测试空白行,例如通过测试
if (c == '\n')
现在用户可以输入字符直到完成,然后他们只需按回车键。
您可能应该将eof 视为错误。