我正在尝试将文件加载到我的程序中,因此我可以单独使用字节,但是当我加载文件时,它会过早地停止加载;总是由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");
}
答案 0 :(得分:1)
让我们看一下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内的命令将 NOT 执行。
另请注意,这总是发生在最后一个元素上。所以你的代码不会打印最后的数据。
在其他解决方案中,您可以使用do-while循环:
do{
printf("%c", cur->val)
cur = cur->next;
} while (cur->next != NULL);
这将保证打印最后一个元素,因为while将停止 AFTER 对最后一个元素执行循环内部。
希望这有帮助。
答案 1 :(得分:1)
printdata()
过早停止。 @Barmar
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;
}