parsing a 2d character array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wrangler2004
    New Member
    • Apr 2007
    • 12

    parsing a 2d character array

    I have a project I'm working on to read a simple text file and sort it's contents based on the data.

    The data file is in the following format:

    Bob Smith: Accounting
    John Doe : IT
    Bill Morris:Maintena nce
    etc...

    My goal is to be able to sort each string by name OR by department.

    So far, I have been able to read the file and sort by name (since name is the first item on each line of the file), but I can't seem be able to figure out how to reverse the contents of each line such as turning:
    Bob Smith : Accounting
    into:
    Accounting: Bob Smith

    I'm currently using a single 2-d array to store the contents of the file.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Do you tokenize the input? If so just reverse the first token with the second token and sort appropriately.

    Also I wouldn't recommend using a 2d array. Are you not allowed to use any container types, maybe a dictionary or some kind of key/value data structure?

    Comment

    • wrangler2004
      New Member
      • Apr 2007
      • 12

      #3
      Right now, I've decided to read the file into a 2-D array, then make 2 copies of that array - one copy will contain the client name and the other will contain the client type.

      Using tokens, I can get the first part of each line (left of the colon), however I can't seem to figure out how to extract only the right side:

      //read data from file in a 2-d array
      for (i=0;i <NUM_ROWS;i++ )
      {
      infile.getline( clients[i],sizeof(client_ name)); //read clients into an array
      strcpy(client_n ame[i], clients[i]);



      //read client name into an array
      strtok(client_n ame[i],":");
      cout << client_name[i]<<endl;

      }

      Comment

      • wrangler2004
        New Member
        • Apr 2007
        • 12

        #4
        well, maybe my approach isn't the best, but I appreciate the advice on tokens. I finally added the following lines inside my loop to pull the client type:

        //read client type into an arry
        tmp = strtok(NULL,":" );
        strcpy(client_t ype[i],tmp);
        cout <<client_type<< endl;

        Thanks for the help!

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You might consider using a sort handle.

          A sort handle is a struct with the sort fields plus a pointer to the data.

          IN this case you wan to sort by Department then by name, so your sort handle looks like:

          Accounting Bob Smith
          Accounting Nancy Drew

          But if you want to sort by name and then deparment your sort handle looks like:

          Bob Smith Accounting
          Nancy Drew Accounting.

          Then make a pass of your data and create an array of sort handles. Then just sort using the handle as the sort sequence.

          Finally, make a pass of the handles and use the pointer in the handle to fetch your data.

          Voila!


          Of course, if you need to split the name so you can sort first, then last your sort handle becomes:

          Smith Bob Accounting
          Drew Nancy Accounting

          Now you have last name a primary, first name within last, and department within first name.

          Comment

          Working...