c-将PPM文件的注释存储在链表中



我正在尝试读取PPM文件并将注释存储在链表中,到目前为止,我已经创建了一个Node结构,其中包含指向下一个节点的值和结构指针。还创建了一个附加函数和一个打印链表函数。但当我尝试调用main函数时,它不会打印任何内容。

struct Node{
char value;
struct Node *next;
};
void append(struct Node * headNode, int newElement){
struct Node *newNode = malloc(sizeof(struct Node)); //Dynamically Allocating Memory for New Node
struct Node *tailNode = headNode;                   //Creating A Tail Node to traverse the linked list
newNode->value = newElement;                        //Assigning the values of newNode to the given value
newNode->next = NULL;                               //Setting next value to be null since its the last node
if (headNode == NULL){                              //Checking if headnode is empty
headNode = newNode;                             //Assigning headnode and tailnode to be newnode
tailNode = newNode;
}
while(tailNode->next != NULL){                      //Traversing through the linked list
tailNode = tailNode->next;
}
tailNode->next = newNode;                           //Setting tailnode's next to be newnode
tailNode = newNode;   
}
void printLinkedList(struct Node* headNode){
while(headNode != NULL){
printf("%d",headNode->value);
headNode = headNode->next;
}
}
struct Node* getComments(char *filename){
struct Node *headNode = malloc(sizeof(struct Node));
FILE *f = fopen(filename,"r");
int ch = getc(f);
while(ch == '#'){
while(ch != 'n'){
append(headNode,ch);
ch = getc(f);
}
}
return headNode;
}

此外,当我使用"%时,我的评论是一个字符串,但在printLinked List中s";它说参数是int,尽管我指定为char。

这是的主要功能

void main(int argc, char * argv[]){
printLinkedList(getComments(argv[1]);
}

PPM文件像这个一样启动

P3
# 100 * 100  square
100 100
255
..
..

这里带有#的行是一条注释。

欢迎对代码发表任何评论

注释行以"但是样本文件的第一行没有
循环while(ch == '#')结束
getcomment()结束
程序结束。

我认为你的计划还有更多的问题。但对于所描述的情况/输入,这只是解释。

最新更新