using fread to read from stdin

This is a discussion on using fread to read from stdin within the C Programming forums, part of the General Programming Boards category; I have: Code: u_char body[256]; flag = fread(body, 1 , sizeof(stdin), stdin); I am not sure about the sizeof(stdin), how ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    using fread to read from stdin

    I have:


    Code:
    u_char	body[256];
    flag = fread(body, 1 , sizeof(stdin), stdin);
    I am not sure about the sizeof(stdin), how can I specify the size that the user inputs there into the size_t count parameter of fread?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can't, you have to decide upon an upper limit. Since fread returns the number of bytes read, you can use it in a loop until it has got everything out of the keyboard buffer (this will work fine, eg., if you set a limit of 6 and I type "this and that", it will loop thru three times, the last time only reading one or two bytes). Obviously you must do something with "body" in the loop or the data will be meaninlessly overwritten (you could collate a larger string, for example).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    hmmm.. not sure that I understand what you mean, can you try to code what you meant...

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Actually, fread() with stdin works best byte by byte so you can pick up the newline and not be left waiting:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	int i=0;
    	char buffer[4096], byte;
    
    	while ((fread(&byte,1,1,stdin))) {
    		if (byte=='\n') break;
    		buffer[i]=byte; 
    		i++; }
    
    	buffer[i+1]='\0'; /* null terminate */
    	printf("You entered: \"%s\"\n",buffer);
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    if I do something like this:

    fread(body, 1 , 256, stdin);

    then will fread wait till there's 256 byte from stdin? in other words 256 characters?

    what if I type in:

    testing

    and I pressed enter, literally testing size is less than 256

  6. #6
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    . . . then it will continue to wait until 256 characters are in the buffer, I do believe. Why not try it yourself?

    You may be wanting fgets instead?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by Nightowl View Post
    . . . then it will continue to wait until 256 characters are in the buffer, I do believe. Why not try it yourself?

    You may be wanting fgets instead?
    I've tried it my self and not sure if that's what it does.. so therefore it will keep on waiting till I enter 256 characters and proceed to the next code in line?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by -EquinoX- View Post
    I've tried it my self and not sure if that's what it does.. so therefore it will keep on waiting till I enter 256 characters and proceed to the next code in line?
    Yes, which means if less than 256 is entered, you will wait forever.

    All of these functions actually use fgetc internally, I think, which really you could just fgetc here, it amounts to the same idea.

    fgets() works too.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    . . .

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        char s[128] = {0};
        fread(s, sizeof(char), 128, stdin);
        puts(s);
        return 0;
    }
    . . . gives you . . .

    Code:
    a
    b
    c
    d
    e
    f
    <CTRL-D>
    a
    b
    c
    d
    e
    f
    So, no, it will go until an EOF is detected, *or* 128 characters are entered.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    ok if an EOF occured first therefore char s[128] is not fully used 128 bytes right? so how do we know how much space s is used if an EOF occured?

  11. #11
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Good question. I've no idea . . .

    Well, it's a safe bet, that if fread returns EOF, then you've got less than 128 chars in there . . .

    But, since the puts works correctly, I'd say it's nice enough to stick a NULL element onto the end of the array.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I think fread does actually return the number of characters/size that it has successfully read... do you mind checking what fread returns when an EOF happened first in your example above?

  13. #13
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    It returns the number of characters (excluding the EOF) that were successfully read.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    also is there any way to check that fread actually hits an EOF

  15. #15
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    If the return value of fread is less than the total number that you asked for, then you're looking at an EOF being entered.

    Make sense?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  3. Question About Reading Lines from stdin
    By Zildjian in forum C Programming
    Replies: 7
    Last Post: 11-12-2003, 04:45 AM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 02:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21