Thread: Help reading file and adding html tags

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Help reading file and adding html tags

    I'm still pretty new to C, so please bear with me...
    I am trying to read a file, parse it's contents and add html tags around each word. For example:

    Code:
    Text file contents...
    This is a test
    This is the next line
    C program to convert to...
    Code:
    <tr>
      <td>This</td> <td>is</td> <td>a</td> <td>test</td>
    </tr>
    <tr>
      <td>This</td> <td>is</td> <td>the</td> <td>next</td> <td>line</td>
    </tr>
    I am a perl guy and I am writing a database application in C - mainly to learn the language. What I'm not sure of is what file io functions I should be using or which would be most efficient. Maybe I should be using some kind of pattern matching here like you would see in perl?
    Here is my actual code - nothing new here, just file io...
    Code:
      while ( fscanf( fp, "%s", readline ) != EOF ){
        if(match(readline,"\r")){   // silly attempt at regex
          fprintf(cgiOut,"%s",readline);
        }
      }
    I can't seem to find the carriage return using this method. Here is the "match" function that I found on the web - doesn't seem to function as the $var =~ // in perl.

    Code:
    int match(char *string, char *pattern){
       int status;
       regex_t re;
       if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0)  {
         return(0) ;
       }
       status = regexec(&re, string, (size_t) 0, NULL, 0);
       regfree(&re);
       if (status != 0)  {
         return(0) ;
       }
       return(1);
    }
    Please help, I'm pulling my hair out. If you're in the Dallas area, I'll buy you a beer!

    Thanks,
    Derek

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The best way to read in one line at a time would probably be to use fgets(). Then you can use a function like strtok() to break that line into individual words. At that point you could just print each word with the surrounding HTML tags. You can use strchr() to see if the word contains a newline character.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Thanks - that worked great. I guess I owe you a beer!!!

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm not in the Dallas area
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM