Thread: Copy defined parts from text-file

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14

    Copy defined parts from text-file

    I need copy defined parts from input.txt file to new output.txt file.
    These parts started with text-string "start" and finished with text-string "stop".
    Eg. input.txt file:
    123
    789
    start
    ab 1
    bc 2
    ab 4
    yy 5
    ab 5
    stop
    001 some text...
    354
    start
    ab A
    ln #
    ab B
    stop
    qwerty
    m 021
    Output.txt file:
    145
    A#B
    I found only parts of solution:
    Variables: line = line-string, fr = input.txt, fw = output.txt, ab = "ab ", start = "start", stop = "stop" :
    Code:
    while (fgets(line, 80, fr) != NULL)  /*write each line of input.txt to variable "line" until EOF*/
    if (strstr(line, start) != NULL)  /*find start-string, but "if" is wrong!*/
    if (strncmp(line, ab, 3) == 0) putc(line[2],fw);  /*write 3rd charecters from line to output.txt*/
    Could you help me, please how can I read strings from input.txt only between strings start and stop?
    I think that it can be possible to use another while-loop but I am not yet successful.
    Thank you.

  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
    Code:
    while (fgets(line, sizeof line, fr) != NULL) {
      if ( strstr( line, "start" ) != NULL ) doCopy = true;
      if ( strstr( line, "stop" ) != NULL ) doCopy = false;
      if ( doCopy ) {
      }
    }

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14
    It is very elegant solution .
    I do not know how to declare doCopy, true, false.
    Or which header file I must add. I am using now only <stdio.h> and <string.h> in this source.
    I am trying to find information elsewhere on board...

  4. #4
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14
    Could you tell me, please what I have study for to make this code from Salem with doCopy, true, false?
    Eg. on http://www.cprogramming.com/tutorial.html
    I do not know if doCopy is function or enum..
    Definition of true and false can be with
    Code:
    #define false 0
    #define true 1
    I cannot find some example.
    Please, write me some link where can I find information.
    Thanks for help.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can use #defines like you do, or an enum

    Code:
    int doCopy;
    or
    enum { false, true } doCopy;

  6. #6
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14
    It is GREAT, thank you very much Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to copy a word or numbers from text to other location
    By trancedeejay in forum C Programming
    Replies: 12
    Last Post: 02-09-2006, 06:43 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Function is called and I am trying to open a file
    By tommy69 in forum C Programming
    Replies: 88
    Last Post: 05-06-2004, 08:33 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM