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

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    62

    Best way to print a large amount of text.

    I have been experimenting with C in a cgi environment. What a difference from php.

    Anyway, I'm wondering what's the best way to print a large amount of text? The best I have been able to come up with is:

    Code:
    puts("Some large amoun tof text"
    "And even more text"
    "and still more");
    Is this the best way?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,293
    Normally, I would put all that in a separate text file.

    If you have many messages, you could structure it in some way.
    Code:
    <messages>
      <message id="1" lang="en">Hello</message>
      <message id="1" lang="fr">Bonjour</message>
    </messages>
    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.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    765
    What do you mean by 'large'? Several characters or loads of megabytes of them?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    455
    The printf() family of functions are intended to work with fairly short strings. A modern system will probably scale up to at least INT_MAX characters, but it's not unlikely that you'll end up passing the data to an old system, or to an informally speciifed drop-in replacement which uses a fixed size buffer.

    So if outputting areally long string, it's best to construct it in memory and write it char by char in a tight loop. Remember that if passing to printf(), you need to pass printf("%s", str) rather than printf(str) if the string has any percentage characters in it.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    193
    I'd imagine puts() would be pretty efficient.

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Sorry, I should have included more info.

    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 agree it would be easier to contain the text in a file, but this is a single webpage, and it's easier to remember all the files needed when there's only one.

    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 don't mind it being in the program, as once it is completed, all that should ever be adjusted is contained in the `#defines` (aside from major changes to HTML standard).

    I guess I'll stick to what I have been doing.

    Ty

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,082
    Quote Originally Posted by Yonut
    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 agree it would be easier to contain the text in a file, but this is a single webpage, and it's easier to remember all the files needed when there's only one.

    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 don't mind it being in the program, as once it is completed, all that should ever be adjusted is contained in the `#defines` (aside from major changes to HTML standard).
    For HTML, I think the usual approach is either to serve the HTML pages directly, or if you need to programmatically muck around with the content, to have some kind of HTML-friendly template language with template files that your program reads and performs the template substitutions before serving. It might be easier to remember a single file, but it would also be easier to understand and modify the HTML code if it were separate, and it isn't that hard to remember such HTML (template) files if they are bundled together in the same project directory (or subdirectory thereof).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    765
    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