打开文件时出现问题

Problems opening a file

本文关键字:问题 文件      更新时间:2023-10-16

我对这方面还很陌生。我正试图打开一个文件,但程序每次都会崩溃。我以前打开过其他类似的文件,这已经奏效了。如果这有什么不同的话,我正在Visual Studio工作。如有任何帮助,我们将不胜感激。

#include <iostream>
#include <fstream>
using namespace std;
void splitStringByCommas (string s, string pieces[]);
struct month {
string name;
int order;
};
int main(int argc, const char * argv[]) {
string baseDir = "C:\Users\Brian\Desktop/";
string onerecord [2];
string oneline;
ifstream monthsfile;
monthsfile.open (baseDir + "months.txt");
int currentRecord = 0;
month months[12];
while (!monthsfile.eof() && currentRecord < 12) {
    monthsfile >> oneline;
    splitStringByCommas(oneline, onerecord);
    months[currentRecord].name = onerecord[0];
    months[currentRecord].order = atoi(onerecord[1].c_str());
    currentRecord++;
}
for (int i=0; i<currentRecord; i++) {
    cout << months[i].name << endl;
}
return 0;
}

// This function splitStringByCommas is necessary
// and I give it to you for free.
// Do not change this.
void splitStringByCommas (string s, string pieces[]) {
size_t comma = 0;
int piece = 0;
while (comma != string::npos) {
    comma = s.find(',');
    pieces[piece++] = s.substr(0, comma);
    s = s.substr(comma+1);
}
pieces[piece] = s; // remainder
}

如果需要更多信息,请告诉我。

您没有包含它,所以我无论如何都要声明它。

#include<string>
#include<iostream>
#include<fstream>
using namespace std;

正如您所说,您的编译器正在向您显示问题的确切位置。您将其命名为splitStringByComma。试试这个。用C++拆分字符串?