【问题标题】:Reading a line from a file and printing it从文件中读取一行并打印
【发布时间】:2015-02-19 14:31:46
【问题描述】:
#include "stdio.h" 

main( ) { 
    FILE *fp1; 
    char oneword[100]; 
    char *c;       

    fp1 = fopen("TENLINES.TXT","r"); 
    do { 
        c = fgets(oneword, 100 ,fp1); /* get one line from the file */ 
        if (c != NULL) 
        printf("%s", oneword); /* display it on the monitor */ 
    }  while (c != NULL); /* repeat until NULL */ 

    fclose(fp1); 
} 

我不明白为什么这段代码需要一个 char *c。 char *c 在这里做什么。我试图将所有的“c”更改为“oneword”,但它总是会出错。你能解释一下吗? 谢谢。

【问题讨论】:

  • 如果文件不存在或由于某种原因您无法打开它,该程序将调用未定义的行为。
  • 推荐#include<stdio.h>。这个和你原来的有区别。
  • 请修正你的缩进。

标签: c


【解决方案1】:

您是否阅读过fgets(3) 的文档?它在失败时返回NULL(例如,当您到达文件末尾时)。

当然,您应该检查fopen(3) 是否失败,例如:

fp1 = fopen("TENLINES.TXT","r"); 
if (!fp1) 
  { perror("fopen TENLINES.TXT failure"); exit(EXIT_FAILURE); };

然后编译所有警告和调试信息(例如gcc -Wall -Wextra -g)并学习如何使用调试器 (gdb)

【讨论】:

    【解决方案2】:

    [评论太长]

    使用 while 循环而不是 do-loop 可以让您在不定义 c 的情况下绕过:

    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void) 
    { 
      int result = EXIT_SUCCESS; /* Be optimistic! */
    
      FILE * fp1 = fopen("TENLINES.TXT", "r"); 
      if (NULL == fp1)
      {
        result = EXIT_FAILURE; 
        perror("fopen() failed");
      }
      else
      {
        char oneline_if_shorter_then_100_characters[100]; 
        while (NULL != fgets(oneline_if_shorter_then_100_characters, sizeof oneline_if_shorter_then_100_characters, fp1)) /* get one line from the file */ 
        {
          printf("%s", oneline_if_shorter_then_100_characters); /* display it on the monitor */ 
        } /* repeat until fgets had returned NULL */ 
    
        fclose(fp1); 
      }
    
      return result;
    }
    

    【讨论】:

      【解决方案3】:

      fgets() 返回一个指针。你想把它存放在哪里?应该为此目的定义一个指针。这就是声明 char *c 的原因。您也不能为此使用oneword,它已经用于存储从文件中读取的行。

      【讨论】:

        【解决方案4】:

        使用fgetc() 处理您的要求的更安全的方法

        #include <stdio.h>
        
        int main() { 
        FILE *fp1; 
        char oneword[100]; 
        int c;
        
        int i=0;
        
        if((fp1 = fopen("Newfile.x","r")) <= 0){
            return 1; // File opening failled
        }  
        
          while((c=fgetc(fp1))!=EOF){
              oneword[i]=c;
              i++;
        
              if(i>=99 || c=='\n'){
                    // We are out of buffer OR new line reached
                    oneword[i]='\0';
                    printf("%s \n",oneword);
                    i=0;
              }
           }
        
            // If some left ; output that too
            oneword[i]='\0';
            printf("%s \n",oneword);
        
          return 1;
        }
        

        【讨论】:

        • char c; 应该是 int c;printf() 格式中不需要 ' ''\n'
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-30
        • 1970-01-01
        • 1970-01-01
        • 2019-03-24
        • 2021-04-02
        • 1970-01-01
        • 2023-04-09
        相关资源
        最近更新 更多