Problem with strtok() for data structure creation

Reply

Join Date: Aug 2007
Posts: 1
Reputation: dannyadp is an unknown quantity at this point 
Solved Threads: 0
dannyadp dannyadp is offline Offline
Newbie Poster

Problem with strtok() for data structure creation

 
0
  #1
Aug 16th, 2007
hey all,
I'm new to this forum, and really start to like it .

Here we go. I have some trouble helping a "tute" of mine (I normally give physics tuitions) with one of his assginments on C++.
As I did C++ years ago, I was feeling confident with it. But soon started getting mad lol.
I used the search feature of the website, but could not find anything relevant.

The Big Picture:
We have a text file with lots of lines, and serveral different types of values on each line. The values on each line are separated by a coma.

example: lets say

L,Water,0
G,Oxygen,1,Water
G,Hydrogen,1,Water
....
The format is the same for all the line, so:
Category,NameofComponent,NbOfElements,NameofElements

I rapidly noticed it would give us a graph, as the main question of the problem is: "given the list of parent-to-child (parents.txt), make a list of child-to-parents(childs.txt)"
so we will go from the above input to the following:

L,Water,2,Oxygen,Water
G,Oxygen,0
G,Hydrogen,0

where Water is the child having 2 parents Oxygen and Hydrogen.
Oxygen and hydrogen themselves have no parents (so NbOfElements=0).
It is a kind of inversed adjancency list if I'm not wrong. So I decided to make a data structure allowing us to build the graph with the adjacencies, and then use and algorithm(not determined yet) which will take the NameOfComponent, look for occurencies in the other lists,etc... and so on, which will rebuild the parents.txt into child.txt. Hope I'm not confusing you

Here is the structure I decided to use (still in process)
  1. class graph{
  2. private:
  3. struct elements{
  4. char Category[1];
  5. string NameofComponent;
  6. int NbOfElements;
  7. string NameofElements;
  8. };
  9.  
  10. public: // method go here
  11.  
  12. };

The Problem:
the thing is that, as each lines are comma separated, I'm trying to write the line in a string, then using strtok(), parse it based on ",".
Here is an example of what I'm trying to achieve (for the moment, I'm doing all the tests under the main(). Will put it in a method once fixed!)

  1.  
  2. int main()
  3. {
  4. string line;
  5. ifstream parent;
  6. fanOutFile.open("parents.txt", ios::in);
  7. ofstream child;
  8. fanInFile.open("child.txt"); //opening the files
  9.  
  10. char *token;
  11. char temp[100];
  12.  
  13. string str1;
  14. int i=0;
  15. char delim1='\n';
  16.  
  17. if ((!parent)&& (!child)) //if both files does not exist
  18. {
  19. cerr << "file not found" << endl;
  20. system("PAUSE");
  21. }
  22.  
  23. if(fanOutFile.is_open())
  24. {
  25. cout << "size of file: " << sizeof(parent) << endl; //size of the parent.txt file, for test purpose
  26.  
  27. while(!parent.eof() )
  28. {
  29. getline(parent, str1, delim1);// see 1.
  30. strcpy(temp, str1.c_str()); // see 2.
  31. for(int i=0; i<(str1.length()+1); i++) // see 3.
  32. {
  33. cout<<temp[i];
  34. }
  35. token=strtok((temp, ",")); // see 4.
  36. while(token!=NULL)
  37. {
  38. cout<<token<<endl;
  39. }
  40. child << str1 << endl; // another file writing test
  41.  
  42. }
  43. }
  44.  
  45. else
  46. {
  47. cout << "unable to open file" << endl;
  48. }
  49. system("PAUSE");
  50. return 0;
  51. }

Explanations:
1. using a stream getline, I'm going till the end of the line("\n"), in order to take the data only line by line (thus the while loop with eof()).
2. have to do this (copying the content of string, so first line, into char temp[100]) cause I'm not able to cast the string into a char when using the strtok() at 4.
3. Have put some "cout" in the loop to clearly see where is he problem coming from. In this case, I just verify that strcpy copied everything in temp
4. Problem, it tells me that strtok has to few arguments to function... Refering to the doc, I followed the template given, i.e.
char* strtok (char* szTokenize, const char* szDelimiters);
but I'm still getting this error.

So, to sum up, I'm not able to parse the line contained in the string str1, therefore won't be able to create the struct elements (hte tokenized items from each lines will be saved in each of the variables contained in elements). So not able to build the graph .

If you guys can give me some hints, or corretct me if I'm wrong anywhere here in the approach or the coding, I would highly appreciate. I definitely need to fix something, but don't know what...

Thanks for reading me, and sorry for being a bit long .

Cheers,

adp
Last edited by dannyadp; Aug 16th, 2007 at 11:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Problem with strtok() for data structure creation

 
0
  #2
Aug 17th, 2007
see: http://www.daniweb.com/forums/thread85114.html
note: cout << "size of file: " << sizeof(parent) << endl; //size of the parent.txt file, for test purposewill not give you the size of the file on disk; sizeof(parent) gives you the amount of memory that the variable parent takes ( as a multiple of sizeof(char) )
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem with strtok() for data structure creation

 
0
  #3
Aug 17th, 2007
So many things to point out.

Like for starters, why are you mixing c-style arrays with strings?
Using eof for file i/o? Not using strtok properly. Not using stringstreams instead of strtok? There's probably more.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC