ProgIng - Programación en Ingeniería
Loading...
Searching...
No Matches
Ejemplo045.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3
4#define N 50
5
6int main(int argc, char *argv[])
7{
8 char filename[N] = "full_data.csv";
9 char *str;
10 char c;
11 int i, j, nc, nl, *ln, *Ceremony, *Year;
12 FILE *fp;
13 fp = fopen(filename, "rt");
14 if(fp==NULL)
15 {
16 printf("Error al leer el archivo.\n");
17 return 1;
18 }
19 //fscanf(fp, "%s", str);
20 /*
21 for(i=0; i<10; i++)
22 {
23 fgets(str, NC-1, fp);
24 printf("%s", str);
25 }
26 */
27 j=0;
28 i=0;
29 while((c=fgetc(fp))!=EOF)
30 {
31 j++;
32 if(c==10)
33 i++;
34 }
35 nl = i;
36 nc = j;
37 printf("Num L: %d\n", nl);
38 printf("Num C: %d\n", nc);
39 str = (char*)malloc((nc+1)*sizeof(char));
40 if(str==NULL)
41 {
42 fclose(fp);
43 return 1;
44 }
45 ln = (int*)calloc(nl, sizeof(int));
46 if(ln==NULL)
47 {
48 fclose(fp);
49 free(str);
50 return 2;
51 }
52 fseek(fp, 0, SEEK_SET);
53 for(i=0, j=1; i<nc; i++)
54 {
55 str[i] = getc(fp);
56 if(str[i]==10)
57 ln[j++] = i+1;
58 }
59 str[i] = '\0';
60 fclose(fp);
61 for(i=1; i<nl; i++)
62 str[ln[i]-1] = '\0';
63 Ceremony = (int*)malloc((nl-1)*sizeof(int));
64 if(Ceremony==NULL)
65 {
66 free(ln);
67 free(str);
68 return 3;
69 }
70 Year = (int*)malloc((nl-1)*sizeof(int));
71 if(Year==NULL)
72 {
73 free(Ceremony);
74 free(ln);
75 free(str);
76 return 3;
77 }
78 for(i=1; i<nl; i++)
79 {
80 Ceremony[i-1] = atoi(str+ln[i]);
81 j=0;
82 while(str[ln[i]+j]!=9)
83 j++;
84 Year[i-1] = atoi(str+ln[i]+j+1);
85 printf("%d. %d\t%d\n", i, Ceremony[i-1], Year[i-1]);
86 }
87 free(Year);
88 free(Ceremony);
89 free(ln);
90 free(str);
91 return 0;
92}
#define N
int main(void)
Definition Ejemplo_035.c:15