Running .exe file by CreateProcess cannot generate .txt file - c++

I have a hello.cpp file:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
cout << "hello" << endl;
ofstream file;
file.open("codebind.txt");
file << "this text is written using C++\n" << "This is a number: " << 10;
file.close();
return 0;
}
After compiling this file by g++ using command prompt: g++ -o output hello.cpp, it generated a output.exe file. Then, I managed to run this output.exe file in another .cpp program called program.cppusing CreateProcess() (link for code). Unfortunately, when I hit Build, the system did not create any codebind.txt file despite the fact that the exitcode variable in program.cpp had a value of 0 as expected. Then I tried going to Explorer and running hello.exe mannually, it created the codebind.txt file.
Could someone tell me what's wrong with my program.cpp file and how to create codebind.txt using CreateProcess? Any help will be appreciated!

Related

Why is this not producing a file in the current directory?

I have some trouble with producing files in C++. I consulted this answer here but when I try using it, it doesn't produce a file. What I wrote:
//~/Documents/Test_CPP/ex2/main_2.cpp
#include <fstream>
int main()
{
std::ofstream file("Hello.txt");
// Hello.txt has been created here
}
I compile it with the command g++ main_2.cpp and run it with ./a.out. I don't really know what could go wrong here, except theorizing that the file might be produced not in the current directory but somewhere else. So I tried changing Hello.txt to ~/Documents/Test_CPP/ex2/Hello.txt, which doesn't change anything. What exactly am I doing wrong here?
I have encountered this problem on macOS with Xcode if you use some IDEs you should point to build-dir.
My suggestion: use std::filesystem::current_path(). It will give full path to you elf\exe dir.
#include <string>
#include <iostream>
#include <filesystem>
#include <fstream>
int main() {
std::string file_name{"Hello.txt"};
auto path{std::filesystem::current_path()};
path = path / file_name;
if (std::filesystem::exists(path)) {
std::filesystem::remove(path);
}
std::ofstream out_stream(path, std::ios::out);
if (!out_stream.is_open()) {
std::cerr << "Error open file" << std::endl;
return -1;
}
out_stream << "test" << std::endl;
out_stream.close();
return 0;
}
This can sometimes happen if you do not properly terminate the connection to the file
EG.
file.close();
This must be done before the program terminates.

Why doesn't this open the file in the directory of the program?

I have this short program:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
int main (int argc, char * argv[]) {
std::string homedir = std::getenv("HOME");
std::string filename = argc > 1 ? argv[1] : (homedir + "/" + "file");
std::cout << homedir << std::endl;
std::cout << filename << std::endl;
std::fstream file;
file.open(filename, std::ios::out);
file << "Yo yo waddup" << std::endl;
file.close();
return 0;
}
When I supply no arguments, it opens a file in the users home directory. That of course makes sense. But when I run it from a different directory like this:
$ ./folder/hometest examplefile
The program creates "examplefile" in my current directory instead of the directory where the program is.
Why exactly is this happening?
Why exactly is this happening?
The program is behaving just as expected.
The file is opened relative to the current work directory, not where the executable is located.
If it didn't work that way,
All your programs will have to work with absolute paths, or
The location of the program will be flooded with files. First, that might not be possible because of permissions issue. Second, in a multi-user system, users will end up trying to create the same file names/directories.
Neither of the above is desirable.

Double Clicked Compiled C++ Unix Executable Doesn't Open Existing File to Read Information From

I've been searching online to solve the above issue with no
success so far. I will describe the issue in more details below.
My program contains only one .cpp file. The program should display text from "test.txt" if this file is opened. Otherwise, it should display the "Failed to open ..." message. The issue follows:
I open terminal, go to the directory containing my file, compile and run with the usual commands: "g++ main.cpp" and "./a.out". When I run my program in this way, using terminal directly, the program works correctly. It displays text when the text file exists and outputs error when it doesn't exist. When I double click the unix executable "a.out", even though the text file exists and is put side by side with the executable, the program displays "Failed to open ..." message. I don't know what to think at that point. Should code contain anything else besides what is below?
Operating system: OS X 10.9.5
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_CHAR_READ = 100;
int main(int argc, const char * argv[])
{
ifstream read_file;
cout << endl << endl;
//Allocate dynamic memory
char * file = new char[strlen("test.txt") + 1];
char * text_line = new char[MAX_CHAR_READ + 1];
strcpy(file, "test.txt");
//Attempt to open a file for reading
read_file.open(file);
if(read_file.is_open() == true)
{
cout << "File: " << file << " is open!" << endl;
read_file.get(text_line, MAX_CHAR_READ, ';');
cout << text_line << endl;
read_file.close();
}
else
cout << "Failed to open: " << file << endl;
cout << endl << endl;
//Deallocate dynamic memory
delete [] file;
delete [] text_line;
return 0;
}
Program execution example using terminal manually:
$ cd Desktop/Other/Test
$ g++ main.cpp
$ ./a.out
File: test.txt is open!
Hello World!
$
Program execution example double clicking the same executable:
$/Users/vladimirmeshcheryakov/Desktop/Other/Test/a.out ; exit;
Failed to open: test.txt
logout
[Process completed]
one of the possible things to cause it could be the case of running the terminal as superuser, in a folder with access restriction to the regular user. (superuser doesn't have that restriction)
solution: give current user the right to Read/Write in this folder.
Now I need to find a solution of obtaining the path to executable.
Check whether argv[0] contains it.

File fails to open with fstream C++ on Mac OSX

Alright, so I could have sworn this worked in my program earlier, but now I'm being driven mad by std::fstream. I just want to open a file from command line arguments, ie.
./main Program1.S
should open the file Program1.S and scan it.
Here is how I set up a open_file() function in my code:
#include <string>
#include <iostream>
#include <fstream>
void open_file(std::fstream &ifp, std::string file_name) {
ifp.open(file_name, std::ios::in | std::ios::out);
if(ifp.fail()) {
std::cout << "File not found." << std::endl;
exit(1);
}
}
void close_file(std::fstream &ofp) {
if(ofp.is_open()) {
ofp.close();
return;
}
std::cout << "This file is not currently open" << std::endl;
}
int main(int argc, char * argv[]) {
std::string in_name;
in_name = argv[1];
std::fstream ifp;
open_file(ifp, in_name);
// do some processing
close_file(ifp);
return 0;
}
Now, I compile my program using (unfortunately I am required to use c++03): g++ -g -std=c++03 -Wall -pedantic main.cpp -o main
Compilation works and provides no errors, but when running the program using: ./main Program1.S, it goes to File not found in open_file(). I even checked what was in argv[1] and it is definitely a file that is in the current working directory. Is there something wrong with the way I am doing this?
Check to make sure your file has been added to your project folder. Otherwise, you need to specify a file path within your computer ex. "/Mac HD/Documents/myfile". The program has no idea what to do with a external file name without its file path. Hope this helps.

Why does this code fail to read Blender's .obj file?

Hi I am trying to read a Wavefront file which was created using Blender. I put a copy of this file into the solution Explorer. When I tried to compile for the first time I got the following message:
fatal error LNK1107: invalid or corrupt file: cannot read at 0x...
It seemed like the compiler confused Blender's .obj files with some other format which also uses the .obj ending. The solution was to exclude the file from the build process in its properties.
Now the application does compile but there is no data displayed like I would expect it. Not sure if this is a code issue.
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
void ReadPrintFile(string _fileName)
{
std::string line;
std::ifstream fileStream (_fileName);
if (fileStream.is_open())
{
while (getline(fileStream,line))
{
cout << line << '\n';
}
fileStream.close();
}
else
{
cout << "Unable to read file";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
ReadPrintFile("Drone.obj");
std::cin.get();
return 0;
}
The code does not jump into the else statement. The filestream simply seems to be empty and I am directly forwarded to the cin.get(); statement. I know that there are tons of tutorials on how to parse .OBJ in C++ but I want to understand.
The trick was not to copy the file into the solution explorer but into the project folder.