Advertisement
gisejo

Untitled

Nov 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. // open file
  2. fstream file(file_name, ios::in | ios::binary);
  3.  
  4. int vertices_count = 0;
  5. long long edges_count = 0;
  6.  
  7. // read header
  8. file.read((char*)(&vertices_count), sizeof(int));
  9. file.read((char*)(&edges_count), sizeof(long long));
  10.  
  11. // print graph
  12. cout << "Graph has " << vertices_count << " vertices" << endl;
  13. cout << "Graph has " << edges_count << " edges" << endl;
  14.  
  15. // get & print graph data for WEIGHTED graph
  16. for(long long i = 0; i < edges_count; i++){
  17.     int src_id = 0, dst_id = 0;
  18.     float weight = 0;
  19.  
  20.     // read i-th edge data
  21.     file.read((char*)(&src_id), sizeof(int));
  22.     file.read((char*)(&dst_id), sizeof(int));
  23.     file.read((char*)(&weight), sizeof(float)); // remove it for unweighed graph
  24.  
  25.     //print edge data
  26.     cout << src_id << " " << dst_id << " | " << weight << endl;
  27. }
  28.  
  29. file.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement