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)

class graph{
 private:
  struct elements{
    char Category[1];
    string NameofComponent;
    int NbOfElements;
    string NameofElements;
  };

 public: // method go here

};

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!)

int main()
{
      string line;
      ifstream parent;
      fanOutFile.open("parents.txt", ios::in);
      ofstream child;
      fanInFile.open("child.txt");  //opening the files

      char *token;
      char temp[100];

      string str1;
      int i=0;
      char delim1='\n';

      if ((!parent)&& (!child))  //if both files does not exist
      {
      cerr << "file not found" << endl;
      system("PAUSE");
      }

      if(fanOutFile.is_open())
                              {
                              cout << "size of file: " << sizeof(parent) << endl; //size of the parent.txt file, for test purpose

                              while(!parent.eof() )
                               {
                               getline(parent, str1, delim1);// see 1.
                               strcpy(temp, str1.c_str()); // see 2.
                               for(int i=0; i<(str1.length()+1); i++) // see 3.
                                {
                                 cout<<temp[i];
                                }
                               token=strtok((temp, ",")); // see 4.
                               while(token!=NULL)
                                {
                                 cout<<token<<endl;
                                }
                               child << str1 << endl; // another file writing test

                              }
                            }
      
      else
      {
      cout << "unable to open file" << endl;
      }
     system("PAUSE");
     return 0;
}

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

see: http://www.daniweb.com/forums/thread85114.html
note: cout << "size of file: " << sizeof(parent) << endl; //size of the parent.txt file, for test purpose will 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) )

Member Avatar for iamthwee

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.