【发布时间】:2016-08-01 10:49:50
【问题描述】:
我有一个程序可以从文件夹中读取不同位置的文件。成功读取文件后,我将文件的 ifstream 变量分配给临时变量。下面的代码是一个例子:
while (!CBAsales.eof())
{
string sFilePath = "Datafolder/CBA/";
sFilePath = sFilePath.append(sFileName);
// Read the csv file
infile.open(sFilePath);
if (!infile)
{
cout << sFileName << ": File not found!!!" << endl;
exit(1);
}
while (getline(infile, record, '\n'))
{
while (!infile.eof())
{
if (record.size() > 0)
{
//insert elements here
}
}
}
infile.close();
mapIAG.insert(pair<string, Vector<Stock>>(sFileName, V1));
}
CBAsales.clear();
CBAsales.seekg(0, ios::beg);
tempSales = &CBAsales;
}
这是文件的读取。现在您可以看到 tempSales = &CBAsales;它被分配给一个临时变量。我在全局范围内声明了这些变量,并且有一个指向 tempSales 的指针,如下所示。
ifstream codeindex;
ifstream IAGsales;
ifstream CBAsales;
ifstream NABsales;
ifstream * tempSales;
ifstream infile;
这里是我显示所有必要记录的地方:
cout << "Enter date of transaction" << endl;
while (!tempSales->eof())
{
getline(*tempSales, record);
cout << record << endl;
}
这是我得到的错误:http://i.imgur.com/I9lv2zv.png
为什么即使分配了它也指向null?我是不是用错了指针?
【问题讨论】:
-
@RichardHodges 我尽量让它尽可能简约。 sn-p 与我的问题相关,因此我添加了这些代码行。
-
最小的意思是提供一个完整的 10 行程序,表现出相同的行为,所以我们可以重现它!
-
使用调试器怎么样?处理异常总是一个好的开始,它可以帮助您通过代码跟踪错误...
-
@Bentoy13 好吧,我确实使用了调试器,这就是我生成屏幕截图中给出的错误的方式。
标签: c++