Teen Programmers Unite  


Return to forum top

Data Manipulation in C++

Posted by Kruptos [send private reply] at May 15, 2002, 09:15:13 PM

Language: C++
Topic: Data/File Manipulation

Okay here's my dilemma. I need a way of organising a list in an external file (rich text) then calling that information up, running through it, and seperating the different parts of the list into seperate variables. Sound fun? You betcha. I use Borland C++ Builder 5.0 which is an Ansi C++ compiler btw. So here's what I was thinking so far: I get the buffer from the file and either put it in a char array or an AnsiString. Then I loop through each character of the string until I come up to where my list seperator is (lets say for now its a semicolon) once I get to that semicolon (which i'll pick out with an if statement within the loop) i'll run a function which assigns all of the text data before the semicolon to an AnsiString (or char array) called 'Data1'... Sounds like I know what I'm doing, right? Not exactly. I know how to loop through the stuff and get up to finding the seperator within the buffer variable, but once I get there I have no idea how to seperate the chunks of data between the seperators. So assume this is what I have so far:


char buffer[] = "This is the buffer text, blah, blah.";

for (int c = 1; c <= strlen(buffer); c++) {
if (strncmp(":", buffer[c])) {
// This is where I need code to seperate the data


So there you have it, if you had no idea what I was just talking about please tell me, I won't blame ya :)

Posted by Kruptos [send private reply] at May 15, 2002, 09:19:30 PM

Ah yes, this might be a little more helpful if you cant figure out what I'm trying to do. Here's what I want...


char buffer[] = "Apples:Oranges:Teacups:Muffins";


char data1[] = "Apples";
char data2[] = "Oranges";
char data3[] = "Teacups";
char data4[] = "Muffins";

So essentially I need to get from BEFORE to AFTER with some code.

Posted by Psion [send private reply] at May 15, 2002, 10:27:16 PM

You might want to take a look at the strtok standard library function if you are not writing multi-threaded code. Regardless, however, you should be able to do this yourself. Is it enough help to suggest that you want to keep an additional pointer to either the beginning of the string or the character right after the last separator found?

Posted by Kruptos [send private reply] at May 15, 2002, 10:36:42 PM

Ohhhhhh, I never thought of that :)
The magic of pointers!! Thanks :)..I'm relatively new at this

Posted by AnyoneEB [send private reply] at May 16, 2002, 03:40:19 PM

Ummmm... now I see why everyone likes java's StringTokenizer soo much, this is just an annoying task. The way I'd do it (I'm a bit rusty in C) would to make a for loop that would add characters to char data1[] until it came to a ':', then it would switch data varibles, probably easier to do my making an array of char arrays.

Hope this isn't too much writing the code for him.

Posted by unknown_lamer [send private reply] at May 16, 2002, 05:14:19 PM

Perl's string splitting function is nice too. So is Guile's string-split. You can easily implement string-split in C by wrapping strtok and returning a list of char*'s. The only problem comes when you delete the string...so you should strcpy the string too.

Posted by Kruptos [send private reply] at May 16, 2002, 08:49:12 PM

Yes, AnyoneEB, I could indeed do that now that you point it out. What I was looking for was exactly what the strtok function does. Its absoultely perfect for my needs, but thanks anyway. And once again, thanks Psion.

Posted by AnyoneEB [send private reply] at May 16, 2002, 09:05:38 PM

oh, strtok=StringTokenizer? so C++ does have that class?

Posted by gian [send private reply] at May 16, 2002, 11:54:23 PM

strtok is part of the C strings library.

You must be logged in to post messages and see which you have already read.

Log on
Save for later automatic logon

Register as a new user
Copyright TPU 2002. See the Credits and About TPU for more information.