fgetc 无法加载文件的最后一个字符

fgetc fails to load last character of file

我正在尝试将一个文件加载到我的程序中,以便我可以单独处理这些字节,但是当我加载文件时,它会过早地停止加载;总是由 1 个字符。如果文件中只有一个字符,则不会加载它。是我读取文件的方式有问题,还是文件位于其他位置?

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

typedef struct node {//linked list structure, I use this because I am working with files of vastly varying length
    char val;
    struct node *next;
} data;

void printdata(data *head);
void freeData(data **head);
data* readFile(FILE *f);

void main(int argc, char *argv[]) {//set this up so it is easier to test
    if(argc == 2) {
        FILE *f = fopen(argv[1], "r");
        data *d = readFile(f);
        fclose(f);
        printdata(d);
        freeData(&d);
    }
}

data* readFile(FILE *f) {//This is the function containing the problem
    data *retVal = malloc(sizeof(data));
    data *cur = retVal;
    int c = fgetc(f);
    if(c != EOF) {
        retVal->val = (char) c;
        while((c = fgetc(f)) != EOF) {
            cur->next = malloc(sizeof(data));
            cur->next->val = (char) c;
            cur = cur->next;
        }
    } else return NULL;//EDIT: was in a rush and forgot to add this.
    cur->next = NULL;
    return retVal;
}

void freeData(data **head) {
    if((*head)->next != NULL) freeData(&((*head)->next));
    free(*head);
}

void printdata(data *head) {
    data *cur = head;
    do {
        printf("%c", cur->val);
        cur = cur->next;
    } while(cur->next != NULL);//EDIT: I changed this because there was a small "problem" that was not the real problem
    printf("\n");
}

我们来看函数printdata()

void printdata(data *head) {
    data *cur = head;
    while(cur->next != NULL) {
        printf("%c", cur->val);
        cur = cur->next;
    }
    printf("\n");
}

注意当

cur->next == NULL

while 内的命令将执行。

另请注意,这总是发生在最后一个元素处。所以你的代码不会打印最后的数据。

在其他解决方案中,您可以使用 do-while 循环:

do{
  printf("%c", cur->val)
  cur = cur->next;
} while (cur->next != NULL);

这将保证打印最后一个元素,因为 while 将停止 AFTER 循环内部对最后一个元素执行。

希望对您有所帮助。

printdata() 停得太快了。

cur->next == NULL时不停止。 cur == NULL

时停止
void printdata(data *head) {
  data *cur = head;
  while (cur) {
    printf(" <%hhx>", cur->val);  // Changed format for debugging
    fflush(stdout);               // Useful for debugging
    cur = cur->next;
  }
  printf("\n");
}

还包括简化的 readFile()

data* readFile(FILE *f) { //This is the function containing the problem
  data head; // Only next field used
  data *cur = &head;
  int c;
  while ((c = fgetc(f)) != EOF) {
      cur->next = malloc(sizeof *(cur->next));
      cur = cur->next;
      assert(cur);
      cur->val = (char) c;
    }
  cur->next = NULL;
  return head.next;
}