简体   繁体   中英

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:

input: Hi
output: 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.

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) .

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. 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:

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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