Page 1 of 1

C++ Help

Posted: Tue Jan 22, 2008 9:44 pm
by MakerOfGames
I am working on a coding project for my c++ class, but I just can't get this right. I need to read a text file which data is seperated by commas until the end of the line there is a new line and a repeat of data.
So, how can I do a double disect of the data stream?

Code: Select all

in.getline(temp, 200, '\n');
That gets me the whole line to a temp string variable, but how do I then look at the string and pick out the data seperated by commas?
Ex data set:
23,53,52,543,63,12,2,523,98
32,53,43,43,65,3,646,2,34
-all rows have the same number of sets of data, its just the length of the individual pieces of data in the string that vary.

Any help would be greatly appreciated!

Re: C++ Help

Posted: Tue Jan 22, 2008 10:26 pm
by Allanon
Try using the strtok function.

Re: C++ Help

Posted: Tue Jan 22, 2008 10:44 pm
by MakerOfGames
Thanks. I think that will help me out.

Re: C++ Help

Posted: Tue Jan 22, 2008 10:54 pm
by paradoxnj
You can also use the Standard Template Library's stringtokenizer lib or Boost::Spirit.

Re: C++ Help

Posted: Wed Jan 23, 2008 4:01 am
by MakerOfGames
paradoxnj wrote:You can also use the Standard Template Library's stringtokenizer lib or Boost::Spirit.
I will look that up. I have only had 2 C++ classes to date. Much of how my course is oriented is that more Java will carry over, so very basic input output was covered so far in C++, which its actually A LOT different from Java. Hopefully my next class will clear things up for me, along with looking that up and more independent research :roll:. Thanks for the help.

Re: C++ Help

Posted: Thu Jan 24, 2008 1:05 am
by SithMaster
I should know this. I just took a semester of C++ and i really cant remember how to do this one. When is it due? I can try and go over my notes and figure it out based on labs ive done. Or i can just send you my code files so you can try and decipher my code.

Re: C++ Help

Posted: Thu Jan 24, 2008 1:34 am
by MakerOfGames
I have until Monday to get this done. I am going to give it another shot tomorrow, because tonight I am out of time(I have other work thats due by Friday that needs done first). I might be able to get this on my own with the help already posted. Thank you very much though. I will let you know if I still can't get this down and then we can go from there. Thanks again!

Re: C++ Help

Posted: Thu Jan 24, 2008 2:43 am
by Allanon
A little help

Code: Select all

char *Result = NULL;

in.getline(temp, 200, '\n');

Result = strtok( temp, ",");  // Get the first number
while (Result)  // loop thru all numbers in string
{
    int num = atoi(Result); // convert string to an integer
    printf("%i",num);        // print the integer
    Result = strtok(NULL,",");  // Get the next number 
}


Re: C++ Help

Posted: Thu Jan 24, 2008 3:20 am
by MakerOfGames
Thanks! That will help a lot!