I'm trying to make a linked list with individual nodes having the names of files in my folder. With every additional folder, I'd like to output the whole linked list with the names of previous, already loaded files. I'm in my C:\Intel folder that has 2 files - ExtremeGraphics and Logs. This is my whole code.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
typedef struct item{
char *data;
struct item *next;
} tItem;
int insert(char *x, tItem **top){
tItem *temp = (tItem *)malloc(sizeof(tItem));
temp->data = x;
temp->next = *top;
*top = temp;
tItem *iterator;
iterator = *top;
while(iterator!=NULL){//display whole linked list
printf("\n\tNode data: %s, node next:0x%x\n", iterator->data, iterator->next);
iterator = iterator->next;
}
return 0;
}
int main()
{
tItem *head = NULL;
WIN32_FIND_DATA FindFileData;
HANDLE hFind, hFind_2;
hFind = FindFirstFile("C:\\Intel\\*.*", &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{ printf ("FindFirstFile failed (%d)\n", GetLastError());
return;}
else
{
while (FindNextFile(hFind, &FindFileData))
{
printf ("\n\nNext file found is %s\n", FindFileData.cFileName);
insert(FindFileData.cFileName, &head);
}
FindClose(hFind);
}
return 0;
}
The output is:
Next file found is ..
Node data: .., node next:0x0
Next file found is ExtremeGraphics
Node data: ExtremeGraphics, node next:0x330f80
Node data: ExtremeGraphics, node next:0x0
Next file found is Logs
Node data: Logs, node next:0x330fa0
Node data: Logs, node next:0x330f80
Node data: Logs, node next:0x0
I have no idea why it gives me the same ->data value for every node in the case of Logs and ExtremeGraphics, I'm not overwriting it anywhere.
Thanks for any advice.
Aucun commentaire:
Enregistrer un commentaire