Thread: Best way to print a large amount of text.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Yonut View Post
    It's for a webpage, and I need to output all the head information (CSS, Javascript, meta tags). I just didn't want to have to type out `printf` for every line.

    I have the webpage currently working, I'm just transfering over to the C program. So a lot of escaping of quotes (") is really all that's needed. I was just hoping there was some version of `heredocs` for C.
    I am assuming the CGI processor spawns a process and, in case of failure (disconnections, for example) it simply kills the process. I don't know how you are dealing with errors in your CGI...

    In that case, to output a large string I think it is better to do it in blocks. For example:
    Code:
    #define CHUNK_SIZE  4096
    
    int outputString( char *strp, size_t size )
    {
      size_t chunks = size / CHUNK_SIZE;
      size_t lastChunk_size = size % CHUNK_SIZE;
    
      while ( chunks-- )
      {
        if ( write( STDOUT_FILENO, strp, CHUNK_SIZE ) < 0 )
          return 0;
        strp += CHUNK_SIZE;
      }
    
      if ( write( STDOUT_FILENO, strp, lastChunk_size ) < 0 )
        return 0;
    
      return 1;
    }
    You can, of course, use bigger "chunks" (I don't recomend more than 64 KiB - write bigger chunks than this will cause some degradation of performance)... Notice the test on `write()`... If it fails, it means data could not be transmitted, since the descriptor 1 (STDOUT_FILENO is defined at unistd.h) will be a socket, not the tty output descriptor. In that case, the funcion will return 0 (false). To transmit the string in chunks gives you a chance to do something in case of errors, faster.

    The way to use such a function is:
    Code:
    static char javascript[] = "var x = 0;\n"
    "function f() ...";
    ...
    if ( ! outputString( javascript, sizeof javascript ) )
    {
      // ... log the error or do anything you want to deal with it...
      exit(1);
    }
    ...
    And keep in mind that you shouldn't mix streamming functions like printf() with write() unless you 'flush' the stream with fflush(stdout) before calling write(). You can use fwrite() instead, but the rule is the same. I prefer to deal directly with the file descriptor, instead of FILE pointer when I want to avoid buffering.


    []s
    Fred
    Last edited by flp1969; 08-27-2020 at 05:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Large amount of data input
    By Rednally in forum C Programming
    Replies: 12
    Last Post: 10-03-2016, 01:41 AM
  2. How to print specific amount of characters per line
    By Yashiiro in forum C Programming
    Replies: 4
    Last Post: 06-13-2016, 11:52 AM
  3. best way to sort large amount of data.
    By jocdrew21 in forum C++ Programming
    Replies: 32
    Last Post: 02-10-2014, 01:11 PM
  4. how to print big amount of number?
    By zzatan in forum C Programming
    Replies: 9
    Last Post: 10-02-2009, 07:46 PM
  5. Read large amount of numbers from file
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 05-10-2007, 09:33 AM

Tags for this Thread