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.
Related
I'm currently new to C++ and I've been watching a tutorial series https://www.youtube.com/watch?v=_bYFu9mBnr4, but I'm having a big issue. My C++ code will not open a file no matter what I do, I've looked online and tried renaming it, the full path, everything I can think of. Here's my code,
#include <iostream>
#include <fstream>
#include <cstring>
#include <cerrno>
#include <filesystem>
int main()
{
std::ofstream file;
file.open("hello.txt");
if (!file.is_open())
{
std::cerr << "Error: " << strerror(errno) << '\n';
std::cout << std::filesystem::current_path() << std::endl;
}
file << "hello!";
file.close();
return 0;
}
Sorry about this question, it may have been a dumb issue. Turns out IT WAS my antivirus. Avast kept blocking it, it was just looking out for me. I decided to change my antivirus afterwards and it now works fine!
i am trying to open file for read/write purpose. Below code is not working ?
can anyone explain me where i m doing wrong?
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
ifstream file;
file.open("program.txt");
if (!file)
{
cout << "failure";
}
return 0;
}
the output of above program is "failure".
but why?
isn't it supposed to open file sucessfully?
If you are using linux / macos. try this code, it will show you the reason of failure.
#include <iostream>
#include <fstream>
#include <stdio.h>
int main()
{
using namespace std;
ifstream file;
file.open("program.txt");
if (!file)
{
perror("open failure");
}
return 0;
}
I guess the reason is "No such file or directory".
Maybe you can try to switch your "current directory" to find the file.
I got the same issue and did some research and not find the cause.
later I tried it using absolute path for the file and find it works.
file.open("program.txt");
------> should be
file.open("/absolute path/program.txt");
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.
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.
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.