0

I have a project which builts and runs just fine. But when I go to bin/release and double click .exe there it doesn't only disappear too quickly, but also doesn't get the right output (which is most likely because it can't find its link to .txt file, which is used in usual codeblocks environment quite normally).

Edit: I tried cin.get(), system("pause") to stop .exe from closing, nothing works!

parsecer
  • 4,758
  • 13
  • 71
  • 140
  • 1
    Your title doesn't seem to match your issue since you already have an exe. What happens if you run the program from the terminal instead of double clicking on it though the OS UI? Also how is the file directory specified in your code? – NathanOliver Jun 23 '16 at 15:07
  • 2
    ***But when I go to bin/release and double click .exe there it doesn't only disappear too quickly*** For a console application that only outputs data that is normal / expected. Windows will close all programs when execution terminates. Run the program from a `cmd.exe` window to see its output. – drescherjm Jun 23 '16 at 15:12
  • 1
    ***doesn't get the right output (which is most likely because it can't find its link to .txt file*** when you double click the default folder will be the one containing the exe. Is that where you have your file. – drescherjm Jun 23 '16 at 15:12
  • 1
    The question probably should be "how to execute a console application in Microsoft Windows". – drescherjm Jun 23 '16 at 15:14
  • @NathanOliver Something's wrong with .txt file. When I run it from codeblocks it's fine, but when i run it from .exe it can't open it. – parsecer Jun 23 '16 at 15:20
  • 1
    How do you open the file in your code? What is the path you use? Where is the file located? – NathanOliver Jun 23 '16 at 15:20
  • 1
    Most likely the text file is in the wrong folder so its not found. Your program should include code to handle this error and print an error message. – drescherjm Jun 23 '16 at 15:21
  • @NathanOliver `ifstream file("bin/Release/input.txt");` I use this – parsecer Jun 23 '16 at 15:23
  • 1
    You need to run the executable from two directories up the tree. [Read up on working directories](https://en.wikipedia.org/wiki/Working_directory) – user4581301 Jun 23 '16 at 15:24
  • Looks like when I make `ifstream file("bin/Release/input.txt");` and run from codeblocks its ok and not ok from exe and cmd. When I make `ifstream file("input.txt");` it's wrong from codeblocks and ok from exe and cmd... – parsecer Jun 23 '16 at 15:24
  • So which variant is right? It seems I can't have it both ways and be able to execute both from .exe and codeblocks. – parsecer Jun 23 '16 at 15:26
  • 1
    The second is better. ***It seems I can't have it both ways and be able to execute both from .exe and codeblocks.*** You should be able to set the default folder in codeblocks to the location of the executable instead of the root project. – drescherjm Jun 23 '16 at 15:27
  • @user4581301 Well I guess in codeblocks my working directory is upper and I need another path to txt, comparing to when iI launch it from exe. I tried to put `input.txt` in both ../bin and bin/Release but it doesn't seem to work (can anyone tell why - the filenames are the same) – parsecer Jun 23 '16 at 15:29
  • 1
    Again I expect codeblocks (I don't use codeblocks) has an option to allow you to change what the current folder is when a program is executed. If you change that to where the executable is then the code will not need the path. – drescherjm Jun 23 '16 at 15:33
  • 1
    Most IDE's play with the working directory to make the program run from a directory easily visible to all build variants so that you can easily use the same files for both the debug and release builds, for example. It looks like Code::Blocks is using the directory immediately before bin. – user4581301 Jun 23 '16 at 15:34
  • 1
    This answer may help: http://stackoverflow.com/questions/1528298/get-path-of-executable It allows the program to find where the exe is. You take the result of [GetModuleFileName](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx), replace the exe name with your file name, then open that. For your purposes GetModuleFileNameA will be a bit easier to use because it returns the more-limited 8 bit characters rather than wide characters needed to support multiple language support. – user4581301 Jun 23 '16 at 15:38
  • Thanks for the answers, I will look into how to change that default directory in codeblocls. But the .exe still closes( – parsecer Jun 23 '16 at 15:43
  • 1
    To stop the closing, force it to wait for input. Easy way is to put a `char junkvariable; cin >> junkvariable;` at the end of the program to force the program to wait for the user to hit enter. If the program crashes and exits before the end, that's a bug you'll have to fix. – user4581301 Jun 23 '16 at 15:44
  • Figured it out! It just never reached the inputs, because of returns somewhere in the code. – parsecer Jun 23 '16 at 15:45

1 Answers1

1

Your program will terminate if it has finished. So if you want to stop it from instantly disappearing you should either pause the program or wait for user input such as enter. As for your path. If you want to open "text.txt" then your program will look in the same folder as your executable is in.

Shiro
  • 2,610
  • 2
  • 20
  • 36
  • As it turns out with newly-provided information, the last part is not true. OP had written their program to assume the Code::Blocks IDE's directory structure and working directories. Recommend a quick update. – user4581301 Jun 23 '16 at 15:28
  • I tried those things, it still closes. `cin.get(); system("pause");` – parsecer Jun 23 '16 at 15:40