繁体   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