Laoding a structure

This is a discussion on Laoding a structure within the C Programming forums, part of the General Programming Boards category; Sorry about the last structure post. I was trying to just load the structure and go from there, but it ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Laoding a structure

    Sorry about the last structure post. I was trying to just load the structure and go from there, but it was a very poor example.

    The program attached compiles with no errors and no warnings. Basically I am trying to load the class.dat file into a structure. The class.dat file is delimited by a pipe "|".

    LIKE THIS:
    001635223|Stevens, Bill R.|COSC1400| |004760097|Smith, William O.|COSC1401| |006238033|Smithson, Thelma R.|ITSC1401| |


    I have tried several ways to load this file into the "student" structure. The attached code is the closest that I have come. I sent the data to a report.dat file just to view the output and this is what it looks like:

    001-63-5223 Stevens, Bill R COSC140 | |
    047-60-097| mith, William OCOSC140 | |
    062-38-033| mithson, Thelma RITSC140 | |
    103-54-130| oodsen, Richard UITSC140 | |


    The pipes are still there and the data is corrupted. I AM LOST!

    Could anyone please help clarify my error(s)? If you know a better way, please give a brief example.

    Thanks,
    Alan
    Attached Files Attached Files

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    001635223|Stevens, Bill R.|COSC1400| |004760097|Smith, William O.|COSC1401| |006238033|Smithson, Thelma R.|ITSC1401| |
    Ok, so fields are delimited by |
    but are records delimited by newline (or anything)?

    Eg, by newlines, the file would look like
    001635223|Stevens, Bill R.|COSC1400| |
    004760097|Smith, William O.|COSC1401| |
    006238033|Smithson, Thelma R.|ITSC1401| |

    In addition, what does "| |" mean - end of record, or just an empty field (containing what appears to be a space)

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    37
    No, the record does not contain a newline/carriage return ''\n'

    The | | is a 12 space field that is reserved for future use.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Here's something

    Code:
    #include <stdio.h>
    
    // This uses a static buffer - you must use the result before
    // calling this function again, otherwise the first result will be lost
    // it also doesn't handle || as being an empty field
    char *gettok( FILE *fp ) {
        static char buff[200];
        int i = 0;
        int ch;
    
        while ( (ch=fgetc(fp)) != EOF ) {
            if ( ch == '|' ) {
                break;          // end of field marker
            } else {
                buff[i++] = ch; // save the char
                buff[i] = '\0'; // and keep the buffer \0 terminated
            }
        }
        if ( i == 0 ) {
            return NULL;    // first read was EOF (or |EOF)
        } else {
            return buff;
        }
    }
    
    int main ( ) {
        FILE *fp = fopen( "member.txt", "r" );
        char *c;
        while ( (c=gettok(fp)) != NULL ) {
            printf( "Found '%s'\n", c );
        }
        fclose( fp );
        return 0;
    }
    Make sure it does all you want it to do, before burying it inside some larger program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21