help reading from .txt file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 10
Reputation: mcook228 is an unknown quantity at this point 
Solved Threads: 0
mcook228 mcook228 is offline Offline
Newbie Poster

help reading from .txt file

 
0
  #1
Mar 26th, 2006
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.
//
Nodes:
Atlanta
Boston
Camloops
Denver

Edges:
Atlanta Denver 1
Denver Atlanta 1
Atlanta Boston 1
Camloops Boston 1
Boston Atlanta 1
Boston Camloops 1

Start:
Boston

Please help??????

Thank you!
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 50
Reputation: AstroNox is an unknown quantity at this point 
Solved Threads: 2
AstroNox AstroNox is offline Offline
Junior Poster in Training

Re: help reading from .txt file

 
0
  #2
Mar 27th, 2006
I assume this is a directed graph because the file states two opposite edges for any two nodes.

You do something along this line:
  1. ifstream fin("myfile.txt");
  2. if (!fin)
  3. cout << "Error" << endl;
  4. char buf[81];
  5. while (fin)
  6. {
  7. if (!fin.get(buf, 81))
  8. break;
  9. if (strlen(buf) == 0) // Make sure we have something before continuing
  10. continue;
  11. if (buf[0] == '/' && buf[1] == '/') // Check for double slashes
  12. continue;
  13.  
  14. // do rest of logic here, and use strtok or whatever string functions if necessary
  15. }
It's a better choice and less of a problem to read line-by-line, then tokenize the line for further use.
Best Regards, God Bless,
AstroNox
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