1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
// test.c
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int index;
char data[80];
} ELEMENT;
int main(void)
{
char buf[80];
FILE *p = NULL;
ELEMENT *e = NULL;
int n = 0, i = 0;
//Execution de la commande (pipe)
if((p = popen("type test.c", "r")))
{
//Lecture du resultat ligne par ligne
while(fgets(buf, sizeof buf, p))
{
//Suppression du caractere '\n'
buf[strlen(buf)-1] = 0;
//Allocation d'une structure par ligne lue
if((e = (ELEMENT *)realloc(e, (i+1) * sizeof(ELEMENT))))
{
//Affectation des donnees a la structure
e[i].index = i;
strcpy(e[i].data, buf);
//Incrementation
n = i++;
}
}
pclose(p);
}
// Affichage du tableau de structures
for(i = 0; i < n; i++)
printf("%3d %s\n", e[i].index, e[i].data);
free(e);
return 0;
} |
Partager