Hi have a problem, this code should read a file and then save it in a linked list, just the <uid><name> from a lie looking like: <username>:<password>:<uid>:<gid>:<fullname>:<home dir>:<shell>
I can't find the segmentation fault error atm, but hints on the code would be helpfull.
Atfer the insert in user list the info should be sorted to the sortUser, with the smalest int first <uid>.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]){
FILE *file;
char line [ 128 ], x;
int y;
unsigned int tmpUid;
void* tmpName;
file=(fopen(argv[1],"r"));
printf("make node and stortNode");
struct node {
unsigned int uid;
char* name;
struct node* next;
} user, *p, sortUser, *sp;
printf("using malloc for nodes");
p = malloc(sizeof(struct node));
sp = malloc(sizeof(struct node));
printf("checking if file is empty");
if(file != NULL){
printf("starting the read loop");
while( fgets ( line, 128, file ) != NULL ){
printf("first-loop");
char str[10];
y=0;
while(x!='\n'){
printf("second-loop");
int i;
int size;
x=fgetc(file);
if(x==':')
y++;
if(y=0){
size++;
realloc(user.name,(sizeof(struct node)) + size);
user.name=&x;
}
if(y=2){
x=str[i];
i++;
}
}
user.uid=atoi(str);
p=p->next;
p->next = NULL;
}
while(sp!=NULL){
printf("trying to sort!");
if(sp->uid < p->uid){
sp->uid = p->uid;
sp++;
}
p++;
}
}
free(p);
free(sp);
return 0;
}