File I/O

This is a discussion on File I/O within the A Brief History of Cprogramming.com forums, part of the Community Boards category; How can I read several data from a file, which includes one data for each line, and some comments follow ...

  1. #1
    Unregistered
    Guest

    File I/O

    How can I read several data from a file, which includes one data for each line, and some comments follow those data?
    Fox example:
    "input.txt"

    12.3 ;first data use for ....
    34.5 ;second data use for...
    90.67 ;3rd data use for...
    12098 ;4th data use for...
    กกกก

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Like so
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( ) {
        FILE *fp = fopen( "input.txt", "r" );
        char buff[BUFSIZ];
        while ( fgets(buff,BUFSIZ,fp) != NULL ) {
            float var;
            char *comment = strchr( buff, ';' );
            if ( comment != NULL ) *comment = '\0'; // blow away the comment
            if ( sscanf( buff, "%f", &var ) == 1 ) {
                printf( "Data=%f\n", var );
            } else {
                printf( "Garbage in %s", buff );
            }
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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