简体   繁体   English

我不知道为什么“提示”无法正常工作

[英]I can't figure out why “cout” didn't work properly

I wrote a code for implementing file I/O. 我编写了用于实现文件I / O的代码。 this code contains two function - one used to write characters into text file, and the other used to read characters from text file. 此代码包含两个功能-一个用于将字符写入文本文件,另一个用于从文本文件读取字符。

#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;
} 

the problem is this: cout << "how many characters?"; 问题是这样的:cout <<“多少个字符?”; cin >> num; cin >> num;

this part doesn't work. 这部分不起作用。 I wanted to display user-inputted number of characters originally but I couldn't input number of characters. 我本来想显示用户输入的字符数,但无法输入字符数。 I want you guys to pick what is problem. 我希望你们选择问题所在。

The problem is that in writefile() you read the stream until you reach the end of file. 问题是在writefile()您将读取流,直到到达文件末尾。 Once you do that the end of file flag in cin will be set and cin will longer read from input. 一旦你这样做文件标志到底cin将被设置和cin将不再从输入读取。 You need to call clear() to remove the flag and continue reading. 您需要调用clear()来删除该标志并继续阅读。

What you need is the following, immediately after cout << "how many characters?"; 您需要的是cout << "how many characters?";之后的以下内容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

The first line clears the error flag of the stream, set to ios::eofbit since you read until EOF. 第一行清除流的错误标志,因为您一直读到EOF,所以将其设置为ios::eofbit The second line ignores everything left in the stream up to and including the delimiter (newline '\\n' in this case) or EOF (however EOF is not discarded ), whichever comes first. 第二行忽略流中剩下的所有内容,包括定界符(在这种情况下为换行符'\\n' )或EOF(无论EOF是否被丢弃 ),以先到者为准。 The last line is important as otherwise your stream will still have the EOF in it, so you need to discard it. 最后一行很重要,因为否则您的流仍将包含EOF,因此您需要丢弃它。

In writefile you loop until cin.eof . writefile ,循环直到cin.eof eof means "end of file": explicitly, there is no more input from here. eof表示“文件结束”:明确地,这里没有更多输入。 On its own, this is a permanent condition like a glass being empty: "I'm trying to take one more sip, but nothing happens!" 就其本身而言,这是一个永久性的条件,例如杯子是空的:“我想再喝一口,但什么也没发生!”

You can call cin.clear() but that's a bit weird: like filling up a used tin can. 您可以调用cin.clear()但这cin.clear() :就像装满用过的锡罐一样。 A better approach might be to test for a blank line eg by testing 更好的方法可能是测试空白行,例如通过测试

if (c == '\n')

Now the user can type characters until they are done, then they just hit enter. 现在,用户可以键入字符直到完成操作,然后只需按Enter。

You should probably treat eof as an error. 您可能应该将eof视为错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM