0

I want to read from a text file in the following format:

1,1,1
2,2,2
3,3,3
4,4,4
5,5,5

Parse its rows into a struct:

//Where the loaded data goes
struct process_stats{
    int process_number  = UNINITIALIZED;
    int start_time      = UNINITIALIZED;
    int cpu_time        = UNINITIALIZED;
};

And then add the structs to a vector.

I am currently using an overloaded extraction operator for istream to retrieve the numbers, like such:

//Operator to read from file into struct and then add to vector
std::istream& operator>>(std::istream& is, process_stats& nums)
{     
    is >> nums.process_number >> nums.start_time >> nums.cpu_time;
    return is;
}

And then I want to add those numbers straight into the vector 'stats'. This is what I have so far:

#include <fstream>
#include <iostream>
#include <vector>
#include <iterator>
#include "Utilities.h"

//Vector to hold process_stats structs
std::vector<process_stats> stats;

//Operator to read from file into struct and then add to vector
std::istream& operator>>(std::istream& is, process_stats& nums)
{
    char comma;

    is >> nums.process_number >> comma >> nums.start_time >> comma >> 
        nums.cpu_time;

    return is;
}

//Loads data from file into process_stats struct, which is then added to 
//vector stats
int loadData(const char* filename)
{
    //Result to be returned:
    //  SUCCESS - file successfully opened,
    //  COULD_NOT_OPEN_FILE - file could not be opened.
    int result;

    std::fstream inFile(filename);

    //Check if file is open - grab data
    if(inFile)
    {
         std::copy(std::istream_iterator<process_stats>(inFile),
                    std::istream_iterator<process_stats>(),
                    std::back_inserter(stats));

          inFile.close();
          result = SUCCESS;
    }

    //Could not open file
    else
    {
        result = COULD_NOT_OPEN_FILE;
    }

    return result;
}

I tried referring to this post: Read file line by line But so far, no luck... Am I missing something, or is there a better way to implement this?

tang
  • 33
  • 1
  • 7
  • look at the getline() function. It reads a line from an ifstream into a string. You may also find the find_first_of() and find_first_not_of() member functions of the string class helpful for separating numbers from other junk, like commas. – Brad S. Sep 14 '17 at 22:16
  • It may be helpful to create a [mcve] – Brad S. Sep 14 '17 at 22:26
  • @Brad S. I did look at the getline() function, but I thought it was intended for reading in strings... I just want to read in the numbers for my struct, to then add to my vector. I will review and edit the question so that it is easier to follow. This is my first C++ program and one of my first programming-related questions on stack, so I apologize for the lack of clarity. – tang Sep 14 '17 at 22:37

1 Answers1

2

The data format is

1,2,3

but

is >> nums.process_number >> nums.start_time >> nums.cpu_time;

only knows how to read numbers and cannot process the commas.

char comma;
is >> nums.process_number >> comma >> 
      nums.start_time >> comma >> 
      nums.cpu_time;

will consume the commas by reading them into the aptly named comma.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • This is correct, however, my program is still terminating... Seems like it is still not reading from the file. – tang Sep 14 '17 at 22:41
  • @tang That I can't help with other than suggesting you place a breakpoint on `result = COULD_NOT_OPEN_FILE;` or a print statement that outputs the value of `result`. If you are not opening the file, make sure the input file is where you think it is. If you are not using the file's full path, make sure that the program is being run (the program's working directory) from the directory where you think it is. – user4581301 Sep 14 '17 at 22:58
  • Added a print statement to make sure I was opening and reading the file, which I was...but the output was in a different format from that of the original text file. – tang Sep 14 '17 at 23:54
  • Could you be more specific about how the input and output are different?This seems to work: https://ideone.com/25JJUk Can't tell you much about your program since there isn't any output in it. – Retired Ninja Sep 15 '17 at 00:23
  • @RetiredNinja Eventually, the program will serialize the vector full of process_stats structs to a file... but I am still having trouble reading from the original text file. I tried implementing your code, but so far it won't print anything out. – tang Sep 15 '17 at 01:08