-1

I have .csv file with 502 lines and i want to organize them so that i can later sort them by duration,weekday,etc. I thought the best way to do this is creating sruct for each attribute but i dont know how to assign each attribute to the struct. Help pls

#include <iostream>
#include <fstream>

using namespace std;

struct classesstruct{
    string cadeira;
    string uccode;
    string weekday;
    int starthour;
    int duration;
    string type;
};


int main() {
    classesstruct Classes[502];
   ifstream myFile;
   myFile.open("C:/Users/utilizador/TrabalhoAED/classes.csv");

   while(myFile.good()) {
       string line;
       getline(myFile, line);
  
       
       
   }}```
  • Separate the sorting part from the reading. For the reading part search here. There are dozens of how to read a csv file in c++ questions. – drescherjm Oct 07 '22 at 21:25
  • `while(myFile.good()) {` read about this: [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Oct 07 '22 at 21:27
  • Start with the 2 duplicates for this question: [https://stackoverflow.com/questions/66067179/how-can-i-read-from-csv-file-to-a-class-in-c-into-either-arrays-or-public-clas](https://stackoverflow.com/questions/66067179/how-can-i-read-from-csv-file-to-a-class-in-c-into-either-arrays-or-public-clas) – drescherjm Oct 07 '22 at 21:28
  • 1
    drescherjm makes a good point about the loop condition, but to be clear, it's not the check of `.good()` that is the problem. It's the order of calling `.good()` and `getline()` that is the problem. `while (string line; getline(myFile, line).good())` is perfectly ok, if a bit unnecessarily wordy (the contextual cast of a stream to `bool` will implcitly call `good()`, so you can also write `while (string line; getline(myFile, line))` ) – Ben Voigt Oct 07 '22 at 21:28
  • i don't think C++ is the best tool for this for many reasons, you have a few things to learn before you are able to do this, like fstreams, lambda functions, stl functions, and a month or so of learning about C++ overall, if you are coding for simple things like reading and writing files, then you should use a higher level language, like python or java or C# – Ahmed AEK Oct 07 '22 at 21:34
  • 2
    @AhmedAEK: For this particular job, PERL is better than any of those. But often the need is to learn a tool (C++) and the task is a practical way of forcing oneself to learn. – Ben Voigt Oct 07 '22 at 21:37
  • Unfortunately, Stackoverflow is not a help site or a C++ tutorial site. If you are asking for help in learning how to do basic tasks in C++ then your best resource will be [a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Oct 07 '22 at 21:54

1 Answers1

0
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <ostream>

using namespace std;

struct classesStruct{
    string ClassCode;
    string UcCode;
    string Weekday;
    int StartHour;
    int Duration;
    string Type;
};


int main() {
    vector<classesStruct> ArrClasses;
    classesStruct classe;
   ifstream myFile;
   myFile.open("C:/Users/utilizador/TrabalhoAED/classes.csv");
    string line="";
    getline(myFile,line);
    string tempString;
   while(getline(myFile,line)) {
        stringstream inputString(line);
        getline(inputString, classe.ClassCode, ',');
       getline(inputString, classe.UcCode, ',');
       getline(inputString, classe.Weekday, ',');

       getline(inputString, tempString, ',');
       classe.StartHour=atoi(tempString.c_str());

       tempString="";
       getline(inputString, tempString, ',');
       classe.Duration=atoi(tempString.c_str());

       getline(inputString,classe.Type,',');

       ArrClasses.push_back(classe);

       line = "";
   }
   for (auto x:ArrClasses){
       cout << x.UcCode << endl;

   }}

solved it, thank u all for the help

  • Note: You still have a potential hole: You've guarded the case where for any reason you don't get a line, but you have not protected yourself against the data on the line not being exactly what you expect. For example an empty line at the end of the file will result in the `getline`s reading from `inputString` quietly failing and bogus values being used. – user4581301 Oct 07 '22 at 22:26