尽管一切似乎都已到位,但ifstream文件仍无法打开(c)
作者:互联网
我正在尝试编写一个程序来解析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;
}
先感谢您!
解决方法:
I have the CSV (“posts.txt”) document in the folder with the executable.
该文件应存在于进程的current working directory中,该进程可能与可执行文件所在的目录相同,也可能不同.如果有疑问,请尝试在ifstream inFile(…)中指定完整路径;看看是否会改变一切.
此外,该文件需要具有正确的权限,以确保该进程可读取.
标签:ifstream,c,file 来源: https://codeday.me/bug/20191007/1866655.html