Why doesn't ofstream create a new file? - c++

I have a problem where ofstream isn't creating a text file. I compile and run the program just fine with no errors and I am 100 percent certain that I am in the right directory and that there are no spelling mistakes or errors. What is strange is when I move my program to my C drive or onedrive folder, then the text file is created, it just doesn't work in any of my other folders. I don't know what the problem is and I have scoured the internet for the past couple hours trying to find a solution, but I think it has something to do with my file permissions perhaps?
Here is my code, it's just a really simple program:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream myfile;
myfile.open("newnumbers.txt");
}

Related

Cannot create a file/write to it, but can read an existing one in c++

I was just learning C++ file handling, but ran into an error immediately using this simple code to create a new file:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file("test.txt");
file << "test";
file.close();
}
File doesn't show up. When I use .is_open(), it always gives 0. Program does compile though.
Then I manually created a .txt file with some text and tried to read it, and it worked. I supposed it was permission thingy, but it seems like all files are available to be changed? I'm not sure I completely understand how to check the permission though (I'm a bit new to all of this...), so please do help with it as well!
I use Atom and its terminal, my compiler is MingW, but I guess it might be a bit too old.
I tried to include the whole path, but it didn't work.
Thank you!
EDIT:
Just tried this code:
ofstream file;
file.open("C:\\Users\\username\\Desktop\\my_folder\\test.txt");
file << "test";
file.close();
Doesn't help. .is_open() gives 0 before I even try to write to a file.
EDIT:
Just tried this code:
fstream fileW;
fileW.open("write.txt", ios_base::in | ios_base::out | ios_base::trunc);
cout<<fileW.fail()<<endl;
cout<<fileW.is_open()<<endl;
fileW<<"Edit";
fileW.close();
Still doesn't work, returns 1 for .fail().
EDIT:
The error is "Permission denied".
EDIT:
Solved! I deleted Avast, it was blocking my program from accessing files.
Avast, I hate you. You are the worst.
Use
ofstream file("test.txt");
Your version does not create the file if it does not exist.
Similarly for input you should really use ifstream. fstream is best reserved for files you want to read and write from.

How do I create a directory inside the directory where my .cpp file is placed?

I am creating a directory in the desktop folder using this code.
#include <iostream>
#include <stdio.h>
#include <direct.h>
#include<string>
using namespace std;
int main()
{
string k,s="c:/Users/Dell/Desktop/";
cin>>k;
string p =s+k;
if (mkdir(p.c_str()) != 0)
{
cerr<<"Failed"<<endl;
}
return 0;
}
But this code will be specific to my pc only as it has the whole parent directory path.
I want to create a new folder inside my working directory where the main.cpp is placed, because we don't know where the user will place the cpp file.
I know there are a lot of solutions similar to this problem on the forum but I couldn't find or understand any that could help me here. Thanks.
Do you really mean the .cpp file?
You can get the filename as a string using the __FILE__ macro and then use std::filesystem::path to manipulate it, but are you sure this is really what you want to do? Are you expecting end users to compile this program in the location they want to run it?
You can also (in theory) get the path through which the program was executed using the argv argument to main, which seems more likely to be what you mean, but it's unreliable in a number of surprising ways ...

So...is there a way to stop files from clearing automatically? (c++)

So i'm making an extremely simple guessing console game and i want to store data permanently in a file (highscore). However everytime i compile the file i'm using empties itself. Is there anyway to stop that?
I've tried a lot of thing which didn't work and i honestly don't know where the problem is. I'm guessing it has to do with the fin and fout but for others it seemed to work
#include <iostream>
#include <fstream>
#include <time.h>
#include <conio.h>
int hs;
//this would be the play_game() function, unrelated to the subject
int main()
{
std::ofstream fout;
fout.open("HS.txt");
std::ifstream fin;
fin.open("HS.txt");
srand(time(NULL));
//menu with 4 options, play, quit, help and highscore (which i'm working on)
fin.close();
fout.close();
}
Don't open your file twice in parallel with two streams. Also, a simple open-for-writing will not truncate your file, but will place you at the start of the file, so you'll be overwriting existing data; see this question. You have to open files with the write mode.
You need to either:
Open your file for both input and output - and without truncating it; see: What does it mean to open an output file as both input and output? , or
Open your file for reading only when your app starts, and open it for writing, and write to it, when it exists (or every once-in-a-while for better resiliency).

My c++ codes doesn't create or edit text file

I searched this problem on web and I found some solutions but they didn't solve. So I must ask to someone. My problem is about c++, I am writing normal code but any file operations command doesn't work, they don't create or change text files. After It work, doesn't create a text file on same directory I tried to change my ide, I tried to run as Admin, I tried to open show secret folders, I tried to change compiler's bit, I tried to run on D:, I turned off my anti-virus app but they didn't solve. I added system("dir") the end of the codes and it showed to me the text file(i couldn't see in the folder again) but after this, I went to same directory with cmd(as admin) but this time i couldn't see the text. I couldn't remember what i've tried more. Also I have some problems about permissions i don't know maybe this about the my problem. Here is a part of my codes;
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
void adding() {
string userName;
cout << "Enter your nickname: ";
cin >> userName;
ofstream userFile;
userFile.open("users.txt", ios::app);
userFile<< userName;
userFile.close();
cout<<"The user is succesfully added!"<<endl;
}
int main()
{
cout<<system("cd");
adding();
system("dir");
}

unable to open file stream c++

I am working with Xcode and I am having trouble opening a file stream to assign variables from a text file. I speculate that placing the txt file in the same directory as the project would allow me open the stream without including the entire dir. I have been messing with this for a little while to no avail can I get it to work properly. I believe I was able to read data at one point, but I think the string printed was in unicode (not sure). It is a very simple program.. I would think that it would work.
I think my problem has to do with the directory the example is in and how Xcode works with project files. I just put the example file in the project folder and hoped that it would work.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string name;
ifstream infile;
infile.open("example.txt");
if(infile.is_open())
{
infile >> name;
}
else
cout << "Unable to open file";
cout << name;
return 0;
}
First of all, remember, that working directory is not always the same directory where the program's binary resides.
Change:
infile.open("example.txt");
to:
infile.open("/full/path/to/program/directory/example.txt");
where /full/path/to/program/directory/ is the location of folder, where program (and thus example.txt file) is placed. It should fix the issue.
By the way, you may also want to read this question, that addresses very similar problem.
Also, read about getcwd() function.