Thread: Parsing specific data from one text file to another..

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    Parsing specific data from one text file to another..

    Hello all,

    Thank you in advance for any help you can provide. This is my second plee for help I haven't programmed in years, but was asked by my boss to come up with a simple program to parse some information. Basically, we will pass through the command line: Program [infile] [outfile].

    The infile looks similar to this:

    Code:
    info 
    info
    info
    info
    
    START
    123456       garbage
    654321       garbage
    Hello        garbage
    Goodbye      garbage
    All the "info" can be ignored. As has to be the "garbage". The info that has to be collected always begins below the "START" statement. I'm looking to collect all data below the "START" that begins in the 0 column. Anything else on an individual line should be ignored except the first string. What the result file should look like is simply:


    Code:
    123456
    654321
    Hello
    Goodbye

    I have no problem opening and writing to a file (I don't believe) but was looking for some pointers as to how to pull the first column string and only that, once past the START marker.


    Thanks again.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe something like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char text[12];
          int ready = 0;
          while ( fscanf(file, "%11s%*[^\n]%*c", text) == 1 )
          {
             if ( ready )
             {
                puts(text);
             }
             else if ( strcmp(text, "START") == 0 )
             {
                ready = 1;
             }
    
          }
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* file.txt
    info
    info
    info
    info
    
    START
    123456       garbage
    654321       garbage
    Hello        garbage
    Goodbye      garbage
    */
    
    /* my output
    123456
    654321
    Hello
    Goodbye
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Thanks a million Dave. I will work on the in/outfile portion. I'll let you know if i hit a snag. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM