如何在Visual Studio Community 2015上打开C ++输入文件? [英] How to open input file, C++ on visual studio community 2015?

查看:91
本文介绍了如何在Visual Studio Community 2015上打开C ++输入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么无法打开文件吗?我也尝试仅将"infile.txt"放入程序文件夹以及调试文件夹,但是我用来检查打开错误的方式均触发了它无法打开的意思.我知道我可以对位置进行硬编码,但我不想这样做.

Does anyone know why the file isn't opening? I also tried just putting "infile.txt" and placing it in the folder of the program and also the debug folder but the ways I used to check for open error both triggered meaning that it could not open. I know I can hard code the location but I don't want to.

听说您应该执行 stringobj.c_str(),但是我不知道这是否正确?

I heard you should do stringobj.c_str() but I don't know if that's accurate?

#include "stdafx.h"

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    ifstream infile;
    ofstream outfile;

    string fileloc = "infile.txt";

    infile.open(fileloc);


    if (!infile)
    {
        cout << "open fail 1" << endl;
    }

    bool fail = infile.fail();
    if (fail)
    {
        cout << "open fail 2";
    }

    return 0;
}

推荐答案

请注意,目录结构(至少对于VS2013)为

Note that the directory structure (at least for VS2013) is

<base>
   - Solution Directory
     - Debug
     - Release
     - Project Directory
       - Debug
       - Release

默认情况下,该程序在项目目录中运行(即使它是在solution/debug目录中构建的).

The program by default runs in the project directory (even though it is built to the solution/debug directory).

如果在启动项目时接受默认的命名约定,则应将文件放置在"Projects \ ConsoleApplication1 \ ConsoleApplication1"目录中,而不是"Projects \ ConsoleApplication1"

If you accepted the default naming convention when starting your project, you should be putting your file in the "Projects\ConsoleApplication1\ConsoleApplication1" directory, not "Projects\ConsoleApplication1"

这篇关于如何在Visual Studio Community 2015上打开C ++输入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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