不使用fopen()读取文件(C语言) [英] Read file without fopen() (C language)

查看:234
本文介绍了不使用fopen()读取文件(C语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个学校项目,在该项目中,我们必须对保存在.txt文件中的表进行一些操作(选择,最小,最大). 问题是我们不能使用诸如fopen,fscanf,fclose之类的常用功能.

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 such as fopen, fscanf, fclose.

程序将从命令行启动,如下所示:.\project.exe select parameters <table.txt

The program will be launched from command line like this: .\project.exe select parameters <table.txt

您是否有一些想法,如何在不使用fopen的情况下将.txt文件的内容获取到stdin? 谢谢.

Do you have some ideas how to get content of the .txt file to stdin without using fopen? Thanks.

推荐答案

您不需要打开文件-操作环境将为您完成该操作.

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

使用<table.txt调用程序时,标准输入将切换为从该文件而不是键盘进行读取.您可以使用scanf读取数据,而不必担心打开和关闭文件.

When your program is called with <table.txt, 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.

对程序的输出和>table_out.txt重定向使用相同的命令:程序中的printf而不是打印到屏幕上,而是写入文件,该文件将在程序退出时自动关闭.当然,如果在重定向输出时需要在屏幕上打印某些内容,则可以通过打印到stderr(例如fprintf(stderr, "Invalid table format\n").

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").

这篇关于不使用fopen()读取文件(C语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆