简体   繁体   English

在C中逐行读取每个char

[英]Reading each char line by line in C

I was looking for a solution on how to read char by char on each line from a txt file and I found one, but I don't get some parts of the code. 我一直在寻找一种关于如何从txt文件中逐行读取char的解决方案,但我找到了一个,但是我没有得到代码的某些部分。 This is it: 就是这个:

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

void handle_line(char *line) {
  printf("%s", line);
}

int main(int argc, char *argv[]) {
    int size = 1024, pos;
    int c;
    char *buffer = (char *)malloc(size);

    FILE *f = fopen("myfile.txt", "r");
    if(f) {
      do { // read all lines in file
        pos = 0;
        do{ // read one line
          c = fgetc(f);
          if(c != EOF) buffer[pos++] = (char)c;
          if(pos >= size - 1) { // increase buffer length - leave room for 0
            size *=2;
            buffer = (char*)realloc(buffer, size);
          }
        }while(c != EOF && c != '\n');
        buffer[pos] = 0;
        // line is now in buffer
        handle_line(buffer);
      } while(c != EOF); 
      fclose(f);           
    }
    free(buffer);
    return 0;
}

It was written by someone from here, but I can't reply 'cause I need more points lol. 它是从这里来的某人写的,但是我无法回答,因为我需要更多积分。 The parts I don't understand are: 我不明白的部分是:

if(c != EOF) buffer[pos++] = (char)c;

What does buffer[pos++] do? buffer [pos ++]有什么作用? does it actually increase the variable "pos"? 它实际上增加了变量“ pos”吗? also, why does it start at 1 instead of 0? 另外,为什么它从1开始而不是0? (pos starts at 0). (pos从0开始)。 I can't really get track of the variable "pos", and I don't know why here buffer[pos] is 0: 我无法真正跟踪变量“ pos”,也不知道为什么buffer [pos]为0:

buffer[pos] = 0;

The way I read the code is: declare the size of the buffer that contains every char of every line (I mean, buffer is just free'd at the end, so it keeps the information on every line right?), then declare the other variables and alloc the memory of the buffer. 我读代码的方式是:声明包含每一行每个字符的缓冲区的大小(我的意思是,缓冲区只是在末尾释放,因此它使每一行的信息正确吗?),然后声明其他变量并分配缓冲区的内存。 Open the file myfile.txt, and if it's not null, make pos = 0, then make "c" to store the first character of the file (now the function points to the next char), then if c != EOF meaning the end of file is not reached, save the character "c" in the position 1 of the buffer (I get confused here, why 1 and not 0). 打开文件myfile.txt,如果不为null,则使pos = 0,然后使“ c”存储文件的第一个字符(现在函数指向下一个字符),然后如果c!= EOF表示未到达文件末尾,将字符“ c”保存在缓冲区的位置1(我在这里感到困惑,为什么1而不是0)。 Then realloc twice as memory as before if needed. 然后根据需要像以前一样重新分配两次内存。 Do that for every character in the line untile you reach EOF or a \\n. 对行中的每个字符执行此操作,直到达到EOF或\\ n。 Now make buffer[pos] = 0, I dont know what value "pos" has, and I assume he makes buffer[pos] = 0 to indicate the end of the line? 现在使buffer [pos] = 0,我不知道“ pos”的值是什么,我假设他使buffer [pos] = 0表示行的结尾? idk. idk。 Print the line, do that until you reach the end of the file. 打印该行,直到到达文件末尾。 Close the file, free the memory on buffer. 关闭文件,释放缓冲区上的内存。

Help! 救命! thanks. 谢谢。

fgetc(fp) - Reads the next character from the specified input stream (fp) and advances the associated file position indicator (you do not need to). fgetc(fp) -从指定的输入流(fp)读取下一个字符,并前进关联的文件位置指示符 (不需要)。 If successful, the function will return the character read; 如果成功,函数将返回读取的字符; otherwise, the value EOF (-1) is returned. 否则,返回值EOF(-1)。

Here is a very simple example of using fgetc() to read each character of a file (and write it to another file using fputc()) 这是一个使用fgetc()读取文件的每个字符的非常简单的示例(并使用fputc()将其写入另一个文件)

char filename1[]={"c:\\play\\_in.txt"};//change paths as needed
char filename2[]={"c:\\play\\_out.txt"};

int main(void)
{
    FILE *fp1 = {0};
    FILE *fp2 = {0};
    int c=0;

    fp1 = fopen(filename1, "r");
    if(fp1)
    {
        fp2 = fopen (filename2, "w");
        if(fp2)
        {
            c = fgetc(fp1);
            while(c != EOF)
            {
                fputc(c, fp2);
                c = fgetc(fp1);
            }
            fclose(fp2);            
        }
        fclose(fp1);
    }

    return 0;
}

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

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