Scanning a line in a document

This is a discussion on Scanning a line in a document within the C Programming forums, part of the General Programming Boards category; Can anyone help me with the code for scanning a line in a document. Here is an example of the ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    8

    Scanning a line in a document

    Can anyone help me with the code for scanning a line in a document.

    Here is an example of the document:

    Something here (Varies from 100-500 lines)

    2001.12.25 (time) Start Time
    Weather for 2001.12.25 (number)

    Something here (Varies from (10-50 lines)

    And I need to scan the "Start Time" and "Weather" lines for the "time" and "number".

    Here is the code im using:

    sscanf(starttime, "2001.12.25 %d Start Time",&stime);
    sscanf(weather, "Weather for 2001.12.25 %d",&wthr);

    If my document only contains the 2 lines it scans them. But if contains other lines before and after it won't. Do any of you know a way to help?

    Thanks, Will be waiting for responses.
    BC

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Well, you could search for the string "weather" and "start time" in the file.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use fgets in a loop to read a line at a time into a buffer. Then use sscanf on the buffer to search for your text.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    8
    I used a for loop and used fgets and sscanf, but it doesnt seem to scan in the values I need.

    Here is the code im using:

    lines = # of lines in file
    readfile is a FILE *

    for(i = 0; i < lines; i++)
    {
    fgets(starttime, sizeof(starttime), readfile);
    sscanf(startime, "%d Start Time", &stime);

    fgets(weather, sizeof(weather), readfile);
    sscanf(weather, "Weather %d", &wthr);
    }

    Can anyone help me with the code?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Code:
    #include <stdio.h>
    #include <string.h>
    
    // Something here (Varies from 100-500 lines)
    // 2001.12.25 (time) Start Time 
    // Weather for 2001.12.25 (number)
    // Something here (Varies from (10-50 lines)
    
    int main ( ) {
        char buff[BUFSIZ];
        FILE *fp;
        int  stime, wthr;    // surely they're more than just ints?
    
        // open the file, check for errors
        fp = fopen( "file.txt", "r" );
        if ( fp == NULL ) {
            perror( "Cannot open file" );
            exit( 1 );
        }
    
        // read each line
        while ( fgets(buff,BUFSIZ,fp) != NULL ) {
            // check each line, to see if it contains our magic words
            if ( strstr(buff,"Start Time") != NULL ) {
                // a start time, extract the data
                // %*s means skip a string - RTM for more details
                if ( sscanf(buff, "%*s %d Start Time",&stime) == 1 ) {
                    printf( "Time Value=%d\n", stime );
                }
            }
            if ( strstr(buff,"Weather for") != NULL ) {
                if ( sscanf(buff, "Weather for %*s %d",&wthr) == 1 ) {
                    printf( "Weather Value=%d\n", wthr );
                }
            }
        }
    
        fclose( fp );
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    8
    Thanks for the help,

    Program seems to be working fine now.

    BC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  2. Help displaying line numbers of a text document
    By Japatron in forum C Programming
    Replies: 4
    Last Post: 05-04-2006, 01:34 PM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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