简体   繁体   English

从文本文件中读取长度未知的字符串并打印它们

[英]Reading Strings of unknown length from text file and printing them

I have a question on how to read strings of unknown length from a text file while printing them out when the program comes across a '\\n'. 我有一个问题,当程序遇到'\\ n'时,如何在打印输出文本时从文本文件中读取未知长度的字符串。 The program should end if it notices that the file has no more strings left to read. 如果程序注意到该文件没有更多的字符串可读取,则程序应结束。

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

#define DEFLEN 2 
#define CHUNKSZ 2 


char *getStrFromFile(FILE *fpin); 

int main(void)
{
      FILE *fpin;
      fpin = fopen("test.txt","r");
      int d = 0;

while(1)
{
    char *ln;
    ln = getStrFromFile(fpin);
    if (!ln)
        break;
    ++d;
    printf("line %d : %s\n", d, ln);
    free(ln);
}
    fclose(fpin);
    return(0);
}



char *getStrFromFile(FILE *fpin) 
{

    int i = 0;
    char *line;


    line = (char*)malloc(DEFLEN);

    fgets(line,strlen(line),fpin);

        if(line[strlen(line-1)] == '\n')

            return line;

char  *temp;
temp = line;
char *read;

    read = (char*)malloc(CHUNKSZ);

    fgets(read,strlen(read),fpin);

    line = (char*)malloc(strlen(temp)+strlen(read));

    for(i; i<strlen(temp);i++);
        line[i]=temp[i];

    for(i;i<strlen(read);i++);
        line[i+strlen(temp)] = temp[i];

    free(read);
    free(temp);

    return line; 

}

The problem is that when I run the program it runs an infinite loop(which I suspect is the while(1) in the main), but doesn't appear to print out the strings in the text file. 问题是,当我运行程序时,它会运行无限循环(我怀疑是主循环中的while(1)),但是似乎无法打印出文本文件中的字符串。

What it prints is just: 它显示的只是:

line 1 :
line 2 :
line 3 :

... and so on till forever it seems. ...以此类推直到永远。

When I need it to print, for example: 当我需要打印时,例如:

line 1 : 0
line 2 : 012345
line 3 :
line 4 : 567890

Example of the program reading a text file with 4 lines. 读取4行文本文件的程序示例。

Quite frankly i'm stuck and don't know what im doing wrong. 坦白说,我被困住了,不知道我在做什么错。 I've gotten it to print something, but its was something along the lines of the ASCII character 177 177 + 177 177. 我已经得到它来打印东西了,但是它有点像ASCII字符177 177 + 177 177。

Which seems to point to an error in this line of code: 这似乎指向此代码行中的错误:

line = (char*)malloc(strlen(temp)+strlen(read));

Only thing I can think of is that my return line; 我唯一能想到的就是我的返回线。 is incorrect? 是不正确的? I am lost here help would be greatly appreciated! 我在这里迷路了,将不胜感激!

There is a need to extend to buffer to reading. 需要扩展以缓冲读取。 But your way is wrong. 但是你的方法是错误的。

char *getStrFromFile(FILE *fpin){
    int i=0, ch, size=DEFLEN+1;
    if(EOF==(ch=fgetc(fpin)))
        return NULL;
    ungetc(ch, fpin);
    char *line = malloc(size);

    while(EOF!=(ch=fgetc(fpin)) && ch != '\n'){//Do not include '\n'
        line[i++] = ch;
        if(i==size-1){
            size += CHUNKSZ;
            line = realloc(line, size);
        }
    }
    line[i] = '\0';
    return line;
}

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

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