Good morning, my question is as follows. I have a flat file called group, in which all linux users are shown. My problem is that I need to read each of the lines in the file and show the group's identifier and its name on the screen. Something like this:
Name of the group: root Group identifier: 0
My problem is that when I scan the file with fscanf, it keeps the entire file line in the first string. Does anyone know how I could go through that line separating each of the elements of the line? File lines:
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,rafa
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
The code:
char *valor="/etc/group";
fich=fopen(valor,"r");
if (fich)
{
while ((fscanf(fich,"%s'%s'%d':'\n",nombre,basura3,&ident)!=0) && (fscanf(fich,"%s'%s'%d':'%s \n",nombre,basura3,&ident,basura)!=0) && (!feof(fich)))
{
printf("Nombre del grupo:%s \n",nombre );
printf("Identificador del grupo: %d \n",ident);
}
fclose(fich);
}
else
{
printf("Error fichero.\n");
}