473,322 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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:Maintenance
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.
Feb 26 '10 #1

✓ answered by RedSon

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?

4 2955
RedSon
5,000 Expert 4TB
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?
Feb 26 '10 #2
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_name[i], clients[i]);



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

}
Feb 26 '10 #3
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_type[i],tmp);
cout <<client_type<<endl;

Thanks for the help!
Feb 26 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Feb 26 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: flam | last post by:
Hello, I am having a hard time spliting a string into an array for use in a search. Here is the situation. The user will input a search string. Normally I can just split the string by "split...
28
by: Fabian | last post by:
I use the following to parse the url var srch = window.location.search.substring(1); // then split srch at the ampersand: var parts = srch.split("&"); // write the parameters into the variables...
12
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ........
1
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement...
1
by: David | last post by:
I have rows of 8 numerical values in a text file that I have to parse. Each value must occupy 10 spaces and can be either a decimal or an integer. // these are valid - each fit in a 10 character...
6
by: kevin | last post by:
I need to parse an third party supplied delimited or fixed width text file into a datatable. The delimiter may vary. I am currently using a SteamReader to read each line and, for delimited...
12
by: Klaus Alexander Seistrup | last post by:
Hi group, I am new to xgawk (and seemingly to xml also), and I've been struggling all afternoon to have xgawk¹ parsing an XHTML file containing a hCard², without luck. I wonder if you guys...
4
by: danu | last post by:
Hello all, basically here's what I'm trying to do: I want to put 5, 10, -20 , 5 , 20 in to array, and -1, 5, 10, -2 into array . All the input are coming through stdin. How can I parse these...
2
by: RG | last post by:
I am having trouble parsing the data I need from a Serial Port Buffer. I am sending info to a microcontroller that is being echoed back that I need to remove before I start the actual important...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.