Formatted & Unformatted Input / Output - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Formatted & Unformatted Input / Output

tokoGtokoG Posts: 209Member
Hi

Studying thru the Input & Output functions.

Unformatted I/O functions...(such as getchar, fgets)
Formatted I/O functions.... (such as scanf, fscanf)

But,,, cant quite put my fingure on the differences between unformatted I/O and formatted I/O.


printf is formatted output. Is it because whatever you write between the
" " can be printed out (output) to the screen and that is refered as "being formatted"??

getchar is UNformatted input. Is it because it takes only one char each? (or did it take string also..?)

I am learning the 'details' of formatted and unformatted I/O but not quite sure about the most important thing, what is unformatted and formatted anyway?

Would anyone explain me or know a good web page to explain this..?

Thank you!

Comments

  • LundinLundin Posts: 3,711Member
    : Hi
    :
    : Studying thru the Input & Output functions.
    :
    : Unformatted I/O functions...(such as getchar, fgets)
    : Formatted I/O functions.... (such as scanf, fscanf)
    :
    : But,,, cant quite put my fingure on the differences between unformatted I/O and formatted I/O.
    :
    :
    : printf is formatted output. Is it because whatever you write between the
    : " " can be printed out (output) to the screen and that is refered as "being formatted"??
    :
    : getchar is UNformatted input. Is it because it takes only one char each? (or did it take string also..?)
    :
    : I am learning the 'details' of formatted and unformatted I/O but not quite sure about the most important thing, what is unformatted and formatted anyway?
    :
    : Would anyone explain me or know a good web page to explain this..?
    :
    : Thank you!
    :


    Yep, formatted I/O would probably be those functions accepting a format string: that is the first parameter in printf/scanf etc:

    printf("%s %d I am the format string", str, i);
  • tokoGtokoG Posts: 209Member

    :
    :
    : Yep, formatted I/O would probably be those functions accepting a format string: that is the first parameter in printf/scanf etc:
    :
    : printf("%s %d I am the format string", str, i);
    :

    Hi Lundin

    Thanks doe the reply.

    For example, looking the prototype of [b]putc(int c, FILE* stream)[/b].
    This is to [b]Writes one btye to [italic]stream[/italic]. I thought the [b]stream[/b] is the string but not? It could be a character one by one? (the stream composed by characters, one by one, but NOT as a string)

    Another example, [b]gets(char* s)[/b]. This is to [b]Reads one line from standard input. Removed the ending newline character[/b].
    This is also a function for [b]unformatted[/b] I/O but, it takes [b]string[/b]. I thought [b]strings[/b] were formatted I/O..?



  • LundinLundin Posts: 3,711Member
    [b][red]This message was edited by Lundin at 2006-4-3 23:23:23[/red][/b][hr]
    :
    : :
    : :
    : : Yep, formatted I/O would probably be those functions accepting a format string: that is the first parameter in printf/scanf etc:
    : :
    : : printf("%s %d I am the format string", str, i);
    : :
    :
    : Hi Lundin
    :
    : Thanks doe the reply.
    :
    : For example, looking the prototype of [b]putc(int c, FILE* stream)[/b].
    : This is to [b]Writes one btye to [italic]stream[/italic]. I thought the [b]stream[/b] is the string but not? It could be a character one by one? (the stream composed by characters, one by one, but NOT as a string)
    :
    : Another example, [b]gets(char* s)[/b]. This is to [b]Reads one line from standard input. Removed the ending newline character[/b].
    : This is also a function for [b]unformatted[/b] I/O but, it takes [b]string[/b]. I thought [b]strings[/b] were formatted I/O..?
    :
    :

    [/b]
    putc() is for character, puts() for string. Same applies to getc() and gets(). Note that getc(stdin) is the same as getchar().

    To write to the screen, use putc() like this: putc('A',stdout);
    To write to a file, replace stdout with a FILE pointer.

    With format string, I don't mean just any string, I mean a string containing things like %d %s etc. I assume that is what they mean with "formatted I/O". printf() will parse through the string you pass to it, replacing those symbols with values. puts() will only print "%d %s" on the screen.


  • tsagldtsagld Posts: 621Member
    Formatted I/O means that presenting data in another form than it exists with.
    For example, in memory I have the integer 321897, which is stored in four bytes (on a 32-bit machine) in binary.
    Presenting this value to the user, on-screen for example, requires reformatting the four bytes to a readable 6-digit string:
    [code]
    int a=321897
    printf("%d", a);
    [/code]

    Another example is reformatting a string:
    [code]
    char* a="18";
    printf("32%s97", a);
    [/code]

    You see the use the %d and %s formatting characters. There are many more.
    Hope this helps,

    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • tokoGtokoG Posts: 209Member

    : putc() is for character, puts() for string. Same applies to getc() and gets(). Note that getc(stdin) is the same as getchar().
    :
    : To write to the screen, use putc() like this: putc('A',stdout);
    : To write to a file, replace stdout with a FILE pointer.
    :
    : With format string, I don't mean just any string, I mean a string containing things like %d %s etc. I assume that is what they mean with "formatted I/O". printf() will parse through the string you pass to it, replacing those symbols with values. puts() will only print "%d %s" on the screen.
    :
    :
    :

    The use of convension specifiers! (%d, and %s)
    I was gonna ask this because I was just reading the chapter to explain more about what is formatted I/O!! Because my tutorial books explains UNformatted I/O first, I couldn't figure out what then formatted ones and what are the differences!!

    Thanks, now with your explanation I understand.
    :)

  • tokoGtokoG Posts: 209Member
    : Formatted I/O means that presenting data in another form than it exists with.
    : For example, in memory I have the integer 321897, which is stored in four bytes (on a 32-bit machine) in binary.
    : Presenting this value to the user, on-screen for example, requires reformatting the four bytes to a readable 6-digit string:
    : [code]
    : int a=321897
    : printf("%d", a);
    : [/code]
    :
    : Another example is reformatting a string:
    : [code]
    : char* a="18";
    : printf("32%s97", a);
    : [/code]
    :
    : You see the use the %d and %s formatting characters. There are many more.
    : Hope this helps,
    :

    Thankd for this. Now I see why my tutorial book started this section wil explaining bits and bytes. I will print this post and paste on my tutorials. :)



  • tokoGtokoG Posts: 209Member

    : : Another example is reformatting a string:
    : : [code]
    : : char* a="18";
    : : printf("32%s97", a);
    : : [/code]
    : :

    Btw, could you tell me why you used the pointer (*) for [b]char a[/b]?
    I mean, of course I tried (to execute) the code [b]without[/b] but didn't work.. Because you can't use the [b]digits[/b] on string between " "..? (I thought we could)

    Thanks!
  • LundinLundin Posts: 3,711Member
    :
    : : : Another example is reformatting a string:
    : : : [code]
    : : : char* a="18";
    : : : printf("32%s97", a);
    : : : [/code]
    : : :
    :
    : Btw, could you tell me why you used the pointer (*) for [b]char a[/b]?
    : I mean, of course I tried (to execute) the code [b]without[/b] but didn't work.. Because you can't use the [b]digits[/b] on string between " "..? (I thought we could)
    :
    : Thanks!
    :

    Because it is a string, not a char.
    It is the same as writing char a[] = "18" or char a[3] = "18".
Sign In or Register to comment.