簡體   English   中英

在C中讀取多行

[英]Reading Multiple lines in C

因此,我試圖從文本文件中讀取輸入並打印與在C中讀取的完全相同的內容。因此,這是輸入,然后是enter:

輸入: Hi
輸出: Hi

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

char *inputString(FILE *fp, size_t size) {
    //The size is extended by the input with the value of the provisional
    char *str;
    int ch;
    size_t len = 0;
    str = realloc(NULL, sizeof(char) * size); //size is start size
    if (!str)
        return str;
    while (EOF != (ch = fgetc(fp)) && ch != '\n') {
        str[len++] = ch;
        if (len == size) {
            str = realloc(str, sizeof(char) * (size += 16));
            if (!str)
                return str;
        }
    }
    str[len++] = '\0';

    return realloc(str, sizeof(char) * len);
}

int main(void) {
    char *m;

    // printf("input string : ");
    m = inputString(stdin, 10);
    printf("%s\n", m);

    free(m);
    return 0;
}

對於此輸入:

    Hi, this is the first line 
    This is the second line
    This is the third line \n

這是我期望的輸出:

    Hi, this is the first line 
    This is the second line
    This is the third line \n

這就是我得到的:

    Hi, this is the first line 

代碼僅打印第一行是有道理的,但是由於擊中新行后防護的條件將不再為真,但是我不知道如何構造代碼,因此它逐行讀取並打印他們分別。

如果要讓代碼讀取每一行,請從while循環的條件中刪除&& ch != '\\n'

另外,代碼正在從標准輸入而不是文件中讀取。 使用fopen讀取文件,即m = inputString(fopen("filename.txt", "r"), 512)

嘗試這個,

#include<stdio.h>

void main(int argc, char **argv)
{
        int cnt=0;
        char buf[1024];
        FILE *fptr=stdin;
        printf("Input: \n");
        char ch=fgetc(fptr);
        buf[cnt++]=ch;
        while(ch!='$')
        {
                buf[cnt++]=ch;
                ch=fgetc(fptr);
        }
        buf[cnt++]='$';
        buf[cnt]='\0';
        printf("Output:\n");
        fputs(buf,stdout);
        fclose(fptr);

}

我已將“ $”作為分隔符。 我已經使用了額外的緩沖區,因為換行符綁定到了stin的EOF。 因此,如果我立即打印出該字符,它將退出循環。

您需要做的就是重復此過程,只要您能閱讀以下行:

int main(void) {
    char *m;

    // printf("input strings: ");
    while ((m = inputString(stdin, 10)) != NULL) {
        printf("%s\n", m);
        free(m);
    }
    return 0;
}

為了使其正常工作,您必須在文件末尾返回NULL

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

char *inputString(FILE *fp, size_t size) {
    //The size is extended by the input with the value of the provisional
    int ch;
    size_t len = 0;
    char *str = malloc(size);

    if (str == NULL)
        return NULL;

    while ((ch = fgetc(fp)) != EOF && c != '\n') {
        if (len + 2 > size) {
            char *new_str = realloc(str, size += 16);
            if (!new_str) {
                free(str);
                return NULL;
            str = new_str;
        }
        str[len++] = ch;
    }
    if (c == EOF && len == 0) {
        /* at end of file */
        free(str);
        return NULL;
    }
    str[len++] = '\0';
    return realloc(str, len);
}

代替:

while(EOF!=(ch=fgetc(fp))&& ch != '\n' ){
    // stuff
}

你可以做:

while(EOF!=(ch=fgetc(fp))){
    // stuff
    if (ch == '\n') break;
}

現在您已經消耗了換行符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM