Estoy tratando de imprimir una tabla de partición utilizando el lenguaje de programación C, todo parece funcionar bien: abrir y leer, pero no entiendo por qué está imprimiendo valores basura.Imprimir tabla de partición - Programa C
Aquí está el código:
struct partition
{
unsigned char drive;
unsigned char chs_begin[3];
unsigned char sys_type;
unsigned char chs_end[3];
unsigned char start_sector[4];
unsigned char nr_sector[4];
};
int main()
{
int gc = 0, i = 1, nr = 0, pos = -1, nw = 0;
int fd =0;
char buf[512] ;
struct partition *sp;
printf("Ok ");
if ((fd = open("/dev/sda", O_RDONLY | O_SYNC)) == -1)
{
perror("Open");
exit(1);
}
printf("fd is %d \n", fd);
pos = lseek (fd, 0, SEEK_CUR);
printf("Position of pointer is :%d\n", pos);
if ((nr = read(fd, buf, sizeof(buf))) == -1)
{
perror("Read");
exit(1);
}
close(fd);
printf("Size of buf = %d\n and number of bytes read are %d ", sizeof(buf), nr);
if ((nw = write(1, buf, 64)) == -1)
{
printf("Write: Error");
exit(1);
}
printf("\n\n %d bytes are just been written on stdout\n", nw,"this can also be printed\n");
printf("\n\t\t*************Partition Table****************\n\n");
for (i=0 ; i<4 ; i++)
{
sp = (struct partition *)(buf + 446 + (16 * i));
putchar(sp -> drive);
}
return 0;
}
Se está imprimiendo la basura en lugar de la tabla de particiones.
Podría tener algunos problemas básicos de comprensión, pero busqué con Google durante mucho tiempo, pero realmente no me ayudó. También vi el código fuente de fdisk, pero está más allá de mi comprensión en este punto. ¿Alguien podría por favor guiarme? No espero que alguien borre mi error y me proporcione el código de trabajo. Solo una oración o dos, o cualquier enlace.
consideró usted buscando en el código fuente del 'fdisk' u otra utilidad de partición? –
@BasileStarynkevitch Starynkevitch Sí, lo hizo de acuerdo con su publicación. @Adorn ¿Tu problema ocurre en esta línea? 'printf (" \ n \ t \ t ************* Tabla de particiones **************** \ n \ n ");'? ¿O te refieres a la salida después de eso? – xQuare
¿qué es 'drive'variable type? –