Cannot create new text files by C++ in Codeblocks in Mac - c++

I have a C++ code in Codeblocks in Mac OS that should create some .txt files and write something on them. It worked on Windows, however, it does not run correctly on Mac. It does not make any errors, and it announces that the procedure is terminated with status 0; however, it does not create any .txt files!! What can be the problem?
It even does not claim that Codeblocks needs special permission. Moreover, the execution window background is White; different from the usual execution window of Codeblocks, which is Black.
This is the code:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
int main(){
for (int i=1;i<=1;i++){
ofstream fout ("test1.txt");
int n=rand()%10+1;
// int n=15;
fout<<n<<endl;
}
return 0;
}
This code just does not create any .txt file in Mac, while it worked on Windows.

"test1.txt",this is a relative path, you maybe hava use clion,you must know where relative path, but it is using a absolute path.

Related

'initwindow' doesn't open a window in code:blocks c++

I wanted to use the code blocks to draw circles. I downloaded BGI and put the files of the BGI in the correct places in the code blocks folder (and also changed the complier settings from codeblocks). However, when calling the initwindow from the main in the codeblocks, nothing happend (there isn't any window);
#include <iostream>
#include <graphics.h>
int main()
{
initwindow(900,900);
circle(90,90,90);
return 0;
}
Thanks in advance.

C++ "system(command)" not working on NetBeans / Windows

I am working on a C++ project using CodeBlocks on Windows but then decided to switch to NetBeans IDE 8.2.
Within my project, I am calling another executable file with some passed parameters (I run the other .exe with suitable parameters then I take the output of it to use in my main project), and it used to work on CodeBlocks but not on NetBeans.
The code is the following:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "My_Constants.h"
#include "Data.h"
#include "Parameters.h"
#include "Pattern.h"
#include "Squish.h"
#include "Deserializer.h"
#include "Automatic_Run.h"
using namespace std;
int main()
{
Parameters parameters;
parameters.mode = SQUISH;
Automatic_Run runner;
string inputname;
//--------------------------------User Input-------------------------------------
cout << "enter input file name \n";
getline(cin, inputname);
parameters.inputFileName.assign(inputname);
cout<<"=============================================================================\n";
//--------------------------------Running SQUISH/first call--------------------------
cout<<"Running SQUISH - first call\n";
char command[1000]="";
string passedParameters = " -i "+parameters.inputFileName +" -f "+ "t";
strcat(command,"C:\\Users\\Administrator\\Documents\\CodeBlocksProjects\\TestSQUISH\\bin\\Debug\\TestSQUISH.exe ");
strcat(command,passedParameters.c_str());
int result = system(command);
// the rest of the code(not relevant to the problem)
return 0;
}
On CodeBlocks, it used to work perfectly and give me the output as a file in the path of my main project (the one I am calling TestSQUISH from). However, now on NetBeans, I am getting the following error:
sh: C:UsersAdministratorDocumentsCodeBlocksProjectsTestSQUISHbinDebugTestSQUISH.exe: command not found
I checked the terminal of NetBeans to get an idea of what is happening (assuming it might be related) and I noticed that I have to change the path first, then run the executable using:
./ TestSQUISH.exe (+parameters)
However, that also didn't work for my project.
Can anyone please suggest a solution or tell me how I can make NetBeans run the command on a Windows terminal as CodeBlocks used to do?
Go to the project settings and set the project path for execution to be the folder where the other application is.
OR
Set the system path to include that folder.
Thanks to the comment of #Yksisarvinen, I was able to solve the problem.
Noticing that NetBeans uses the shell and not the Windows-style commands and after using the NetBeans own terminal to really get a clear idea of how it translates paths, I was able to run the code successfully using the following:
char command[1000]="";
string passedParameters = " -i "+parameters.inputFileName +" -f "+ "t";
strcat(command, "/cygdrive/c/Users/Administrator/Documents/CodeBlocksProjects/TestSQUISH/bin/Debug/TestSQUISH.exe ");
strcat(command,passedParameters.c_str());
int result = system(command);
Netbeans terminal adds cygdrive to the beginning of the path, and uses c instead of C:.
And in case the executable is in the same directory as your own project then this would be enough:
char command[1000]="";
string passedParameters = " -i "+parameters.inputFileName +" -f "+ "t";
strcat(command ,"./TestSQUISH.exe ");
strcat(command,passedParameters.c_str());
int result = system(command);

Can you open a file from a an online compiler (repl.it) using C++

I want to see if it's possible to open a file, write and save it using an online compiler. The reason why I am using that is because I am using a really old macbook that has a hard time running xcode or codeblocks. The file I want to open is saved on my desktop under the file name "image.ppm". This is the code :
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char filename[100];
ifstream input;
cin.getline(filename, 100);
input.open(filename);
}
When I run it nothing happens and I assume it's because the online compiler can't see the image.ppm file
Whether or not repl.it will let you read/write files is irrelevant; there is of course no way it can see files on your machine.

Open an additional program with c++ on Mac [XCode]

I want to open an additional program with c++ on XCode.
It is Firefox.
But if I make
Shell Execute("file://localhost/Applications/Firefox.app");
There is an error 'ShellExecute' was not declared in this scope
In other forum there was a clue to include windows.h and shellapi.h
#include <shellapi.h>
#include <windows.h>
but that makes other errors
shellapi.h: No such file or directory
windows.h: No such file or directory
What should I do? I want to open frefox with c++ in XCode on Mac?
Try running this in Terminal to open Firefox:
open -a Firefox http://www.ibm.com
If that does what you want, you need to wrap it in system() like this:
#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
std::system("open -a Firefox");
}
ShellExecute() is only available through the Windows API. You don't have a Windows system.
You can simply use the (more portable) system() function, or one of the exec() functions available on POSIX compliant systems.
I tried the same with chrome and I had to set it in inverted commas:
int main() {
system("open -a 'Google Chrome'");
return 0;
}
with the apostrophe it worked!

Reading Text file line by line in QtCreator does not work

I am trying to code c++ in QtCreator 3.4.2. I create Non Qt Project - Plain C++ Project. I have a code, which tries to read content of text file line by line and output to the console screen. But it does not write anything. I run same code in Visual Studio 2013 it works very well, without any problem. So What is the problem. The code is below.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fin("input.txt");
string s;
while(getline(fin,s))
{
cout<<s<<endl;
}
fin.close();
return 0;
}
Check that the input.txt file is in the working directory of the program. Note that the working dir is not the same as the folder the program's .exe is in - you can set it from the project properties.