我正在尝试编写一个程序来解析 CSV 文件的第一列和第十六列(转换为 .txt)。我在可执行文件的文件夹中有 CSV(“posts.txt”)文档。但是,每当我尝试运行可执行文件时,我的程序都会显示它无法打开文件(或“!infile.is_open()”)。介意给我一些帮助吗?我在 Mac OSX 10.8.3 上的 Xcode 3.2.3 中运行。代码如下所示。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string.h>
using namespace std;
void answeredPostGrabber()
{
ifstream inFile("posts.txt");
string postNumber;
string answerNumber;
string throwAway;
if(inFile.is_open())
{
while(inFile.good())
{
getline(inFile,postNumber,',');
cout << postNumber << ",";
for(int y=1;y++;y<16)
{
getline(inFile,throwAway,',');
}
getline(inFile,answerNumber,',');
cout << answerNumber << endl;
ofstream edges;
edges.open("edges.txt",ios::app);
edges << postNumber << "," << answerNumber<< endl;
edges.close();
ofstream nodes;
nodes.open("nodes.txt",ios::app);
nodes << postNumber << "\n" << answerNumber << endl;
nodes.close();
getline(inFile,throwAway);
}
}else cout << "ERROR: Unable to open file." << endl;
}
int main ()
{
answeredPostGrabber();
return 0;
}
先感谢您!