Thread: read() -> array

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    22

    read() -> array

    This gets char array sent via pipe to a second process and prints it:
    Code:
    int x;
    int pipe;
    char buf[PIPE_BUF];
    while ((x=read(pipe, buf, PIPE_BUF))>0)
        write(fileno(stdout), buf, x);
    I want the data to go into 2D array (5x5), so it would look like:
    Code:
    char arr[5][5] = {  'q', 'w', 'e', 'r', 't',
                        'y', 'u', ..............
                         ....
                         ....
                         ...                 'a' };
    How can I do this

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    have you tried using two for loops?
    Code:
    int x, y;
    
    for ( x = 0; x < 5; x++)
    {
      for ( y = 0; y < 5; y++)
      {
         // read character here and assign it to the array at position [x][y]
      }
    }

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    22
    Code:
    int x, y,z;
    char arr[5][5];
    
    for ( x = 0; x < 5; x++){
      for ( y = 0; y < 5; y++){
         z=read(pipe, buf, PIPE_BUF);
         arr[x][y] = z;
         printf("%c%c", arr[x][y]);
      }
       printf("\n");
    }
    This is not working since the elements are not being printed. What is wong ?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    read function returns number of chars read - so that what you are storing in the array instead of actual buffer read

    printf("&#37;c%c", arr[x][y]); - you have 2 format specifiers and only 1 additional argument to the printf - you are asking for crash using it in this way
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int x, y,z;
    char arr[5][5];
    
    for ( x = 0; x < 5; x++){
      for ( y = 0; y < 5; y++){
         z=read(pipe, buf, PIPE_BUF);  
         // You read into buf, and z contains the number of bytes read. 
         // as a side comment, z is really not a good name here - it implies that it's
         // got something to do with x and y (in a coordinate type way), which is absolutely
         // untrue - perhaps call it "readCount" or "bytesRead" (camel case optional).
         arr[x][y] = z;
         printf("%c%c", arr[x][y]);
         // As pointed out, two format specifications with only one argument will not print the right thing. 
         
      }
       printf("\n");
    }
    If you read a block of PIPE_BUF size [presuming this is at least 5 * 5 bytes], then you could just loop around and assign arr[x][y] with the corresponding position in buf. If you want to read and immediately assign, you should read one byte at a time [which is definitely not optimal, but may make the logic clearer].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read coefficients of linear equations from file into 2d array
    By omaralqady in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2009, 07:39 AM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Array -> 2D array
    By Tupcia in forum C Programming
    Replies: 4
    Last Post: 05-16-2008, 02:29 AM
  4. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  5. Function to read data from file to array
    By anatazi in forum C Programming
    Replies: 3
    Last Post: 07-01-2002, 11:08 PM