简体   繁体   中英

Problems running code in non-debugging mode

I'm using Visual Studio 2012 Premium. When this code is executed normally (not in the debugger), the only output is a blank line. But when it is run in the debugger, the output is "January 9 8". Switching between Debug and Release makes no difference and turning off the optimization for the main() function using #pragma optimize("", off) does nothing. What can I do to make it output the "January 9 8"?

P02.cpp contains:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main(){

    system("cls");

    string s = "";

    ifstream tt;
    tt.open("P02.DAT");

    getline(tt, s);

    cout << s << endl;

    return 0;
}

P02.DAT contains:

January 9 8
February 19 17
March 20 23
April 20 15
May 15 16
June 8 7
July 12 9
August 20 18
September 21 27
October 18 17
November 19 24
December 18 17

This sounds like a classic working directory issue. Make sure that P02.DAT is in the same directory as your executable when running it outside of Visual Studio.

Visual Studio generally sets the current working directory to the root of the solution when executing within the IDE. You may have a layout like:

  • build\\project.sln
  • build\\P02.DAT
  • build\\debug\\project.exe
  • build\\release\\project.exe

When executing project.exe in Visual Studio it will set the working directory to build, allowing your application to access P02.DAT. If you execute project.exe outside of Visual Studio (say in a cmd instance), it will look in whatever directory you are in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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