I have this file
3
10 10 10
0 0 0
0 0 1
1 0 0
1 1 1
1 0
0 0
1 1

and I want to read the first line as intger in X
the 2nd line in camp1
the 3rd line in camp2
line 4 to 6 into vertices
the other lines into edges

I wrote this code but it doesn't work ,, it doesn't fill the strcture at all.

typedef struct {
        point3d camP1;
        point3d camP2;
        double X;
        int noEdge;
        int noVer;
        point3d* vertices;
        edge*  edges;
    } scene;

int readFile(char *filname, scene *sc) {

    FILE* fp;
    fp = fopen("objCoordinate.txt", "r");
    if (fp == NULL) {
        printf(" Error!!!\n");
    }

    else
    {
        char lines [Line_Max];

        while (fgets (lines, Line_Max, fp) != NULL) {
             fscanf (fp, "%d", sc->focLen);  //Scan first line into variable

             fscanf (fp, "%f" "%f" "%f" , sc->camP1.x , sc->camP1.y , sc->camP1.z);

             fscanf (fp, "%f" "%f" "%f" , sc->camP2.x , sc->camP2.y , sc->camP2.z);

             for (int i=0 ; i < noVer ; i++)
                     fscanf (fp, "%f" "%f" "%f" , sc->vertices->x , sc->vertices->y , sc->vertices->z);

            for (int j=0; j< noEdge; j++)
                fscanf (fp, "%f" "%f"  , sc->edges->start , sc->edges->end );

        }
    }


    fclose(fp);

    return 0;
}

Can someone help me with it, please?

Dani AI

Generated

is right: the biggest problems are memory and the way you call the scanners. The file you showed can be read deterministically (first integer, two 3-value camera lines, then three vertex lines, then the remaining edge pairs), but your code mixes fgets with fscanf, passes the wrong arguments to fscanf (must use addresses), and never allocates space for the vertices and edges arrays. Also check format specifiers: use %d for ints, %f for floats/doubles.

Use one consistent parsing method (either fscanf sequentially or fgets + sscanf per line). Read the header values first, then allocate arrays, then read into vertices[i] and edges[j] (use &sc->vertices[i].x, not sc->vertices->x). If the file does not include an explicit edge count, read edges in a loop until fscanf returns EOF, growing the array with realloc as needed.

Example pattern (trimmed) — defines simple point3d and edge, reads header, allocates, reads vertices, then reads edges until EOF:

typedef struct { int x,y,z; } point3d;
typedef struct { int start,end; } edge;

int read_scene(const char *file, scene *sc) {
    FILE *fp = fopen(file,"r"); if (!fp) return -1;
    sc->vertices = sc->edges = NULL; sc->noVer = sc->noEdge = 0;

    if (fscanf(fp,"%d", &sc->X) != 1) { fclose(fp); return -2; }
    if (fscanf(fp,"%d %d %d", &sc->camP1.x,&sc->camP1.y,&sc->camP1.z) != 3) { fclose(fp); return -3; }
    if (fscanf(fp,"%d %d %d", &sc->camP2.x,&sc->camP2.y,&sc->camP2.z) != 3) { fclose(fp); return -4; }

    /* if X is the number of vertices in your format: */
    sc->noVer = sc->X;
    sc->vertices = malloc(sc->noVer * sizeof *sc->vertices);
    for (int i=0;i<sc->noVer;i++)
        if (fscanf(fp,"%d %d %d",&sc->vertices[i].x,&sc->vertices[i].y,&sc->vertices[i].z)!=3) { /* handle error */ }

    /* dynamic read of remaining edge pairs */
    size_t cap = 4;
    sc->edges = malloc(cap * sizeof *sc->edges);
    int a,b;
    while (fscanf(fp,"%d %d",&a,&b) == 2) {
        if (sc->noEdge == cap) { cap *= 2; sc->edges = realloc(sc->edges, cap * sizeof *sc->edges); }
        sc->edges[sc->noEdge].start = a; sc->edges[sc->noEdge].end = b; sc->noEdge++;
    }

    fclose(fp); return 0;
}

Troubleshooting tips: always check fscanf return values, initialize pointer fields to NULL before free, and free allocated memory when done. If your coordinates are floats or doubles, switch %d to %f/%lf and change types accordingly.

Recommended Answers

All 2 Replies

what is point3d?

line 31 doesn't work because there is no memory allocated for the pointers. We hve no clue what point3d and edge are so we can't really help you very much, except to say they are pointers which need to be allocated before attempting to read the data into them.

point3d is a struct that has three intger values x,y, and z.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.