c - 如何在没有fopen的情况下读取C中的文本文件

标签 c text fopen getchar

can anyone help me? I need to read a file called address.txt and work with each word in there (I have one word per line to make it simple).

I am forbidden to use these functions:

Malloc, freee, fopen, fclose, fscanf,... qsort, lsearch, bsearch a hsearch I think the teacher wants us to use getchar, argv, argc or something like that.

Let's say the name of the program is test.c ____ /test.c < address.txt

If I write this as an argument when starting the program, I should be able to work with the text file. Any tips how to extract the data stored in address.txt? I have tried several times without success :/ Only fopen worked for me.

编辑:我所做的一切都很好,Code::Blocks 编辑程序导致了问题。我在Linux终端上尝试了一下,效果非常好! :)

最佳答案

是的,如果你的程序是这样启动的:

$ ./test < address.txt

然后(假设有一个兼容的操作系统,但这在主要桌面系统中是相当标准的)您的程序将以连接到文件 address.txt 的内容的 stdin 流启动>.

所以你可以使用getchar()从中阅读并找到单词。

请注意,您不会启动 C 文件,您必须先编译它。

关于c - 如何在没有fopen的情况下读取C中的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46886892/

相关文章:

android - 来自卡片 View 的所有文本都没有显示在 ScrollView 中

ios - iOS 中突出显示的文本(类似卡拉 OK 的应用程序)

c++ - 在系统驱动器 (C :\)) 中创建文件时,fopen 无形地失败

c - 我应该调用 fopen - fclose 每个 fwrite 操作吗

c++ - 在 c/c++ 中使用函数 system() 调用管道传输到 where 的 gdb,以记录堆栈跟踪 - 在 fd 0 上检测到错误挂断

c - append 到 C 中的文件,读取到结构数组

c - Scanf 不等待输入

c - 无符号整型的偶校验

linux - bash grep 文本中的字符串并发送电子邮件

c - 理解指针算术