0

I have text file that is a large list of integers separated by commas.

E.g. "1,2,2,3,3,4,1,3,4,10". Every two integers represents a vertex adjacency in a graph.

I want to create some C++ code that reads the text file and recognizes every two integers as some sort of data structure (such as a Boolean for yes/no adjacent).

Additionally I will be creating a class that colors this graph using these adjacencies. Being able to have the code remember what color or value each vertex has been given (this is not as important yet as just getting the program to parse in the data from the text file), but help with this would be appreciated or appear in a later question.

How would I do this in C++?

Gene
  • 104
  • 15
  • 2
    Create a class that encapsulates the tuple. Then override the `>>` and `<<` operators. – AndyG Jan 30 '18 at 23:31
  • 1
    [Reading a CSV file](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) is easy. Then just combine every two values into a vertex class. – Bo Persson Jan 30 '18 at 23:48
  • Show what you have done already. You need to post a [mcve] – Jive Dadson Jan 31 '18 at 08:26

1 Answers1

0

Assuming that the input file is one line comprised of a list of integers with a comma as the separator. Seems like the mains steps are to:

  1. Read in the the line as a string.
  2. Parse the individual numbers out of the string (using ',' as the delimiter). So that each number is its own separate string.
  3. Convert those individual strings to integers.
  4. Once you have a vector of integers it should be pretty straightforward to get every pair and do as you wish.

Example:

#include <iostream> 
#include <fstream>
#include <sstream> 
#include <vector>
#include <string>

using namespace std;

int main() {

    fstream inFile;
    inFile.open("input.txt");       // File with your numbers as input.
    string tempString = "";

    getline(inFile, tempString);    // Read in the line from the file.
                                    // If more than one line per file you can put this whole thing in a loop.
    stringstream ss(tempString);    // Convert from a string to a string stream.

    vector<string> numbers_str = vector<string> (); // string vector to store the numbers as strings
    vector<int> numbers = vector<int> ();           // int vector to store the numbers once converted to int

    while(ss.good())    // While you haven't reached the end.
    {
        getline(ss, tempString, ',');       // Get the first number (as a string) with ',' as the delimiter.
        numbers_str.push_back(tempString);  // Add the number to the vector.
    }

    // Iterate through the vector of the string version of the numbers.
    // Use stoi() to convert them from string to integers and add then to integer vector.
    for(int i=0; i < numbers_str.size(); i++)
    {
        int tempInt = stoi(numbers_str[i], nullptr, 10);    // Convert from string to base 10. C++11 feature: http://www.cplusplus.com/reference/string/stoi/
        numbers.push_back(tempInt);                         // Add to the integer vector.
    }

    // Print out the numbers ... just to test and make sure that everything looks alright. 
    for(int i = 0; i < numbers.size(); i++)
    {
        cout << i+1 << ": " << numbers[i] << endl;
    }

    /* 
    Should be pretty straight forward from here. 
    You can go iterate through the vector and get every pair of integers and set those as the X and Y coor for your
    custom data structure.
    */
    inFile.close(); // Clean up. Close file.
    cout << "The End" << endl;
    return 0;
}

If your file has more than one line then you can just extend this and do the same thing for every line in your file.

Hope that helps.

NerdNoob
  • 5
  • 3