segmentation fault

input:
10.5,2.4,3.4 5.3,2.0

57.3,52.0,7.2 8.6,3.5

1.9,5.2 4.6,8.1,1.8

3.5,9.6 7.8,9.8,1.5

7.5,0.5 0.005,4.50,4.751

I need that output is well:

10.5 2.4 3.4 5.3 2.0

57.3 52.0 7.2 8.6 3.5

1.9 5.2 4.6 8.1 1.8

3.5 9.6 7.8 9.8 1.5

7.5 0.5 0.005 4.50 4.751



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
#include <stdio.h>
#include <stdlib.h>

int main()
{

    FILE *p, *q; 
    double dados[4];   /* 4 element here   */
    char cara [3];         /* 3 elements here */

    p = fopen("entrada.txt","r");
    q = fopen("saida.txt","w+"); 

    if (p == NULL) 
    {
        printf("Arquivo inexistente!\n");
    }
    else
    {
        while(!feof(p)) 
        {
            fscanf (p,"%lf%c%lf%c%lf%c%lf%c%lf", &dados[0],&cara[0],&dados[1],&cara[1], &dados[2], &cara[2], &dados[3], &cara[3], &dados[4]); 
                    

            fprintf(q,"---------------------------------------------------------------\n");
            fprintf(q,"%lf \t %lf \t %lf \t %lf \t %lf\n",dados[0],dados[1],dados[2],dados[3],dados[4]); 
            fprintf(q,"---------------------------------------------------------------");
        }
    }
    return 0;
}

arthur@ClusterLapis03:~/Área de Trabalho/Algoritmos/Testes/Script$ ./teste.exeFalha de segmentação
Last edited on
closed account (z05DSL3A)
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
#include <stdio.h>
#include <stdlib.h>

int main()
{

    FILE *p, *q; 
    double dados[4];   /* 4 element here   */
    char cara [3];         /* 3 elements here */

    p = fopen("entrada.txt","r");
    q = fopen("saida.txt","w+"); 

    if (p == NULL) 
    {
        printf("Arquivo inexistente!\n");
    }
    else
    {
        while(!feof(p)) 
        {
            fscanf (p,"%lf%c%lf%c%lf%c%lf%c%lf",
                       &dados[0],&cara[0],&dados[1],&cara[1],
                       &dados[2], &cara[2], &dados[3], &cara[3], &dados[4]); 
                         /* 5 dados elements and 4 cara elements here */

            fprintf(q,"---------------------------------------------------------------\n");
            fprintf(q,"%lf \t %lf \t %lf \t %lf \t %lf\n",dados[0],dados[1],dados[2],dados[3],dados[4]); 
            fprintf(q,"---------------------------------------------------------------");
        }
    }
    return 0;
}
Last edited on
You are Forgetting Of That dados [0] and cara [0] has also count. Definitely this is not the error!
closed account (z05DSL3A)
dados[4] is dados[0] to dados[3]

You may need to re-read about arrays.
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
Topic archived. No new replies allowed.