【问题标题】:How to print the text from a file if the line number is given?如果给出了行号,如何从文件中打印文本?
【发布时间】:2021-08-21 10:17:23
【问题描述】:

我想将输入作为行号,并将输出作为文本文件中该行号的相应文本。

示例文本文件:

Hi this is Stefen  
Hi How are you

输入示例:

Enter the line number:2

预期输出:

Hi How are you

我的程序是:

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fp;
    fp = fopen("sample.txt", "r");
    if (fp == NULL) {
        perror("Unable to open the file\n");
        exit(1);
    }
    char buf[256];
    
    while (fgets(buf, sizeof(buf), fp) != NULL) {
        printf("%s\n", buf);
        print("~~~~\n");
    }
            
    fclose(fp);
            
    return 0;
}

我得到的输出:(每行下方带有分隔符~~~~ 的整个文件)

Hi this is Stefen
~~~~
Hi How are you
~~~~

谁能告诉我怎么做?

【问题讨论】:

  • 添加一个 count 变量,初始化为 0 并在循环内递增。对于您的程序的第一个版本,假设循环每次读取一个完整的行。仅当 counter 是所需的行号时才打印。

标签: c linux for-loop file-handling fgets


【解决方案1】:

按照pmg的建议,请您尝试以下方法:

#include <stdio.h>
#include <stdlib.h>
#define INFILE "sample.txt"

int main()
{
    FILE *fp;
    char buf[BUFSIZ];
    int count = 0, n;

    fp = fopen(INFILE, "r");
    if (fp == NULL) {
        perror(INFILE);
        exit(1);
    }

    printf("Enter the line number: ");
    fgets(buf, sizeof buf, stdin);
    n = (int)strtol(buf, (char **)NULL, 10);

    while (fgets(buf, sizeof buf , fp) != NULL){
        if (++count == n) {
            printf("%s", buf);
            break;
        }
    }

    fclose(fp);
    return EXIT_SUCCESS;
}

【讨论】:

    【解决方案2】:

    最好使用第二个文件

    检查你是否在 \n 这意味着换行并增加一个变量,如“line”

        printf(" \n Enter line number of the line to be deleted:");
        scanf("%d", &delete_line);
        //open new file in write mode
        ptr2 = fopen("c:\\CTEMP\\newfile.txt", "w");
         if(ptr2==NULL)
        printf("second error opening newfile");
        while (!feof(ptr1))
        {
            ch = fgetc(ptr1);
            if (ch == '\n')
            {
                temp++;
            }
            //except the line to be deleted
            if (temp != delete_line)
            {
                //copy all lines in file newfile.c
                fputc(ch, ptr2);
            }
        }
        
        fclose(ptr1);
        fclose(ptr2);
    

    “detele_line”变量供用户交互。

    【讨论】:

    • 不要这样做:while (!feof(ptr1))stackoverflow.com/questions/5431941/…
    • 另外,您的代码片段试图与 OP 的目标完全相反:我想将输入作为行号并将输出作为文本文件中该行号的相应文本.
    【解决方案3】:

    最简单的方法是使用数组保存行,然后打印特定的行。

    #include <stdio.h>
     
    #define M 10010
    #define N 256
    char buf[M][N];
     
    int main(){
        FILE *file;
        char fileName[50] = "sample.txt";
     
        file = fopen(fileName, "r");
        if(file == NULL)
            return 1;
        int n = 0;
        while(fgets(buf[n], N, file) != NULL){
            n++;
        }
        fclose(file);
     
        int i, x;
        printf("Example input:\nEnter the line number:");
        scanf("%d", &x);
        printf("Expected Output:\n%s", buf[x-1]);
     
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 2018-12-30
      • 2018-03-06
      • 2021-01-31
      • 2021-07-25
      相关资源
      最近更新 更多