Errno 13 when trying to open file c++ - c++

I've been working on this for a while, and I'm out of ideas. I've been trying to open and write to a file, but nothing's happening. I've been getting errno 13, but I have full permissions in the directory the file is in. If anyone has any ideas what's going on, I'd appreciate it.
Here's my code:
#include <fstream>
#include <iostream>
int main () {
std::ofstream myfile;
myfile.open ("example.txt", std::ios::out);
std::cout << errno << std::endl;
myfile << "test";
myfile.close ();
return 0;
}
I've also tried using fopen, but I didn't have any luck with that either.

Not sure what the deal was, but I got it working by moving it to a different directory. Guess it was a permissions error, but for some reason windows was saying I had permission to write to the directory.

Related

std::ofstream does not show error when permission denied C++

The following code when path = "c:\" doesn't write to file c:\err.txt because permission is denied. But it doesn't generate an error at the same time. Rather, it outputs "OK".
How I can check whether the permissions would allow the write?
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
bool writeLineToErr(string path, string err_line){
std::ofstream outfile(path+"err.txt", std::ios_base::app);
if(!outfile){
cout<<"Error 1 "+path+"err.txt"+" can't open file!";
return false;
}
if(outfile.fail()){
cout<<"Error 2 "+path+"err.txt"+" can't open file!";
return false;
}
outfile << err_line << endl;
cout<<"OK";
outfile.close();
return true;
}
int main(int argc, char** argv) {
writeLineToErr("c:\\","Some Line");
return 0;
}
I'd say your code works and the write operation is actually done, but for the sake of it, add a check after the write too:
outfile << err_line << endl;
if(outfile.fail()) cout << "Error3\n";
else cout<<"OK";
On my system, I'll get your Error 1 ... can't open file if the file isn't opened for writing successfully.
Edit: Or are you running Windows with Compatibility Files virtualization still active? If so, the file will probably be in the Virtual Store, not in the real C:\err.txt path.
Example: C:\Users\username\AppData\Local\VirtualStore
If you find it there, you may find a lot of other stuff in there too. At least I did years ago when I had a similar problem. I decided to manually move (with admin rights) the few important files that some of my older programs had put there and then turn Virtual Store off. I can't find a good and simple official Microsoft link for how to turn off file and registry virtualization right now so perhaps this will do:
RegEdit:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\
Create a DWORD Key with the name EnableVirtualization and give it the value 0. If the key is already there, but set to something else than zero, change it.
There's more here:
UAC Group Policy Settings and Registry Key Settings

Copy a certificate file to another location

I have a .crl file which I want to copy to another location. From all the posts which I have seen till now, it can't be done without copying the contents. Is there any method in which I can transfer the file to another location in cpp without copying the contents?
I tried by copying the contents by using the usual fopen method. But the data was not being written to the buffer . If there is no direct method, could anyone please tell me how to read the certificate file and write the contents to another file in a different location?
I have also tried the fstream methods
std::ofstream dest("destination.crl", std::ios::trunc|std::ios::binary);
if(!dest.good())
{ std::cerr << "error opening output file\n";
//std::exit(2);
}
std::fstream src("sourcec.crl", std::ios::binary);
if(!src.good())
{ std::cerr << "error opening input file\n";
//std::exit(1);
}
dest << src.rdbuf();
if(!src.eof()) std::cerr << "reading from file failed\n";
if(!dest.good()) std::cerr << "writing to file failed\n";
But it displayed the errors:
error opening input file
reading from file failed
writing to file failed
Thank you in advance
I found this here.
See, this might help you. As you do not want to read, I thought you do not want to perform read and write operation yourself.
#include <istream>
#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;
int main()
{
fstream f("source.crl", fstream::in|fstream::binary);
f << noskipws;
istream_iterator<unsigned char> begin(f);
istream_iterator<unsigned char> end;
fstream f2("destination.crl",
fstream::out|fstream::trunc|fstream::binary);
ostream_iterator<char> begin2(f2);
copy(begin, end, begin2);
}
Based on your response, I write another answer.
For Window, there is function [CopyFile][1]. You can use this function to copy the file without reading the content by yourself.
For linux/unix based, I am unable to find the direct equivalent of CopyFile.
I believe that this question might help you.

Cannot read text file in C++

I am stumped by this simple problem. I am reading a text file with C++:
std::ifstream stream;
stream.open(filename);
if (!stream)
cout << "Invalid stream" << endl;
And !stream is true but there seems to be nothing wrong with the text file. Under what circumstances can stream be false?
Note: is_open returns true
You have not provided enough information. Nevertheless, my psychic powers reveal:
filename is a relative path, and your current working directory is not what you think it is.
Inside your if clause, before printing via std::cout, add this:
perror(filename.c_str());
This code works for me:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
std::ifstream infilestream;
std::string line;
infilestream.open("test.txt");
while(infilestream)
{
std::getline(infilestream, line);
cout<<line<<"\n";
}
infilestream.close();
return(0);
}
chances are your file is inaccessible OR you might not have enough privileges to access the file.
Maybe its open somewhere else? Check if the path to the file is right.

Problems using .open with ifsteam objects

The best way to explain my problem is probably just to show you my code, because it's as simple as it gets.
#include <iostream>
#include <fstream>
int main (int argc, const char * argv[])
{
std::ifstream in;
std::string line;
in.open("test.txt");
if (in.fail()) std::cout << "failed. \n";
getline(in, line);
std::cout << line;
return 0;
}
So when I run this, console is returning "failed." instead of opening up the file called test.txt — which is in the same folder as my .xcodeproj file and is also displayed in my Xcode navigator.
I'm not sure what I'm misunderstanding about this process, but I suspect it will be something simple.
Thanks! :)
The file is in the same directory as your .xcodeproj file? Well, there's your problem right here.
By default, the working directory of a process launched from Xcode will be the output directory (that is, the directory where the program is). Depending on your Xcode version, it's probably going to be in <Project Directory>/build/Debug.
Try moving the file there.

Unable to write file in C++

I'm trying to to the most basic of things .... write a file in C++, but the file is not being written. I don't get any errors either. Maybe I'm missing something obvious ... or what?
I thought there was something wrong with my code, but I also tried a sample I found on the net and still no file is created.
This is the code:
ofstream myfile;
myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt");
myfile << "Writing this to a file.\n";
myfile.close();
I've also tried creating the file manually beforehand, but it's not updated at all.
I'm running Windows 7 64bit if that has got something to do with this. It's like file-write operations are completely forbidden and no error messages or exceptions are shown.
You need to open the file in write mode:
myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt", ios::out);
Make sure to look at the other options for that second argument, as well. If you're writing binary data you'll need ios::binary for example.
You should also be checking the stream after opening it:
myfile.open(...
if (myfile.is_open())
...
Update:
AraK is right, I forgot that an ofstream is in write mode by default, so that's not the problem.
Perhaps you simply don't have write/create permissions to the directory? Win7 defaults a lot of directories with special permissions of "deny all". Or perhaps that file already exists and is read-only?
Start off by turning that slash around.
Even Windows understands the slash being the other way around.
ofstream myfile("C:/Users/Thorgeir/Documents/test.txt");
You could test if there are any errors:
if (!myfile)
{
std::cout << "Somthing failed while opening the file\n";
}
else
{
myfile << "Writing this to a file.\n";
myfile.close();
}
Make sure the directory exists.
If the file exists make sure it is writeable (by you)
Check the directory you are writing into is writeable (by you)
Have you read about UAC (User Account Control) and UAC Virtualization / Data Redirection in Windows Vista and 7? It's possible that your file is actually in the Virtual Store.
User Account Control Data Redirection
Your example output directory is in Users, so I wouldn't think this would be the issue, but it's a possibility worth mentioning and something that can be very frustrating if you're not looking out for it!
Hope this helps.
This code should catch any error. Most likely it's a permissions thing if any errors are encountered. Make sure you can read/write to the folder you're creating the file in.
#include "stdafx.h"
#include <fstream>
#include <iostream>
bool CheckStreamErrorBits(const std::ofstream& ofile);
int _tmain(int argc, _TCHAR* argv[]) {
std::ofstream ofile("c:\\test.txt");
if(ofile.is_open()) {
CheckStreamErrorBits(ofile);
ofile << "this is a test" << std::endl;
if(CheckStreamErrorBits(ofile)) {
std::cout << "successfully wrote file" << std::endl;
}
}else {
CheckStreamErrorBits(ofile);
std::cerr << "failed to open file" << std::endl;
}
ofile.close();
return 0;
}
//return true if stream is ok. return false if stream has error.
bool CheckStreamErrorBits(const std::ofstream& ofile) {
bool bError=false;
if(ofile.bad()) {
std::cerr << "error in file stream, the bad bit is set" << std::endl;
bError=true;
}else if(ofile.fail()) {
std::cerr << "error in file stream, the fail bit is set" << std::endl;
bError=true;
}else if(ofile.eof()) {
std::cerr << "error in file stream, the eof bit is set" << std::endl;
bError=true;
}
return !bError;
}
Update:
I just test my code under Windows 7 Enterprize and it failed the first time (fail bit was set). Then I turn off User Account Control (UAC) and tested again and it wrote the file. That is probably the same problem you're seeing. To turn off UAC go to:
Control Panel (view by Small icons) | User Accounts | Change User Account Control settings. Set it to Never notify then click OK button. You will have to restart for the changes to take affect.
I'm curious how to make it work with UAC on, i'll look into that.
Try this:
if( ! myfile)
{
cerr << "You have failed to open the file\n";
//find the error code and look up what it means.
}
Use FileMon and look for failed WriteFile calls from your process.