I'm trying to read data from file into a graph and am wondering if anyone could suggest the best way to go about this... The data will be presented with commented lines that need to be skipped - these lines will begin with "/". Once I've skipped the comment lines, I need to read each line, one word at a time.

My confusion is that if I read each line, in order to skip the commented lines, then I end up with an entire line in the buffer and would need to extract the words, one at a time. I have no idea how to do that. OR if I read each word, one at a time, then I skip the first word of the comment line, but read the rest of the words as if they're to be added to the graph.

Sample text file:

// An Example graph file
// Nodes are one-word strings.
// Edges are formatted as whitespace-delimited triples:
// sourceVertex destinationVertex edgeWeight
// By default, the graph is directed. An undirected
// graph is represented by including two equally weighted
// edges between every pair of connected vertices.
// The graph for Program 2 will be undirected and every
// pair of edges will be explicitly included in the file.
// The starting vertex is a one-word string corresponding
// to one of the nodes appearing in the Nodes: section.
// The keywords "Nodes:," "Edges:," and "Start:" are
// guaranteed to be unique in the file and will appear
// exactly as written here.

Atlanta Denver 1
Denver Atlanta 1
Atlanta Boston 1
Camloops Boston 1
Boston Atlanta 1
Boston Camloops 1


Please help??????

Thank you!

I assume this is a directed graph because the file states two opposite edges for any two nodes.

You do something along this line:

ifstream fin("myfile.txt");
if (!fin)
	cout << "Error" << endl;
char buf[81];
while (fin)
	if (!fin.get(buf, 81))
	if (strlen(buf) == 0) // Make sure we have something before continuing
	if (buf[0] == '/' && buf[1] == '/') // Check for double slashes

	// do rest of logic here, and use strtok or whatever string functions if necessary

It's a better choice and less of a problem to read line-by-line, then tokenize the line for further use.

Be a part of the DaniWeb community

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