Thread: Making a string from a text file after a specific text

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    10

    Making a string from a text file after a specific text

    I'm trying to create a string from a text file that pulls the characters after a specific sequence of characters to use as an output. Specifically, all the important characters in this text are proceeded by <1>, and end with an endline.
    I currently am using fscanf, but it doesn't appear to be working:

    fscanf(calibrate, "*[<1>] %s", enz);

    where calibrate is the file I'm reading from (already opened), and enz is the array I want filled. Any advice would be appreciated. I already have a while loop in place to make sure I'm not at the end of the file.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    fscanf() is really for computer generated files that have a specific formatted input. If you are searching for <1> in the middle of a line it will probably just get stuck.
    Code:
    while ( fgets( line, sizeof line, calibrate ) != NULL ) {
       char *ord = strstr( line, "<1>" );
       if ( ord != NULL ) {
          erz[0] = '\0';
          strncat( erz, ord, sizeof(erz) - 1);
          break;
       }
    }
    This is a bit more thorough search that will correctly find content such as

    Lorem ipsum <1> dolor sit amet\n

    as long as attention is paid to things like line length and the size of erz.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    10
    I see how this could work, but it isn't helpful in this context since the important text is right after the <1> flag. Since "calibrate" is a text file, fscanf ends up working fine in the format:

    fscanf(calibrate, "%s", enz);

    but has the useless <1> text at the start of the output, which needs to be missing in the final program.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I guess you want to start copying at ord + 3 (i.e. the length of "<1>") then. Finding the token is the important thing.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,733
    You're making it way too complicated.
    Code:
    #include <stdio.h>
    
    int main()
    {
      char test[] = "<1>hello";
      char result[10];
      int n = sscanf(test,"<1>%s",result);
      printf("n=%d, result=%s\n",n,result);
      return 0;
    }
    
    
    $ gcc -Wall bar.c
    $ ./a.out 
    n=1, result=hello
    Aside from the % character, every character stands for itself in a format string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2016, 12:53 PM
  2. parsing specific text from xml file
    By rabers in forum C Programming
    Replies: 5
    Last Post: 03-07-2011, 02:14 AM
  3. Compare text string to a specific character.
    By Danne in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2011, 09:57 AM
  4. Replace wildcard text in file with specific text.
    By untz in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2010, 06:35 PM
  5. Searching a specific string/word/etc in a text file?
    By zacharyrs in forum C Programming
    Replies: 5
    Last Post: 11-29-2009, 07:54 PM

Tags for this Thread