Thread: reading table with chemical elements in structure

  1. #1
    Registered User
    Join Date
    Dec 2017

    reading table with chemical elements in structure


    I am new to c and I try to write a script that reads chemical elements into a structure, write to a binary file, where the elements are classified according to the number in the third column. I should try to do that with fseek function.
    I could write the elements into a structure in binary file, but don't succeed to classify on the 2nd column.

    1. input file with element

    Actinium Ac 89 227.0278
    Aluminum Al 13 26.981539
    Americium Am 95 243.0614
    Antimony Sb 51 121.760

    2. script

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct Element {
        char nom[44];
        char symbole[4];
        int numeroatomique;
        double masseatomique;
    typedef struct Element FICHE;
    int main() {
        char line[300];
        FICHE element  = {"","",0,0};
        char nom[44];
        char symb[4];
        FILE *pf = NULL;
        FILE *mendelf;
        if ( ( pf = fopen ( "tabletriee.txt", "r")) == NULL) {
            printf ( "could not open file\n");
            return 1;
        if ( ( mendelf = fopen( "elements.DAT", "wb")) == NULL) {
            printf ( "could not open file\n");
            return 1;
        while ( ( fgets ( line, sizeof ( line), pf))) {
            if ( ( sscanf ( line, "%44s %4s %d %lf", element.nom, element.symbole, &element.numeroatomique, &element.masseatomique)) == 4) {
                fwrite(&element, sizeof(element), 1, mendelf);
        fclose (pf);
        fclose (mendelf);
        int rec;    
        mendelf = fopen("elements.DAT","rb");
         printf("%s\t%s\t%d\t%lf\n", element.nom, element.symbole, element.numeroatomique, element.masseatomique);            
       return 0;    

    3. expected output

    Aluminum Al 13 26.981539
    Antimony Sb 51 121.760
    Actinium Ac 89 227.0278
    Americium Am 95 243.0614

    Thanks for helping!!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Why don't you save them all in an array, call qsort on it, and then output then to the binary file.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    From the description, you need to do
    fseek (sizeof(struct Element)*element.numeroatomique)

    before each read or write.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MPI_Scatter structure elements
    By Chris_1980 in forum C Programming
    Replies: 0
    Last Post: 06-30-2010, 04:46 AM
  2. giving value to structure elements
    By sarathius in forum C Programming
    Replies: 3
    Last Post: 04-21-2008, 01:24 AM
  3. Table elements compare...
    By ihtus in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2006, 09:30 AM
  4. New Chemical Element Found
    By Salem in forum A Brief History of
    Replies: 7
    Last Post: 12-18-2005, 10:10 PM
  5. How to compare structure elements
    By khpuce in forum C Programming
    Replies: 6
    Last Post: 04-10-2005, 11:40 AM

Tags for this Thread