Ifstream文件不打开,虽然一切似乎就位(c + +) [英] Ifstream file does not open although everything seems in place (c++)

查看:186
本文介绍了Ifstream文件不打开,虽然一切似乎就位(c + +)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个程序来解析CSV文件的第一列和第十六列(转换为.txt)。我有与可执行文件夹中的CSV(posts.txt)文档。但是,每当我尝试运行可执行文件时,我的程序传递它无法打开文件(或!infile.is_open())。注意给我一些帮助?我在Mac OSX 10.8.3上运行在Xcode 3.2.3。代码如下所示。

I'm trying to write a program to parse the first and sixteenth columns of a CSV file (converted into .txt). I have the CSV ("posts.txt") document in the folder with the executable. But, whenever I try to run the executable, my program delivers that it cannot open the file (or that "!infile.is_open()"). Mind giving me some assistance? I'm running in Xcode 3.2.3 on Mac OSX 10.8.3. The code is shows below.

#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;
}

提前感谢!

推荐答案


我在可执行文件的文件夹中有CSV(posts.txt)文档。

I have the CSV ("posts.txt") document in the folder with the executable.

文件应显示在 当前工作目录中的进程,它可能是也可能不是可执行文件所在的目录。如果有疑问,尝试在 ifstream inFile(...); 中指定完整路径,看看是否改变了东西。

The file should be present in the current working directory of your process, which may or may not be the same directory where the executable lives. If in doubt, try specifying the full path in ifstream inFile(...); to see whether that changes things.

此外,该文件需要具有正确的权限,以确保其可被进程读取。

Additionally, the file needs to have the correct permissions to ensure that it's readable by the process.

这篇关于Ifstream文件不打开,虽然一切似乎就位(c + +)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆