Thread: fastest way to read / output strings ??

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    fastest way to read / output strings ??

    What is the fastest way to read input from stdin ?
    I tried fread() but when used it does not end the string with '\0' terminator, and the number of read bytes is unknown. It's hard to tame it.
    Please share your knowledge.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Why not use fgets?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by author
    I tried fread() but when used it does not end the string with '\0' terminator, and the number of read bytes is unknown. It's hard to tame it.
    What do you mean the number of read bytes is unknown? If you pass 1 in for the size argument then you know exactly how many bytes were read.
    fread and fwrite return the number of items successfully
    read or written (i.e., not the number of characters). If
    an error occurs, or the end-of-file is reached, the return
    value is a short item count (or zero).
    If the size argument is different than you just multiply fread()'s return value by that size to find out how many bytes were read. Using that you can easily nul-terminate the buffer yourself. The reason fread() doesn't nul-terminate it for you is because fread() is meant to be usable for binary data and you wouldn't want fread() adding 0s to that data.

    If you're going to be getting 1 char at a time then consider using getchar() or if you want to get a line at a time use fgets() like pianorain suggested.
    If you understand what you're doing, you're not learning anything.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What is the fastest way to read input from stdin ?
    Files and speed are not something normally associated - just how fast do you type?
    Multi-Ghz processor, and your 10 chars per second - no contest really is it?

    Even if you're reading from a file, realise that disks are still way slower than a processor reading from memory.


    Here's a tip - get the thing working in the first place using fgets(), then worry about it later when you have something you can actually measure.
    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. how to read strings?
    By happyclown in forum C Programming
    Replies: 10
    Last Post: 01-23-2009, 05:06 AM
  2. How to read the execl() output?
    By estratos in forum C Programming
    Replies: 3
    Last Post: 06-07-2006, 12:39 AM
  3. Help with Strings -- Please read
    By Drakkath in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2002, 10:01 PM
  4. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM
  5. To all who read last post formatted output
    By spliff in forum C Programming
    Replies: 8
    Last Post: 08-21-2001, 03:37 AM