Computing.Net > Forums > Programming > C++: reading space delimited data

C++: reading space delimited data

Reply to Message Icon

Original Message
Name: LooFi
Date: November 14, 2003 at 05:11:54 Pacific
Subject: C++: reading space delimited data
OS: RH9
CPU/Ram: P4 2.4ghz / 512
Comment:

I need some suggestions and tips on how to handle a dataset.

I have 4 collums of data in a space delimited txt file. The first collum is an indices for a sample plot, next two are the x and y coordinates of the plot and last is the data measured on that spot.

As you can guess, the relationship between the four collums can not be lost, it's vital information.

What's the best way of reading such information into a c++ program? A friend of mine suggested using a database an querry the database.

Any suggestions, tips... idea's would be welcome

Thank you for your time


Report Offensive Message For Removal


Response Number 1
Name: Tugg
Date: November 15, 2003 at 17:11:05 Pacific
Reply: (edit)

Im not quite sure exactly what you mean, but im guessing you could use an array to save all the information in a structure similar to what you have in your txt file.

Using fstream you can read in the data from the file and store it in a two-dimensional array( ie: myArray[25][25]. Run a loop during the read-in to get eof and than nest another loop in that that will increment the second dimension to move to the next line in the array.
(unless its all one line in your txt...space-delimited?? not quite sure what it means as im pretty noob myself)

Hope im not completly off-course.

~Tugg~


Report Offensive Follow Up For Removal

Response Number 2
Name: LooFi
Date: November 16, 2003 at 03:23:56 Pacific
Reply: (edit)

To be clear, data is ordered like this:

plot| x | y | data
--------------
A1 | 23| 34| foo1
A2 | 34| 54| foo2

Anyway, I have 500 lines of data an array would look like array[500][4].

My problem is rather if such arrays are still manageable.

I also found a better solution to mix types namely structures, then I can mix several different types. This would give something like:

struct dataset {
char plot; //A1
float x; //x
float y; //y
float data; //measured data
};

Any idea's are still welcome, I'm just brainstorming.



Report Offensive Follow Up For Removal

Response Number 3
Name: Tugg
Date: November 16, 2003 at 21:33:52 Pacific
Reply: (edit)

ok...for brainstorming how bout using linked-lists. These have two benefits: for one, you dont have to declare how long they will be, as you will just link another set of data on at the end, and secondly, linked lists work alot faster than arrays. Only problem with them is they store data one after the other...in a row...so you'd have to use some loops to format the data upon retrieval. The initialization would look something like this (mind you, you better create a header with some #define statements and especially a structure with the list', operands):

//Initialization
int my_list(MYLIST **pp_list)
{
*pp_list = (MYLIST *)malloc(sizeof (MYLIST));
if (*pplist_NULL)
return error_flag;
(*pp_list)->data = NULL; /*should be defined in your own structure*/
(*pp_list)->next = NULL;
return ok_flag;
}

//Addind data to list
int my_list_insert(int **pp_list, void *p_data)
{
//1: If list empty
if ((*pp_list)->data == NULL)
{
(*pp_list)->data = p_data;
return ok_flag;
}
//2: Something in list, add on in the end
pNewElement = (MYLIST *)malloc(sizeof (MYLIST));
//Error during initialization
if(pNewElement == NULL)
return error_flag;
else {
//insert data and set pointer
pNewElement->data = p_data;
pNewElement->next = *pp_list;
*pp_list = pNewElement;
return ok_flag;
}
}

This should allow you to save as much information as your system can handle while still maintaining the fastest possible speed. The fact i didnt include the necessary structures you need to create might be a little confusing so if you want clerification on those let me know.

cheers,
~Tugg~


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C++: reading space delimited data

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge