V
Valerio Daelli
Hello everybody
I have a problem with a linked list.
I just get segmentation faults.
Could anyone please help me?
Thanks, this is the code
__________________________
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
using namespace std;
struct TCell {
char* NumeroTel;
TCell* Next;
};
struct TList {
unsigned Size;
TCell* First;
};
int main()
{
TList List;
List.Size=0;
int FloatToRead;
char* testo[100];
char* buffer;
int nbytes=100;
int i=0;
int a=0;
FILE *filePtr;
filePtr = fopen("input","r");
buffer=(char *)malloc((nbytes+1)*(sizeof(char)));
while (fgets(buffer,nbytes,filePtr) != NULL)
{
TCell* Temp=List.First;
List.First=new TCell;
List.First->NumeroTel=(char *)malloc(sizeof(buffer)+1);
sprintf(List.First->NumeroTel,buffer);
i++;
printf("Letti %d valori: %s - %s",i,buffer,List.First->NumeroTel);
List.First->Next=Temp;
++List.Size;
}
fclose(filePtr);
for(int j=0;j<List.Size;++j) {
printf("Valore: %s \n",List.First->NumeroTel);
TCell* Temp=List.First;
List.First=List.First->Next;
cout << "Distruzione della cella..." << endl;
delete Temp;
}
}
I have a problem with a linked list.
I just get segmentation faults.
Could anyone please help me?
Thanks, this is the code
__________________________
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
using namespace std;
struct TCell {
char* NumeroTel;
TCell* Next;
};
struct TList {
unsigned Size;
TCell* First;
};
int main()
{
TList List;
List.Size=0;
int FloatToRead;
char* testo[100];
char* buffer;
int nbytes=100;
int i=0;
int a=0;
FILE *filePtr;
filePtr = fopen("input","r");
buffer=(char *)malloc((nbytes+1)*(sizeof(char)));
while (fgets(buffer,nbytes,filePtr) != NULL)
{
TCell* Temp=List.First;
List.First=new TCell;
List.First->NumeroTel=(char *)malloc(sizeof(buffer)+1);
sprintf(List.First->NumeroTel,buffer);
i++;
printf("Letti %d valori: %s - %s",i,buffer,List.First->NumeroTel);
List.First->Next=Temp;
++List.Size;
}
fclose(filePtr);
for(int j=0;j<List.Size;++j) {
printf("Valore: %s \n",List.First->NumeroTel);
TCell* Temp=List.First;
List.First=List.First->Next;
cout << "Distruzione della cella..." << endl;
delete Temp;
}
}