Thread: File Handling Problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    File Handling Problem

    Hi All,

    I'm trying to process a text file. My file has a line format like the following:

    'some text' bnm 'some numbers'

    This line format repeats itself until the end of the text. The thing I need is 'some numbers'.

    What I want to do is:
    - scan every line for bnm
    - when I find bnm, I want to read 'some numbers' after the bnm and write them to another file in the same order.
    - this will go on like this until the end of the file.

    I tried really hard, but I couldn't do it. I need some advice about which i/o function to use, some kind of algorithm perhaps. Any idea?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Does the 'some text' potentially contain bnm? If not then it's really a simple matter of shifting forward to 'some numbers', since all you're doing is writing everything after bnm to the output file:
    Code:
    while ( fgets ( line, sizeof line, in ) != NULL ) {
      char *p = strstr ( line, "bnm" );
    
      if ( p == NULL )
        panic();
    
      while ( isspace ( *p ) )
        ++p;
    
      fputs ( p, out );
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    No, 'bnm' is unique. The part of the line I'm interested in is actually like this:

    ...\tN 4021.156431\t\t\t\tE 2945.142006 and continues

    ('\t' stands for horizontal tab)

    I want to take out the numbers from the file. Algorithmically I can say, when I meet '\tN ', I know that I want the next 11 characters and when I meet '\tE ' again 11 characters after that should be read and written somewhere else.

    Prelude, thanks for your example, it will be helpful for me.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Prelude
    Does the 'some text' potentially contain bnm? If not then it's really a simple matter of shifting forward to 'some numbers', since all you're doing is writing everything after bnm to the output file:
    Code:
    while ( fgets ( line, sizeof line, in ) != NULL ) {
      char *p = strstr ( line, "bnm" );
    
      if ( p == NULL )
        panic();
    
      while ( isspace ( *p ) )
        ++p;
    
      fputs ( p, out );
    }
    You'd want to increment p by 3 (strlen("bnm")) before the while, otherwise p will be pointing to the 'b' character in "bnm", and the isspace(*p) will immediately fail, rendering the while ( isspace... ) pointless.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You'd want to increment p by 3
    Yes, thanks. I always get burned when I write code while distracted. You'd think I'd know better by now.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  4. Problem while dealing with file handling functions
    By RoshanGautam in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 01:42 AM
  5. file handling and function problem.
    By prepare in forum C Programming
    Replies: 7
    Last Post: 10-09-2004, 02:26 AM