如何检索文本文件中的行

发布于 2024-10-08 05:06:54 字数 196 浏览 0 评论 0原文

我开始用 C 语言编程,在读取文本文件时遇到一些问题。让我解释一下。

我有一个文件文本,其组织方式如下:

Tony 
12.23
John
09.45
Tayris
03.99

我想检索所有少于十的笔记并显示它们,但我不能......

有人可以帮助我吗?

多谢。

I started programing in language C, and I have some problems with reading text files. Let me explain.

I have one file text which is organized like this :

Tony 
12.23
John
09.45
Tayris
03.99

I would like to retrieve all notes less than ten and display them, but I can't...

Does anybody could help me?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-10-15 05:06:54

C 提供了四个可用于从磁盘读取文件的函数:

  1. fscanf()

    面向字段的函数。

  2. fgets()

    面向行的函数。

  3. fgetc()

    面向字符的函数

  4. fread()

    面向块的函数。

请参阅本文了解更多信息。

C provides four functions that can be used to read files from disk:

  1. fscanf()

    field oriented function.

  2. fgets()

    line oriented function.

  3. fgetc()

    character oriented function

  4. fread()

    block oriented function.

See this article for more information.

梦晓ヶ微光ヅ倾城 2024-10-15 05:06:54

查看 fgets 函数。它将返回直到(并包括)字符串字符的末尾(如果需要,您可以将其从目标字符串中删除)。

http://people.cs.uchicago.edu/~iancooke/osstuff/ccc .html 提供了一个示例:

这是一个更复杂的示例。
Readline() 使用 fgets() 读取最多
MAX_LINE - 1 个字符
缓冲区“输入”。它剥离前面的
空白并返回一个指向
第一个非空白字符。

 char *Readline(char *in) {
   char *cptr;

   if (cptr = fgets(in, MAX_LINE, stdin)) {
     /* kill preceding whitespace but leave \n 
        so we're guaranteed to have something*/
     while(*cptr == ' ' || *cptr == '\t') {
       cptr++;
     }
     return cptr;    
    } else {
     return 0;
   }
 }

我想这应该足够了。

Check out the fgets function. It will return until (and including) the end of string character (you can strip it from the destination string if you want).

http://people.cs.uchicago.edu/~iancooke/osstuff/ccc.html offers an example:

Here's a more complicated example.
Readline() uses fgets() to read up to
MAX_LINE - 1 characters into the
buffer 'in'. It strips preceding
whitespace and returns a pointer to
the first non-whitespace character.

 char *Readline(char *in) {
   char *cptr;

   if (cptr = fgets(in, MAX_LINE, stdin)) {
     /* kill preceding whitespace but leave \n 
        so we're guaranteed to have something*/
     while(*cptr == ' ' || *cptr == '\t') {
       cptr++;
     }
     return cptr;    
    } else {
     return 0;
   }
 }

That should be enough I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文