Thread: display the content of a file in a program

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    display the content of a file in a program

    Hi,

    I have a question concerning file and structure. My file is REGLECO.DAT
    Here's a part of it's content

    Code:
    sIdentif   fRegle    sType   sDescript
    HRSUP      40.0000   CIE     Nm. hres pour temps supplementaire
    TXSUP      1.5000    CIE     Taux pour heures supplémentaire
    TXSYN      0.0078    CIE     Taux cotisation syndicale
    And this is what my program originally display (word are located with gotoxy(x,y))
    Code:
        Identification    Regle      Type    Description
    What I want is to read my file (using structure if possible) and that when someone open this part of my program, it display
    Code:
    Identification  Regle     Type    Description
    HRSUP           40.0000   CIE     Nm. hres pour temps supplementaire
    TXSUP           1.5000    CIE     Taux pour heures supplémentaire
    TXSYN           0.0078    CIE     Taux cotisation syndicale
    This part of my program only display information, nothing more. So, how do I read my file REGLECO.DAT and put all the info in a structure (because i'm new with this notion) and after, display on the screen the info like in the last box.

    Thanks

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I suppose from the extension .DAT that its a binary file you're talking about. In that case, you need the exact definition of your structure to be able to read them from the file, something like the code below:
    Code:
    struct myStruct{
        char ident[6];
        double regle;
        char type[4];
        char description[32];
    }
    The problem is that without knowing how your structure looks, you won't read the correct bytes from the file and won't be able to display the content correctly. Did you generate that file and if yes, do you know how the bytes inside are made up?

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Yes, I've generated that file, I don't know how the bytes inside are made up. I've created it with notepad and put the content in it and saved as a .dat.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Oh, in that case it's a little easier to do what you want. Do you want to:

    - only read the entire lines and write them to the screen
    or
    - read a line, separate the different values into specific variables or structures and generate the output from there ?

    In the first case, you just open the file in text mode and read the lines using fgets() and then print them to the screen:

    Code:
    /* fgets exmaple */
    #include <stdio.h>
    
    int main()
    {
       FILE * pFile;
       char string [BUFSIZ];
    
       pFile = fopen ("REGLECO.DAT" , "r");
       if (pFile == NULL) perror ("Error opening file");
       else {
         while (fgets (string , BUFSIZ, pFile) != NULL)
         {
             puts (string);
         }
         fclose (pFile);
       }
       return 0;
    }
    In the second case, you still open the file in text mode and read the lines with fgets(), but you also use sscanf() to extract the different values, you create a structure and assign the values to the structure attributes, you put the structures into a list and finally you iterate over the list and generate the output.
    Last edited by KONI; 04-07-2007 at 09:54 AM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Thanks KONI, I think I'll only read the entire lines and write them after on the screen. Do I just have to read the lines and then write them on the screen with gotoxy(x,y), or is there something else I should do?

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I think, as does the majority of the people on this forum, that you should avoid using gotoxy() at all, since it's non standard. Just copy the code I posted, which should display the entire content of the file already. From there, modify the display as you please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM