Thread: Reading Strings

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1

    Exclamation Reading Strings

    I'm trying to read in a header for a dat file which explains what the data in each comma delimited record equals. But the header file changes order so what I need to do is read the header file, break it up (comma dilimited) and determine the data in each record that way. But i'm having trouble figureing out how I should read that header line and break it up.

    I've tried doing it this way:

    while (d++ <= 3)
    {
    i = 0;
    while ((C = getc(infile)) != ',')
    {
    p[d].structstring[i] = C;
    i++;
    };
    }

    but it doesn't want to break it at the commas correctly, or it doesn't want to save it to the structure created.

    Please help me!!!! There has to be an easier way of doing this.

    Does anyone have any suggestions?

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This is how to read a simple CSV file
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( ) {
        char buff[BUFSIZ];
        char sep[] = ",\n"; // commas and the newline at the end of fgets buffers
        FILE *fp = fopen( "comma.txt", "r" );
        while ( fgets( buff,BUFSIZ,fp) != NULL ) {
            char *p;
            for ( p = strtok(buff,sep) ; p!=NULL ; p = strtok(NULL,sep) ) {
                printf( "Comma field=%s\n", p );
            }
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading data from strings
    By Winston Hong in forum C Programming
    Replies: 11
    Last Post: 05-28-2008, 08:56 PM
  2. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  3. reading from a file + list of strings
    By stewie1986 in forum C Programming
    Replies: 2
    Last Post: 12-06-2007, 11:59 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM