2015-04-24 44 views
0

我想在这个程序中创建一个createLinkedListNode函数。在这个函数中,我打开一个参数文本文件,并且询问用户结构中的信息,然后从文件指针中引用的文件逐行读取它。我将数据添加到链接列表。我初始化链表的结构部分,并将下一个指针设置为NULL,并返回一个指向这个新创建节点的指针。我有两个结构,这混淆了我,因为我不知道我将如何将数据添加到指定的结构:阅读纺织,并添加到链接列表C

struct List{ 
    struct EMPLOYEE{ 
     int ID; 
     char *fName[25]; 
     char *lName[35]; 
     double salary; 
     char Location; 
    }employee; 
    struct List *next; 
}; 

这是我有两个链表的主要功能,我没有他们尚未初始化。但我在这里呼吁createLinkedList功能,我呼吁文件指针:

void main() { 
    FILE *fp; 
    struct List *onHead = NULL; 
    struct List *offHead = NULL; 

    createLinkedListNode(); 
} 

,当涉及到逐行读取文本文件并相应添加数据我只是迷路。所以如果有人能做到这一点会很棒。

struct List *createLinkedListNode(FILE *fp, struct List *list, int ID, char fName, char lName, double salary, char Location){ 
    //This is the created node with its memory allocated. 
    struct List* node = (struct List)*malloc(sizeof(struct List)); 

    fp =fopen("textfile.txt","r"); 

    //prompt for all info required in passenger struct 
    printf("Enter Passenger ID:\n"); 
    fgets() 
    node ->passenger.ID = ID; 
    printf("Enter first name:\n"); 
    node ->passenger.fName = fName; 
    printf("Enter last name:\n"); 
    node ->passenger.lName = lName; 
    printf("Enter salary:\n"); 
    node ->passenger.CBalance= CBalance; 

    printf("Enter location:\n"); 
    printf("1- on\n"); 
    printf("0- off\n"); 
    node ->passenger.Location =Location; 

    node ->next = list; 
    return node; 
} 

比方说正在读取的纺织品是这样的:

0001 
John 
Tyler 
23.00 
1 
0002 
Erin 
Marc 
25.00 
0 
0003 
Jason 
Ed 
15.00 
1 
+0

你的问题有点混乱。 1.您是否有意从文件中读取学生数据或提示用户使用它?你似乎同时暗示了两者。 2.这两个链表应该代表什么? 3.您对'createLinkedListNode'的调用与定义不匹配(缺少参数并忽略返回值)。 4.你的具体问题是什么?你需要比“不工作”更具体。 – kaylum

+0

1)数据只能从文件中读取,而不能从用户读取。问题就在那里,所以你可以看到每一行都被单独阅读。 2)稍后使用两个链表,如果员工位置是1,他被添加到onHead列表中,并且如果他们的位置是0,则他被添加到offHead列表中。 3)我不知道我在函数中的参数是否正确,所以现在我留下了空白。 4)我的具体问题是,我将如何从文件中读取这些数据并将其添加到结构和链接列表中,并创建此节点以便稍后使用它。 – user2188315

回答

0

我dont't真正理解你的问题,但它看起来像约与fgets一些信息会帮助你^^刚所以你看fgets如何工作:

char buffer[100]; 
FILE *fp = fopen("textfile.txt","r"); 

while (fgets(buffer, 100, fp)) { 
    printf("%s", buffer); 
} 

这将打印整个文件一行一行。到与fgets存储在第一个参数传递的字符串在文件的下一行每次调用(第二参数是字符,你可以读出的最高金额,而最后一个是你的文件)

与fgets返回NULL当你到达文件的结尾时,这就是这样工作的过程。

因此,不必在每个fgets之后打印,您可以计算行并填充结构,您必须将字符串转换为整数/双重tho。

编辑:那么这里是如何填写你的结构破坏者,它不是理想的解决方案,因为它应该是更强大的,像HANDELING情况下,该文件是不喜欢你所期望的,但它可能会帮助你。

而且我认为什么是错在你的结构,你写:

char *fName[25]; 
    char *lName[35]; 

这会造成你的25/35的char *数组,我想你想,如果没有那么*它只是一个数组的25/35字符(这是一个字符*)

SPOILERS !!!因此,在这里我写了一个小C文件,它读取您的文本文件并打印列表以显示它的工作原理,正如我所说的必须改进才能用于严重的软文件,文件可能不像我们想要的那样。

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

struct List{ 
    struct EMPLOYEE{ 
     int ID; 
     char fName[25]; 
     char lName[35]; 
     double salary; 
     char location; 
    }employee; 
    struct List *next; 
}; 

int main(int argc, char const *argv[]) 
{ 
    char buffer[100]; 
    struct List* node = NULL; 

    FILE *fp = fopen("textfile.txt","r"); 

    printf("Reading file....\n"); 

    while (fgets(buffer, 100, fp)) { 
     // File is not empty, it should at least contain a full employee 
     struct List* new_node = malloc(sizeof(struct List)); 
     new_node->next = NULL; 

     // ID 
     new_node->employee.ID = atoi(buffer); 

     // fName 
     fgets(buffer, 25, fp); 
     int l = strlen(buffer); 
     if (l > 0 && buffer[l-1] == '\n') buffer[l-1] = '\0'; // We dont want the \n in the name ^^ 
     strcpy(new_node->employee.fName, buffer); 

     // lName 
     fgets(buffer, 35, fp); 
     l = strlen(buffer); 
     if (l > 0 && buffer[l-1] == '\n') buffer[l-1] = '\0'; // We dont want the \n in the name ^^ 
     strcpy(new_node->employee.lName, buffer); 

     // salary 
     fgets(buffer, 100, fp); 
     new_node->employee.salary = atof(buffer); 

     // Location 
     fgets(buffer, 100, fp); 
     new_node->employee.location = buffer[0]; 

     // We add to the list 
     if (node == NULL) { 
      node = new_node; 
     } else { 
      new_node->next = node; 
      node = new_node; 
     } 
    } 

    printf("We're done\n"); 

    while (node != NULL) { 
     printf("Employee:\n"); 
     printf(" ID:\t\t%i\n", node->employee.ID); 
     printf(" fName:\t%s\n", node->employee.fName); 
     printf(" lName:\t%s\n", node->employee.lName); 
     printf(" Salary:\t%f\n", node->employee.salary); 
     printf(" location:\t%c\n\n", node->employee.location); 
     node = node->next; 
    } 

    // Memory leaks fest :) 
    return 0; 
} 

而且最后一件事,如果你有大升命名的位置没有特别的原因,你应该把它位置

告诉我,如果事情是错^^