EOF shortcut keys for linux system and windows system**

EOF shortcut keys for linux system and windows system

When I was studying today, I was tossed by the following code for nearly an hour, but the problem has not been solved yet. Finally, I asked the brother to help find the cause of the problem. It was extremely embarrassing...

#include <iostream>
#include <fstream>
#include <limits>

using namespace std;

int main(void){
    
    

   fstream str;
   int num;

    str.open("num.txt",ios::out);
    if(!str.is_open()){
    
    
        cout << "文件打开失败!" << endl;
        return 1;
    }
   
   while(1){
    
       
        int n = 0;
        //**linux系统下**
        cout << "请输入一个整数[**ctrl+d**退出]:";
        
        /**/windows系统下**
       // cout << "请输入一个整数[**ctrl+z**退出]:";
        
        cin >> num;

        if(cin.eof()){
    
    
            break;
        }

        while(cin.fail()){
    
    
            cin.clear();
            cin.ignore(std::numeric_limits<streamsize>::max(),'\n');

          cout << "请重新输入:" ;
          cin >> num;
        }
    
        str << num << "\t";
        if(++n % 10 == 0){
    
    
            str << endl;
        }
   }

    str.close();
    return 0;
}

** Purpose of the program: ** Repeat the input of numbers, and save the numbers to a file, requiring only 10 numbers per line.
(This was originally a very simple program, but the difference between the shortcut keys of the Linux system and the Windows system was ignored. The result was crawling in this pit for an hour...)
This code runs normally on VS, but under the terminal of the Linux system There is a problem with the operation: the number entered is not written into the file! ! !
Problem phenomenon : The

cause of the problem:
Insert picture description here
because of this pit, the data cannot be written into the file! ! !
Reason analysis :
ctrl + z: is the shortcut key of the end of file EOF under the windows system;
the function of the shortcut key under the linux system is to suspend the currently running program! ! !
ctrl + d: is the EOF shortcut key under linux system! ! !

Self-summary :
1. When encountering problems, thinking is not divergent enough, too hardened, it is easy to find the problem, always thinking in one direction, not knowing how to think about the problem in another direction.
2. The basic knowledge is not strong enough and comprehensive.

Guess you like

Origin blog.csdn.net/Zouzonghua/article/details/108740093