Simple C++ input from file…how to?

后端 未结 4 1283
执笔经年
执笔经年 2021-02-03 16:35

I have a file:

P 0.5 0.6 0.3
30 300
80 150
160 400
200 150
250 300
T
r  45 0 0
s 0.5 1.5 0 0
t 200 –150
.
.
.

When I read in \'P\' I know that

4条回答
  •  礼貌的吻别
    2021-02-03 17:16

    I think you can use standard streams
    to check "P" and "T"
    use get(char &ch);
    and putback(ch) to push it back to stream
    and
    yourstream >> x >> y >> endl;

    http://www.cplusplus.com/reference/iostream/istream/putback/

    // Example
    // istream putback
    #include 
    using namespace std;
    
    int main () {  
      char c;  
      int n;  
      char str[256];  
    
      cout << "Enter a number or a word: ";
      c = cin.get();  
    
      if ( (c >= '0') && (c <= '9') )
      {  
        cin.putback (c);
        cin >> n;
        cout << "You have entered number " << n << endl;
      }  
      else
      {  
        cin.putback (c);
        cin >> str;
        cout << " You have entered word " << str << endl;
      }  
    
      return 0;  
    }
    

提交回复
热议问题