mingw64 + sublime 3 input output files not working - c++

I am trying to set up an environment for competitive programming. I have successfully installed mingw 64 with sublime 3. My code is able print hello world in the console. However, I am unable to read inputs and output into the input and output files. I am following the instructions here and this is the c++ code that I am trying to run.
#include<iostream>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout << "Hello world";
int var;
cin >> var;
cout << var;
}
I have the integer 5 in my input.txt file. When I build with c++ single file and run, nothing happens and the console just says [Finished in 5.6s]. I hoping that the code will read inputs that I place in the input.txt file and output them to the output.txt file. When I remove the 2 freopen code, Hello world is printed onto my console. May I know what am I doing wrong here? Thank you!

I had a similar problem and it turns out windows defender was blocking edit to output.txt file (A new feature has been added in windows defender called Controlled folder access). Check if your directory is not included in that feature. Are you using the default build system for c++ in Sublime? Check paths are correct

Related

Exe file not opening

I am trying to learn C++ now and created a Hello World program. When I compile it on Linux using g++ and it works perfectly fine. When I compile it on Windows using the Build tools, it still compiles the code into machine code, but I can't open the executable. I used the Microsoft build tools as a compiler. The code was:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
}
The output should be: Hello, World!
**Question already answered:
The program closes because it is not run in cmd. To prevent the program from crashing add
```system("pause");```
at the end**
The executable is showing the correct output on the terminal, but that terminal closes that fast that you don't even realise it.
I'd advise you to open a command prompt, go to the directory where the executable is located and launch it over there. You'll see the desired output.
it possibility because
the app closes immediately after ouputing
add system("pause");
on the end
Its not that you can't open that exe file.
Once you click on that file, it opens up and does its work and closes.
To prevent your exe file from getting closed after doing its operation, you can add following line at the end of the main function :
getchar();
And now once you open your exe file, it wont close automatically, you will need to press "enter" to close it.
#include<iostream>
using namespace std;
int main(){
cout<<"hello world";
getchar();
}

Can't execute compiled C++ exe file

I am having trouble executing my C++ code. I have written a basic "Hello World" program, and compiled it using the g++ make command. Here is my code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
I am on Windows 10, using Emacs for code editing, and CygWin for compilation. I saved this file as hello.cpp. I then navigated to the directory in CygWin. Then I did the command make hello. This created hello.exe. Then, I attempted to execute the file using ./hello.exe. I also tried ./hello which also didn't work. When I type one of these commands and hit Enter, it just on the next line, not doing anything. I can type in this blank line, but it won't do anything. Does anyone know a way to make my code execute properly. Thank you.
EDIT: I tried running this at cpp.sh, an online C++ compiler, and it worked fine.
Your program probably is working but the console window is closing before you can see anything.
Try adding an input at the end of the program so it will wait.
I.E.
int a;
cin >> a;
Your code is most likely executing, but not outputting anything. That's because it's failing. Try checking the return value after it has run with echo $?. If it's not 0 then it has crashed. Also run it in gdb and see if it fails. The reason why it's failing is most likely a windows/cygwin clash - it's not your code.

Variant on: New to Xcode can't open files in c++?

Like the questioner in "New to Xcode can't open files in c++?" I'm learning Xcode and OS X (I'm using Xcode 7 on a Yosemite mac).
I can get the code to work perfectly when I build and run it, but can't get the executable to work when I try to run it as a stand alone program.
I'm trying to translate some games I've written on a PC in C++ using SFML.
There has to be a way to save high scores and previous games within an app, but this has me stymied.
This is the sample code I used based on the previous question:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
string input;
fin.open("inputFile.txt");
if(fin.fail())
cout << "File failed to open." << endl;
fin >> input;
fin.close();
fout.open("outputFile.txt");
fout << input;
fout << "\n Data transferred \n";
fout.close();
}
This works perfectly when I build and run it, so I've got the proper path to the desktop folder set up. (I'm putting the data files in the same folder as the executable and specifying the path in Xcode.)
No problems when I run this within Xcode, but this is the message on the terminal console when I run the executable by itself:
"…/Desktop/datafiles/Build/Products/Debug/datafiles ; exit;
…/Desktop/datafiles/Build/Products/Debug/datafiles ; exit;
File failed to open.
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]"
Is there another flag or path that needs to be set within Xcode for this to work? Two other related questions: How do I access the terminal history to see what is going on? Finally, if I set up the project as an SFML app instead of a terminal project (command line tool app), why can't I see the files within the SFML app, even though I've set the command line flag to see hidden files, and I can see other hidden files on my hard drive? I can see the files if I open the SFML app folder in Windows, so I know they are there.
This is my first question on Stack Overflow, so apologies if this should be appended to the previous question, but this doesn't appear to be an answer to me, but is quite a different version of the original question that is not addressed in the answers.
Thanks!

I can't read a .dat file using fstream

I know there are a ton of questions pertaining to this subject but I cannot get this to work. The program ran fine on my laptop, but when I try to compile and run it in the the schools Linux lab the program cannot open the file. I have tried defining the absolute file position but nothing has worked. The file name is correct and everything but when I try to run the program it displays "failed". I'm using gedit and compiled the program with bash.
ifstream fin("rainfall.dat"); // If the file cannot open display failed
if(fin.fail()){
cout << "failed" << endl;
return 1;
}
try
#include <errno.h>
if(fin.fail())
perror("open failed ");
this will give you a human readable message for the last error

ofstream outputs in debug but not run

I have a very simple C++ program with a very simple project setup, but when I run the program I get no output. If I run the program in debug mode, it works perfectly. I am using Eclipse Kepler CDT 32 bit on windows with MinGW. I am somewhat new to eclipse, so it's probably something I did wrong.
The program is:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
ofstream outfile("testdata.txt");
int main()
{
outfile << "Program Start\n";
cout << "Program Start\n";
return 0;
}
Help!
If the problem is that the program quickly opens and then closes before you can see the output on the screen, then you can just run your program from any shell (CMD on Windows, bash on Linux, etc.). That way, it won't exit once your program ends and you can see the results.
Make sure also that you flush/close your ofstream before your program exits.
The problem is rather not releted to c++ itself. You should check if via "cmd" typying it in "launch menu" after you click start. Find the path of your program, then run it.
For the very beginning it is recommended to spend a few hours with terminal(cmd). To know how things works. After that you will be independent - you will be able to write the code in any IDE. Also simple trick to make it working is to use std::cin.get() . It is prefered to system("pause").
You open the testdata.txt file using the relative path.
And the created file may be created in the project binary output path, where the executable located in.
You can use everything software to check whether a file is created and its created path.
everything
For example, you can type your output file name testdata.txt into everything software to see where output file created.And check if the testdata.txt created in a wrong path or directory.