本文介绍了在C链表(从文件中读取)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的C编程,和我有一些困难。我试图读取行到一个文本文件行,然后每行添加到一个简单的链表。我已经尝试了很多,但我还没有找到一个解决方案。所以在我的code到目前为止我可以从文件中读取,但我不明白如何保存文本行的行并将其添加到链接列表。

这是我迄今为止:

 结构列表{
字符的字符串;
结构列表*接下来的;
};typedef结构列表清单;诠释主要(无效){    FILE * FP;
    TMP的char [100];
    LIST *目前,*头;
    焦炭℃;
    INT I = 0;
    电流= NULL;
    头= NULL;
    FP = FOPEN(test.txt的,R);    如果(FP == NULL){
        的printf(错误,同时打开文件\\ n);
        出口(EXIT_FAILURE);
    }    的printf(文件打开\\ n);    而(EOF!=(C =龟etc(FP))){
       的printf(%C,C);
    }    如果(FCLOSE(FP)== EOF){
        的printf(在关闭文件\\ n错误!);
        出口(EXIT_FAILURE);
    }
    的printf(\\ n文件关闭。);
}

如果任何人都可以给我一些指点我需要什么旁边使其工作要做,我会极力AP preciate它。我已经习惯了Java和不知我的大脑无法理解如何在C做这些事情。


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;结构列表{
    字符*串;
    结构列表*接下来的;
};typedef结构列表清单;诠释主要(无效){
    FILE * FP;
    焦线[128];
    LIST *目前,*头;    头=电流= NULL;
    FP = FOPEN(test.txt的,R);    而(与fgets(线,的sizeof(线),FP)){
        LIST *节点=的malloc(sizeof的(LIST));
        与于节点GT;字符串=的strdup(线); //注:的strdup不是标准功能
        于节点>接着= NULL;        如果(头== NULL){
            电流= =头节点;
        }其他{
            电流=电流 - >接着=节点;
        }
    }
    FCLOSE(FP);
    //打印测试
    对于(电流=头;电流;电流=电流 - >接下来){
        的printf(%S型,电流>字符串);
    }
    //需要免费为每个节点
    返回0;
}

I'm very new to C-programming, and I'm having some difficulties. I'm trying to read line from line to a text file, and then add each line to a simple linked list. I have tried a lot, but I haven't found a solution. So far in my code I'm able to read from the file, but I can't understand how to save the text line for line and add it to the linked list.

This is what I have so far:

struct list {
char string;
struct list *next;
};

typedef struct list LIST;

int main(void) {

    FILE *fp;
    char tmp[100];
    LIST *current, *head;
    char c;
    int i = 0;
    current = NULL;
    head = NULL;
    fp = fopen("test.txt", "r");

    if (fp == NULL) {
        printf("Error while opening file.\n");
        exit(EXIT_FAILURE);
    }

    printf("File opened.\n");

    while(EOF != (c = fgetc(fp))) {
       printf("%c", c);
    }

    if(fclose(fp) == EOF) {
        printf("\nError while closing file!");
        exit(EXIT_FAILURE);
    }
    printf("\nFile closed.");
}

If anyone could give me some pointers on what I need to do next to make it work, I would highly appreciate it. I'm used to Java, and somehow my brain can't understand how to do these things in C.

解决方案
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct list {
    char *string;
    struct list *next;
};

typedef struct list LIST;

int main(void) {
    FILE *fp;
    char line[128];
    LIST *current, *head;

    head = current = NULL;
    fp = fopen("test.txt", "r");

    while(fgets(line, sizeof(line), fp)){
        LIST *node = malloc(sizeof(LIST));
        node->string = strdup(line);//note : strdup is not standard function
        node->next =NULL;

        if(head == NULL){
            current = head = node;
        } else {
            current = current->next = node;
        }
    }
    fclose(fp);
    //test print
    for(current = head; current ; current=current->next){
        printf("%s", current->string);
    }
    //need free for each node
    return 0;
}

这篇关于在C链表(从文件中读取)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:29