Am I missing something here? (Problem with read())

This is a discussion on Am I missing something here? (Problem with read()) within the C Programming forums, part of the General Programming Boards category; This function should print the buffer I suppose, but it doesnt seem to do that. Anyone got any pointers asto ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    6

    Am I missing something here? (Problem with read())

    This function should print the buffer I suppose, but it doesnt seem to do that. Anyone got any pointers asto whats going on?

    Code:
    int laes(int socket,char *buffer,int langd) {
      int counter = 0;
      int laes = 1;
      int storlek = 0;
      char c;
     
      while(counter < langd) {
        if( (storlek = read(socket,&c,1)) == 1) {
          if(c == '\n') {
    	*buffer = '\0';
    	printf("Avklarat %s,",buffer);
    
    	return counter;
          } else {
    	*buffer = c;
          }
        } else {
          printf("Avklarat annars"); 
         return counter;
          
        }
    
        printf("Ett varv\n");
        buffer++;
        counter++;
      }
      
      return counter;
    }
    Last edited by Anth; 07-15-2004 at 03:45 PM.

  2. #2
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >but it doesnt seem to do that
    What does it seem to do?

    >printf("Avklarat %s,",buffer);
    I don't see a *buffer = '\0' anywhere, so you're probably overrunning your buffer.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    Oups wrong version.
    The *buffer = '\0' is in there.
    Let me clarify. It doesnt print anything in the printf. It prints "Avklarat ,". And if the buffer that gets returned from this function is past as an argument to another function the sizeof says its only 4 bytes long.

  4. #4
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >It doesnt print anything in the printf.
    Well, that's probably because you've been incrementing buffer and now it points to an empty string because the first character is '\0'. Maybe something more like this:
    Code:
    int laes(int socket,char *buffer,int langd) {
      int counter = 0;
      int laes = 1;
      int storlek = 0;
      char c;
      char *pbuffer = buffer;
     
      while(counter < langd) {
        if( (storlek = read(socket,&c,1)) == 1) {
          if(c == '\n') {
    	*pbuffer = '\0';
    	printf("Avklarat %s,",buffer);
    
    	return counter;
          } else {
    	*pbuffer = c;
          }
        } else {
          printf("Avklarat annars"); 
         return counter;
          
        }
    
        printf("Ett varv\n");
        pbuffer++;
        counter++;
      }
      
      return counter;
    }
    As for sizeof giving you 4, that's perfectly normal. You aren't working with an array anymore (assuming buffer was declared as such originally). In value context, an array is converted to a pointer to the first element. And on your system, pointers are 4 bytes.

    By the way, this:
    Code:
    int laes = 1;
    probably won't work very well. laes is the name of the function.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Some good read functions are detailed in the last post in this thread:
    recv/send

    >>sizeof says its only 4 bytes long
    Pointers are only 4 bytes long, are you sure you're not getting confused and using sizeof when you should really be using strlen() or some other length calc'ing function?

    [edit]
    Beat!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    Thanks. This was driving me insane.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read in problem
    By florian100 in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2009, 06:33 AM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  4. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 06:02 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM

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