我必须从一个文本文件中创建一个双向链表,其中每一行都有时间和温度。例如每一行是这样的:12:48 23.69
所以我无法将数据放入双向链表。我不知道如何实现它。所以我做了一个元素的 typedef 结构的数组,并希望我可以从数组的第一个元素开始,然后将下一个指向数组的第二个元素。这就是我所拥有的...
所以这是我的双向链表头文件:
#include<stdio.h>
typedef struct tempdata_{
int *hour;
int *min;
int *temp;
struct tempdata_ *next;
struct tempdata_ *prev;
}tempdata;
teypdef struct templist_{
tempdata *head;
tempdata *tail;
int size;
}templist;
`
这是我的主文件:
#include <stdio.h>
#include "linkedlist.h"
int main ( int argc, char *argv[] )
{
FILE *ifp, *ofp;
//char outputFilename[] = argv[2];
int SIZE = 1;
tempdata tempdata1[100];
tempdata *temp, *current = NULL;
if ( argc != 2 ) /* argc should be 3 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf( "usage: %s filename", argv[0] );
}
else
{
// We assume argv[1] is a filename to open
ifp = fopen( argv[1], "r" );
/* fopen returns 0, the NULL pointer, on failure */
if ( ifp == 0 )
{
printf( "Could not open file\n" );
}
else
{
//ofp = fopen(outputFilename, "w");
/* reads the hours, min, tempeture integers and temperature decimals and
prints them out on the screen and on the output file that is given.
we dont need this printing function but I just left to have the function do something*/
while (fscanf(ifp, "%d:%d %d.%d ", &tempdata1[SIZE].hour, &tempdata1[SIZE].min, &tempdata1[SIZE].tempI, &tempdata1[SIZE].tempD) != EOF) {
printf("the tempeture is %d.%d at %d:%d\n", tempdata1[SIZE].tempI, tempdata1[SIZE].tempD, tempdata1[SIZE].hour, tempdata1[SIZE].min);
/*fprintf(ofp, "the tempeture is %d.%d at %d:%d\n", tempdata1[SIZE].tempI, tempdata1[SIZE].tempD, tempdata1[SIZE].hour, tempdata1[SIZE].min);*/
SIZE++;
}
fclose(ifp);
//fclose(ofp);
}
}
getchar();
}