简体   繁体   English

在C中读取多行

[英]Reading Multiple lines in C

So I am trying to read input from a text file and print the exact same thing I read in C.So this below is the input followed by enter: 因此,我试图从文本文件中读取输入并打印与在C中读取的完全相同的内容。因此,这是输入,然后是enter:

input: Hi 输入: Hi
output: 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;
}

For this input: 对于此输入:

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

This is the output I expected: 这是我期望的输出:

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

This is what I got: 这就是我得到的:

    Hi, this is the first line 

It makes sense that the code is printing only the first line, but since the condition in the guard will no longer be true after hitting the new line, but I don't know how to structure my code so it reads line by line and prints them respectively. 代码仅打印第一行是有道理的,但是由于击中新行后防护的条件将不再为真,但是我不知道如何构造代码,因此它逐行读取并打印他们分别。

If you want the code to read each line, remove && ch != '\\n' from the condition of the while loop. 如果要让代码读取每一行,请从while循环的条件中删除&& ch != '\\n'

Also, the code is reading from stdin instead of a file. 另外,代码正在从标准输入而不是文件中读取。 Use fopen to read from a file, ie m = inputString(fopen("filename.txt", "r"), 512) . 使用fopen读取文件,即m = inputString(fopen("filename.txt", "r"), 512)

Try this, 尝试这个,

#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);

}

I have put '$' as the delimiter. 我已将“ $”作为分隔符。 I have used an extra buffer as newline is bound to EOF for stdin. 我已经使用了额外的缓冲区,因为换行符绑定到了stin的EOF。 So if I print out the character immediately it comes out of loop. 因此,如果我立即打印出该字符,它将退出循环。

All you need is repeat the process as long as you can read lines: 您需要做的就是重复此过程,只要您能阅读以下行:

int main(void) {
    char *m;

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

For this to work correctly, you must return NULL at end of file: 为了使其正常工作,您必须在文件末尾返回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);
}

Instead of: 代替:

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

you could do: 你可以做:

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

Now you have consumed the newline. 现在您已经消耗了换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM