Read file without fopen() (C language)

后端 未结 2 1057
情话喂你
情话喂你 2021-01-19 06:23

I am working on a school project in which we have to do some operations (select, min, max) on a table saved in .txt file. The problem is that we can\'t use common functions

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-19 07:22

    You do not need to open the file - the operating environment will do it for you.

    When your program is called with , your standard input is switched to read from that file instead of the keyboard. You can use scanf to read the data, and do not worry about opening and closing the file.

    Same goes for the output of your program and the >table_out.txt redirection: rather than printing to the screen, printfs in your program would be writing to a file, which would be automatically closed upon your program's exit. Of course if you need to print something to the screen when your output is redirected, you can do so by printing to stderr (e.g. fprintf(stderr, "Invalid table format\n").

提交回复
热议问题