locked
Text input question. RRS feed

  • Question

  • Hello everyone..

    I have gotten a silly question: How do I read a text file? I would like not to use C++, but stay with classic C.

    #include <iostream.h>
    FILE* cfg=fopen("poly.cfg",r); // from my old Borland compiler of course doesn't work..


    I wanted to find some tutorials, but all are using c++, also I'm not aware of how exactly does the code work, and what exactly do the classes do.

    I want to read this text file:

    FORMAT:
    {X,Y,Z;    X,Y,Z;    X,Y,Z;    R,G,B}

    POLYGONS:
    12

    {-1,1,1;    1,1,1;    1,-1,1;    255,255,0}    +Z
    {-1,1,1;    -1,-1,1;    1,-1,1    255,255,0}

    {1,1,1;    1,1,-1;    1,-1,-1;    255,0,255}    +X
    {1,1,1;    1,-1,1;    1,-1,-1;    255,0,255}

    {1,1,1;    1,1,-1;    -1,1,-1;    0,255,255}    +Y
    {1,1,1;    -1,1,1;    -1,1,-1;    0,255,255}

    {-1,1,-1;    1,1,-1;    1,-1,-1;    255,0,0}    -Z
    {-1,1,-1;    -1,-1,-1;    1,-1,-1;    255,0,0}

    {-1,1,1;    -1,1,-1;    -1,-1,-1;    255,0,0}    -X
    {-1,1,1;    -1,-1,1;    -1,-1,-1;    255,0,0}

    {1,-1,1;    1,-1,-1;    -1,-1,-1;    255,0,0}    -Y
    {1,-1,1;    -1,-1,1;    -1,-1,-1;    255,0,0}

    and, in case that the file starts with this:

    FORMAT:
    {X,Y,Z;    X,Y,Z;    X,Y,Z;    R,G,B}
    POLYGONS:
    12

    (or any other number instead of 12) and spaces instead of "enter"s, input this, in to this struct:

    typedef float vect0r[4]; //the vector[3] must be 1, always..

    typedef struct
        {
        vect0r cor[3];
        float z;
        int col;
        }flat;

    and for input to col, i will be using function "int makecol(int,int,int)"

    Thanks for everybody for reading..
    Tuesday, May 15, 2007 2:46 PM

Answers

  • I think

    FILE* cfg=fopen("poly.cfg","r")

    is classical more than enough and works fine in Turbo C 2.0, if you replace <iostream.h> to <stdio.h>

    Tuesday, May 15, 2007 2:52 PM
  • first, you need to include stdio.h header file.
    it is this header file which contains FILE STRUCTURE.
    & then you just need to read the file using fgets() or fgetc().

    Tuesday, May 15, 2007 3:02 PM

All replies

  • I think

    FILE* cfg=fopen("poly.cfg","r")

    is classical more than enough and works fine in Turbo C 2.0, if you replace <iostream.h> to <stdio.h>

    Tuesday, May 15, 2007 2:52 PM
  • first, you need to include stdio.h header file.
    it is this header file which contains FILE STRUCTURE.
    & then you just need to read the file using fgets() or fgetc().

    Tuesday, May 15, 2007 3:02 PM
  • Thanks a lot, i was sure it was in iostream.h Indifferent

    I am using VS6
    Tuesday, May 15, 2007 3:07 PM