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/