Thread: write vs fprintf

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    write vs fprintf

    Can I use the write function instead of fprintf? And if so, is there an advantage besides write not being buffered? Any insight is appreciated, thanks.
    Last edited by Annonymous; 06-08-2011 at 10:14 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you mean 'write' or 'fwrite'? In either case, they're going to function nearly the same, but both will function differently than *printf would. Say you wanted to write out a string that you had inserted a number into the middle:
    Code:
    fprintf( file, "Hello %d world!", 10 );
    To do the same thing with fwrite, you would have to prepare the string "Hello 10 world!" first, then write out that many bytes from the buffer containing that string.

    That isn't to say you can get the same job done using both, but they function differently. For example, if you wanted to write out the number 10 as text, you would need to (as I mentioned in the earlier example) convert it to text, and fwrite out the two bytes that made up the text for "10". But if you wanted to actually write an integer which contained the value of 10 out, you could do so using fwrite and it would take up a different amount of space depending on the size of the object you were writing out, and, it wouldn't necessarily appear as humanly readable if you opened the file up in a text editor.

    While they can do the same job, they are generally used to write out different types of data in different ways from each other.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Using fwrite() to output text is probably silly. It will give an output that looks funny when read in and displayed by text editors, because the default character sequence to represent end of line varies between operating systems (notably: it is different between windows and most unix variants). Some text editors correct the way they display data for that, but a lot do not.

    fwrite() is more usually used to output the machine representation of whatever is being output. For example, assuming our compiler supports 8-bit characters and a 32-bit int type (which a lot of systems do), an int can be output as four (often non-printable) bytes to the file.

    The main advantage of using fwrite() is often a smaller output file than would be achieved for formatted output with fprintf(). For example, formatted output of a 32-bit int is larger than four bytes for any value greater than 9999 or less than -999 (assuming base 10 output).

    One disadvantage of using fwrite() is that (unless printable strings are output) humans find the output files difficult to read.

    A key disadvantage of using fwrite(), however, is that there is no guarantee (unless one uses specialised encoding schemes) that the output can be read in again (using fread()) unless the code is compiled using the same compiler and run on the same operating system as the code that originally output the data. This is because the size and representation of data types in memory are compiler (and often operating system) dependent.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Thank you grumpy and quzah. Much appreciated

  5. #5
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Just to clarify so I do not look like a "complete" moron... which I probably already do. I would never use the write function instead of fprintf when working with file i/o or printing text. Like you said grumpy, that would just be silly. I was actually talking about using fprintf to write buffered strings over a tcp socket. I was just curious to know if using fprintf could be used at all with writing over a socket and if doable, would it be efficient? And would it have an advantage over using the write function. But I will just stick with the write function, as my book uses it for a reason.
    Last edited by Annonymous; 06-09-2011 at 11:42 PM.

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Its not a good idea to use fwrite or other such f* functions to write
    to a socket as those functions expect a FILE pointer as their file descriptor where as a socket is normally represented as a simple "int"

    Socket/network code is specific to the OS...so thats why you have to use the OS supplied routines to read/write to them...on *nix machines you can use read/write or send/recv as *nix treats sockets as file descriptors...on windows however it is entirely different...

  7. #7
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Nonpuz, I am aware network programming is platform dependant. As well as the routines used to write over a socket for Linux. I just wanted to know if using fprintf over a Linux tcp socket instead of the write/send function was possible. You answered my question spot on tho. I guess my question was that dumb, I must have looked just that, dumb lol Thanks man, much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket question
    By GReaper in forum Networking/Device Communication
    Replies: 7
    Last Post: 06-10-2011, 06:46 AM
  2. Socket Question
    By BENCHMARKMAN in forum C Programming
    Replies: 15
    Last Post: 03-12-2008, 09:57 PM
  3. Socket Question
    By DaveHope in forum Networking/Device Communication
    Replies: 12
    Last Post: 06-21-2005, 04:50 PM
  4. Socket question
    By ~Kyo~ in forum Game Programming
    Replies: 4
    Last Post: 11-04-2004, 06:25 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM