简体   繁体   English

从C中的文件读取文本

[英]Reading text from a file in c

So I am trying to write a program that reads random lines of text from an input file. 因此,我正在尝试编写一个从输入文件中读取随机文本行的程序。 I can open the file but I dont know how to read characters yet, let alone character strings (only numbers so far). 我可以打开文件,但是我还不知道如何读取字符,更不用说字符串了(到目前为止只有数字)。 I am trying to make it so it can read in random lines of text and then I can manipulate them (ie print them in any order) 我正在尝试使其能够读取随机的文本行,然后对其进行操作(即以任何顺序打印)

And is it possible for the program to recognize spaces (or even better periods) in between words in the input file? 程序是否有可能识别输入文件中单词之间的空格(甚至更好的句点)? For example could I make it stop reading after the end of a sentence? 例如,我可以让它在句子结束后停止阅读吗?

I am not so much looking for someone to write the code for me or anything, I am using this project as kind of a learning exercise so if anyone could tell me what topics in c to study to make this possible that would be great! 我并不是在寻找某人为我或任何东西编写代码,我正在将该项目用作一种学习练习,因此,如果有人可以告诉我要学习c的哪些主题以使之成为现实,那将是很棒的!

Thanks! 谢谢!

Reading your file with fscanf() is a good start. 使用fscanf()读取文件是一个好的开始。 Have a look at the man page which will tell you how to read this either one variable at a time or many variables at a time. 看一下手册页,它将告诉您如何一次读取一个变量或一次读取多个变量。 If your file is more free form than that, you may want to read the whole thing into memory and then process into tokens (perhaps using strtok or strtok_r ), or use a combination of fscanf then process strings you have read with strtok or strtok_r . 如果您的文件更自由的形式比,你可能需要阅读整个事情到内存中,然后加工成令牌(可能使用strtokstrtok_r ),或使用组合fscanf然后处理字符串您已经阅读用strtokstrtok_r That should give you a start. 那应该给您一个开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM