Sinking under multidimensional arrays

This is a discussion on Sinking under multidimensional arrays within the C Programming forums, part of the General Programming Boards category; Hi to all, Need some help with a function I'm trying to write, if I may. The object of the ...

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

    Sinking under multidimensional arrays

    Hi to all,

    Need some help with a function I'm trying to write, if I may. The object of the function is to read through a comma delimited file placing the data between the commas into individual elements of the array, to be worked on by the calling program. What I have so far is -

    Code:
    int get_info(FILE *fp, fpos_t seek_ptr, char (*ptr)[MAX_REC])
    {
      char buffer[28];
      int x, n, t;
      char *p, ch;
    
      p = (char *)ptr;
      fsetpos(fp, &seek_ptr);
    
      while(ch != ';')  // End of field pointer
      {
        ch = fgetc(fp);
    
        if(ch == '{' || ch == ' ') { t = 0; }  // Remove header info
    
        if(ch == ',')
        {
          ch = '\0'
          buffer[t++] = ch;
    
          if (( p = (char *)malloc(strlen(buffer)+1)) == NULL)
            return -1;
          strcpy(p[n++], buffer);
          ch = 0; t = 0;
        }
        if( ch == '}')  // Reached end of a line
       {
          ch = '\0';
          buffer[t++] = ch;
    
          if (( p = (char *)malloc(strlen(buffer)+1)) == NULL)
            return -1;
          strcpy(p++, buffer);
          ch = 0; t = 0;
        }
        buffer[t++] = ch;
      }
      return o;
    }
    I've obviously done something seriously wrong because all this does is hang the computer, I've been staring at this code for too long and just can't see it. Any help greatly appreciated!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can do anything you want with p -- what does that have to do with your char array ptr? (Hint: nothing.) You claim that ptr is a pointer to a char[], so the space should already be there for a string, so I don't see why you malloc anyway. (Unless you want ptr to be an array of char *, which would look like char *ptr[SIZE]. Then, you would need to do ptr[n] = malloc, and so on.)

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    Sorry guys, have tried a few different things and I just can't get it, all I get when trying to read is garbage, obviously I'm pointing somewhere incorrectly or its not returning the data properly. Any chance anyone can give me some coded examples of where I'm going wrong? Thanks to all who reply.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ubj qb jr xabj jung lbhe pbqr ybbxf yvxr abj? Naq qb lbh haqrefgnaq jung jnf jebat jvgu jung lbh svefg cbfgrq?

    (Hey, you wanted a coded reply.)

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    I'm having a hard time understand what it is you're supposed to be doing exactly.

    What is the input specification? Do you need to store your output in a two dimensional array of strings? If so, your "ptr" parameter is declared incorrectly.

    Since a string in C is an array of characters, you then need:

    A pointer -> to an array -> of strings

    char * -> [arraySize] -> [stringSize] myArray;

    Which is basically a char***.

    Anyway, from the little bit of info you gave about the problem, I managed to come up with a function that reads in a single record which seems similar to what you were trying to accomplish. But keep in mind, this isn't tested and it makes some assumptions that are almost certainly invalid.

    Obviously, I wrote ignore_whitespace and ignore_non_nl_whitespace myself, and they have easy enough implementations.

    PHP Code:
    [code]
    /*
       Reads a single record from the input file into the array of strings
       "ptr".  It is assumed that memory has already been allocated for
       the array, but NOT for each individual string in the array.
       
       A single record is assumed to be in the format:
       { field1,field2,field3,field4,field5 }
       and is expected to be all on one line.  Reading stops
       when the '}' character is read, or if an error occurs.
       
       Each field is stripped of leading and trailing whitespace, and empty
       fields are read as empty strings, so:
         {} gives the array {""}
         {,} gives the array {"", ""}
         {blah,    ,blah,} gives the array {"blah", "", "blah", ""}
       
       Returns the number of fields contained in the record, or a negative
       number if an error occurred.
    */
    int read_single_record(FILE *fpchar **output)
    {
       
    char buffer[128];     // Temporary storage for a single field
       
    char *buf buffer;   // A pointer into buffer (to make manipulation easier)
       
    char **out output;
       
    char ch;
       
       
    ignore_whitespace(fp); // ignore whitespace before the record
       
    ch fgetc(fp);
       if (
    ch != '{') {
          
    // The input is not formatted properly (no leading '{')
          
    if (ch != EOFungetc(chfp);
          return -
    1;
       }
       
    ignore_non_nl_whitespace(fp);
       
       
    // read in comma separated fields until a '}' is reached
       
    while(1) {
          
    ch fgetc(fp);
          
          if (
    ch == ',' || ch == '}') {
             
    // Trim trailing whitespace from the field
             
    while (buf buffer && isspace(buf[-1])) buf--;
             *
    buf++ = '\0';
             
             
    // Allocate output space for the field and copy the buffer there
             
    *out malloc(buf buffer);
             if (*
    out == NULL) return -2;
             
    strcpy(*out++, buffer);
             
    buf buffer;
             
             
    // If there is another field, ignore the whitespace in front of it
             
    if (ch == ','ignore_non_nl_whitespace(fp);
             
             
    // If this was the end of the record, break out of the loop
             
    if (ch == '}') break;
          }
          else if (
    ch == '\n') {
             
    ungetc(chfp);
             return -
    3;
          }
          else if (
    ch == EOF)
             return -
    4;
          else
             *
    buf++ = ch;
       }
       
       return 
    out output;
    }
    [/
    code
    Btw, I wanted to use the nifty PHP hilighting, but it wouldn't let me post unless I put code tags around it as well.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You can highlight code without PHP code tags if you use an external highlighter . . . like codeform. (I wrote it. For less biased results, search the forum.)

    It sounds like a bug in the code-tag-detection script. I guess no one uses PHP tags, so it hasn't really come up.

    The object of the function is to read through a comma delimited file placing the data between the commas into individual elements of the array, to be worked on by the calling program.
    Hmm . . . why not read the data in, line by line, and separate into tokens with strtok() or your own equivalent?

    I guess character-by-character works too . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Adding multidimensional arrays
    By newbie2C++ in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2001, 03:05 PM

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