stdin size problem

This is a discussion on stdin size problem within the C Programming forums, part of the General Programming Boards category; Hi, I have the following program: Code: for (;;) { char linea[16384] = ""; char byte; int i = 0; ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    stdin size problem

    Hi,

    I have the following program:

    Code:
     for (;;) {
        char linea[16384] = "";
        char byte;
    
        int i = 0;
        while ((fread(&byte,1,1,stdin))) {
          linea[i]=byte;
          i++;
          linea[i]='\0';
          if (byte=='\n') break;
        }
    
        ......
    
    }

    My partner send me lines from time to time. When the line sended is less than 256 bytes I received OK, but when the length is > 256 I only received first 256 and loss the rest.

    How can I receive bytes > 256????? is there any buffer limit????

    P.S.: I'm using MINGW under Windows
    Last edited by Kempelen; 04-12-2009 at 01:25 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    stdin does have a buffer size, but I doubt it is as small as 256 bytes. Is fread() returning zero? Are you sure that the loop is not breaking out due to receiving a newline character?

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    There's no particular reason that you shouldn't be able to read more than 256 bytes from standard input. I suppose your operating system may have some limits somewhere that are causing you issues (256 seems less than random, for example). But perhaps there's something going on elsewhere in the code causing problems; or perhaps your sender is, somehow not sending what you think it is.

    It might help to provide the full program (assuming it's relatively short) and a description of how you're sending data to the standard input. At the very least, you'll want to check ferror() after fread() returns a short count to see if there was an error reading, instead of hitting EOF. That might help narrow down where the problem lies.

    Incidentally, why are you using fread() instead of getchar()? Not that fread() can't work this way, but it's kind of like using a sledgehammer to hang a pictureframe.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    145
    Quote Originally Posted by bithub View Post
    stdin does have a buffer size, but I doubt it is as small as 256 bytes. Is fread() returning zero? Are you sure that the loop is not breaking out due to receiving a newline character?
    I have discarted it is a partner problem. The program which are sending the data is a commercial one and works OK with different clients (my program is a client). Also I have a free app which catch traffic in-between and I can see in the logs that all lines sended are OK, even those > 256. I have also tried with another app and it sends data OK.

    Also I have tried fgets or getchar, and I continue with the problem. I think the 256 is because what I'm programming is a console/dos app and not a pure windows app.

    Could be caused because the servers are windows app and my client a console one?

    Any claud/tip how can I read those bytes?

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    when fread returns 0 you can check the feof and ferror flags, and if the ferror returns non-zero value - examine the errno as well

    Why are you reading 1 byte at a time? try to read the whole buffer and examine the return value of fread to know how many byte were actually read...

    if you want to stop on \n - fgets should be used...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    145
    Quote Originally Posted by vart View Post
    when fread returns 0 you can check the feof and ferror flags, and if the ferror returns non-zero value - examine the errno as well

    Why are you reading 1 byte at a time? try to read the whole buffer and examine the return value of fread to know how many byte were actually read...

    if you want to stop on \n - fgets should be used...
    OK, it was a very obscure bug I had in the function just after the loop, just the one that print the result!.

    thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdin buffer size
    By Kempelen in forum C Programming
    Replies: 6
    Last Post: 03-16-2009, 03:06 PM
  2. Another memory problem
    By alwut in forum C Programming
    Replies: 2
    Last Post: 03-01-2009, 12:43 AM
  3. Having a problem with templates and classes
    By bowluswj in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2007, 12:55 PM
  4. size of Object c++ compile problem
    By micha_mondeli in forum C++ Programming
    Replies: 5
    Last Post: 04-06-2005, 01:20 PM
  5. Problem in getting size of a window
    By axr0284 in forum Windows Programming
    Replies: 3
    Last Post: 02-22-2005, 05:15 PM

Tags for this Thread


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