すべての行に時間と温度が含まれるテキスト ファイルから、二重にリンクされたリストを作成する必要があります。たとえば、各行は次のようになります: 12:48 23.69
そのため、データを二重にリンクされたリストに入れるのに問題があります。実装方法がわかりません。そこで、要素の typedef 構造体の配列を作成し、配列の最初の要素から始めて、配列の 2 番目の要素の次を指すことができることを望んでいました。これが私が持っているものです...
二重リンク リストのヘッダー ファイルは次のとおりです。
#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();
}