Advertisement
gisejo

Untitled

Nov 19th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<iomanip>
  4. #include<fstream>
  5. #include<cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. int main(int argc, char **argv){
  10.  
  11.     const char* file_name = argv[2];
  12.  
  13.     char undirected = argv[1][0];
  14.    
  15.     // open file
  16.     fstream file(file_name, ios::in | ios::binary);
  17.  
  18.     ofstream outfile;
  19.  
  20.     outfile.open (argv[3]);
  21.  
  22.     int vertices_count = 0;
  23.     long long edges_count = 0;
  24.  
  25.     // read header
  26.     file.read((char*)(&vertices_count), sizeof(int));
  27.     file.read((char*)(&edges_count), sizeof(long long));
  28.  
  29.     // print graph
  30.     //cout << "Graph has " << vertices_count << " vertices" << endl;
  31.     //cout << "Graph has " << edges_count << " edges" << endl;
  32.  
  33.     // get & print graph data for WEIGHTED graph
  34.     for(long long i = 0; i < edges_count; i++)
  35.     {
  36.         int src_id = 0, dst_id = 0;
  37.         float weight = 0;
  38.  
  39.         // read i-th edge data
  40.         file.read((char*)(&src_id), sizeof(int));
  41.         file.read((char*)(&dst_id), sizeof(int));
  42.         file.read((char*)(&weight), sizeof(float)); // remove it for unweighed graph
  43.  
  44.         //print edge data
  45.         outfile <<  src_id << " " << dst_id << endl;
  46.  
  47.         if (undirected == '1'){
  48.             outfile <<  dst_id << " " << src_id << endl;
  49.         }
  50.     }
  51.  
  52.     file.close();
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement