打开文件时出现问题
Problems opening a file
我对这方面还很陌生。我正试图打开一个文件,但程序每次都会崩溃。我以前打开过其他类似的文件,这已经奏效了。如果这有什么不同的话,我正在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++拆分字符串?
相关文章:
- 使用mongocxx驱动程序时包含头文件问题
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 编译要在英特尔Hyperscan中使用的.cc文件时出现问题
- 使用 seekg() 读取C++中的文件时出现问题
- sqlite3 和生成文件中的链接出现问题
- C++头文件和类实现出现问题
- 用户输入字符串的文件附加问题..C++
- node-gyp 的先有鸡还是先有蛋的问题:指向依赖项中的头文件
- 读取文件并将其存储在unordered_map中时出现问题
- 编译 llvm 插件时出现问题:llvm/Config/llvm-config.h:没有这样的文件或目录
- 我在使用C++文件时遇到了一些问题
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 将文件复制到自定义位置,存在字符串转换问题
- DirectX12 的问题:"d3dApp.h":没有这样的文件或目录
- (ODR 使用问题)在不同文件中priority_queue名称相同的结构
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 读取制表符分隔的文件时出现问题 C++
- 当我在 CLion 中读取数组中的 txt 文件时C++编码问题
- 使用 RapidXML 解析大型 XML(大小大于 65 KB)文件时出现问题
- C++:读取.BMP文件时出现问题;文件结束时间早于预期