Simple C++ input from file…how to?

后端 未结 4 1355
执笔经年
执笔经年 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 16:58

    • Keep a kind of 'global state'
    • Write a loop that reads a line from the file until end-of-file.
    • Read the line into a buffer
    • Check the first character of the buffer, if it is P or T or r or s or t, change the global state of the application
    • If the first character was a T, use a sscanf(Buffer+1,"%lf %lf %lf",&first,&second,&third) to read the rest of the line.
    • Do something similar if the first character is r, s or t.
    • If the application is in the 'P-state' just scan the buffer using sscanf(Buffer,"%lf %lf",&first,&second)

提交回复
热议问题