| | |
help reading from .txt file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
I assume this is a directed graph because the file states two opposite edges for any two nodes.
You do something along this line:
It's a better choice and less of a problem to read line-by-line, then tokenize the line for further use.
You do something along this line:
C++ Syntax (Toggle Plain Text)
ifstream fin("myfile.txt"); if (!fin) cout << "Error" << endl; char buf[81]; while (fin) { if (!fin.get(buf, 81)) break; if (strlen(buf) == 0) // Make sure we have something before continuing continue; if (buf[0] == '/' && buf[1] == '/') // Check for double slashes continue; // do rest of logic here, and use strtok or whatever string functions if necessary }
Best Regards, God Bless,
AstroNox
AstroNox
![]() |
Similar Threads
- Reading txt file into Hash Table (C++)
- Reading multiple lines from a txt file (C++)
- Setting an array and reading in a txt file backwards (C++)
- how to move to the second line in C++ .txt file reading? (C++)
- Need Help in Reading characters from a text file (C++)
- reading txt file into array (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- uploading .txt file via phpmyadmin to Mysql (MySQL)
Other Threads in the C++ Forum
- Previous Thread: Sorting in C++
- Next Thread: Speech recognition
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





