0
void ImportFileCSV()
{
    string year, semester, Class, sourcefile;
    cout << "Enter academic years: ";
    cin >> year;
    cout << "Enter semester: ";
    cin >> semester;
    cout << "Enter class: ";
    cin >> Class;
    cout << "Enter file: ";
    cin >> sourcefile;
    ifstream csvFin;
    csvFin.open(sourcefile,ios::in);
    if (!csvFin.is_open()) {
        cout << "Can not open CSV file to import!!!" << endl;
        return;
    }
    ofstream fout("CS162\\Schedule\\"+year + "-" + semester + "-Schedule-" + Class+".txt", ios::out);
    while (csvFin.good()) {
        string line;
        getline(csvFin, line, ',');
        fout << line;
    }
    fout << sourcefile;
    csvFin.close();
    fout.close();
}

sourcefile :CS162\Schedule\Schedule-19APCS1.csv

FILE CSV INCLUDE:


No,Course ID,Course Name,Class,Lecturer user,Lecturer name,Lecturer Degree,Lecturer gender,Start date,End date,Day of week,Start hour,Start minute,End hour,End minute,Room 1,CS162,Introduction to Programing,19APCS1,dbtien,Dinh Ba Tien,TS,1,1/4/2020,1/6/2020,MON,7,30,11,30,I42 2,CM101,Communications,19APCS1,dnvu,Duong Nguyen Vu,GS,1,1/4/2020,1/6/2020,SAT,13,30,17,30,I41

======================================================================== But screen output : Can not open CSV file to import!!!

1 Answers1

0

The reason is that the source file is not in the directory, where it should be, or where it is specficied. Please carefully check again and again. Also the pathname could be writting wrong.

You program will also not work, while (csvFin.good()) is a broken design. There are many many pages here on Stackoverflow on how to do that. The most popular answers with detailed explanations can be found here

Armin Montigny
  • 7,879
  • 3
  • 11
  • 29