简体   繁体   English

调试时打开文件时出错

[英]Error opening file when debugging

When i try to run the following program: 当我尝试运行以下程序时:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    std::ifstream inFile("test.txt");

    if(!inFile.is_open()){
        std::cout << "Doesn't work" << std::endl;
    }

    inFile.close();
    return 0;
}

The program isn't able to open the file, the file exists in the same folder as the executable(I also tried to put in the explicit path of the file: C:\\Users\\..) 该程序无法打开文件,该文件与可执行文件位于同一文件夹中(我也尝试将其放置在文件的显式路径中:C:\\ Users \\ ..)

The value of the variable inFile after trying to open the file is: 尝试打开文件后,变量inFile的值为:

+ inFile    {_Filebuffer={_Set_eback=0xcccccccc <Error reading characters of string.> _Set_egptr=0xcccccccc <Error reading characters of string.> ...} }    std::basic_ifstream<char,std::char_traits<char> >

Try this: 尝试这个:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    std::ifstream inFile("c:\\test.txt");

    if(!inFile.is_open())
    {
        std::cout << "Doesn't work" << std::endl;
    }

    inFile.close();
    return 0;
}

This should work if you put your file to C:\\test.txt . 如果将文件放入C:\\test.txt则此方法应该有效。 The rest is up to you to figure out. 其余的工作由您自己决定。 Relative paths work, you need to make sure the file is where your program looks for it (or the other way 'round). 相对路径有效,您需要确保文件在程序查找的位置(或者相反)。 Try to print out the current path if you are unsure what's wrong. 如果不确定出什么问题,请尝试打印当前路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM