简体   繁体   中英

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

I wrote a code for implementing file 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?"; 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. Once you do that the end of file flag in cin will be set and cin will longer read from input. You need to call clear() to remove the flag and continue reading.

What you need is the following, immediately after 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. 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. The last line is important as otherwise your stream will still have the EOF in it, so you need to discard it.

In writefile you loop until cin.eof . eof means "end of file": explicitly, there is no more input from here. 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. 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.

You should probably treat eof as an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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