Is there a way to open a file in C++ and see only the context of the file from the state the file was in when it was opened (so if the file was modified while it was opened, the changes won't be visible)?
For example:
#include <iostream>
#include <fstream>
#define CHUNK 100000
int main()
{
std::ifstream m_istr("a.txt", std::ios::binary);
if(!m_istr.is_open())
{
std::cout << "NOT OPENED!" << std::endl;
return -1;
}
auto buffer = new uint8_t[CHUNK];
system("#echo lol >> a.txt");
m_istr.read((char*)buffer, CHUNK);
std::cout << buffer << std::endl;
}
I do not want to see the string lol appended to my buffer.
I can make an exclusive tmp copy file, open it and after I am done delete it, but I am wandering if there is a cleaner solution.
Yes, there is a cleaner solution. But it is OS specific.
For example, in Windows you can open file with exclusive access rights. Something like this:
For example: HANDLE hFile = ::CreateFile(lpszFileFullPathName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
Related
Newb here. I have spent the last 4 hours trying to solve this problem.
ifstream is suddenly not opening files.
ofstream has no problems writing to the file.
The file exists, it's contents are, ThisIsText, and it is in the reference directory, which I confirmed with system("dir & pause")
I tried Code::Blocks and Dev C++, but I think they're using the same compiler(GNU GCC Compiler).
I tried using the full filename path with double backslashes.
I see people mentioning permissions, but I don't know how to tinker with that.
I'm on Windows 10.
Edit: I just found a new compiler(Embarcadero 10.1 AKA Borland) and the code works with it. I still want to know what the problem is with GNU GCC
The following code skips to the else statement:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string message;
ifstream inputFile;
inputFile.open("File.txt");
if (inputFile.good())
{
inputFile >> message;
cout << message;
system("dir & pause");
}
else
{
cout << "failed to open input file\n";
system("dir & pause");
} return 0;
}
If it helps, I found the following code online and it outputs, "Error code = 2"
from winerror.h, that is: ERROR_FILE_NOT_FOUND
#include <windows.h>
#include <iostream>
int main()
{
HANDLE hFile = CreateFile(
"one.txt", // Windows does not case about case
GENERIC_READ,
0, // no sharing
NULL, // default security
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL ); // no file template
if(INVALID_HANDLE_VALUE == hFile)
{
DWORD errCode = GetLastError(); // see winerror.h for meanings
std::cout << "File wouldn't open :-(" << std::endl;
std::cout << "Error code = " << errCode << std::endl;
}
else
{
std::cout << "File opened OK :-)" << std::endl;
CloseHandle(hFile);
}
return 0;
}
PS. I know using namespace std; is bad practice
I have opened a file using CreateFile fn and tried to print data into the file. Since the data includes some print statements like
wprintf(L"Channel %s was not found.\n", pwsPath);
The declaration for DATA and pwsPath
#include <iostream>
#include <sstream>
using namespace std;
string data;
LPWSTR pwsPath = L"Channel1";
I tried to use stringstream to get the data and convert it to a LPCVOID to use the WriteFile fn as shown
hFile1 = CreateFile(L"MyFile.txt", // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
std::stringstream ss;
ss << "Channel" << pwsPath << "was not found.";
ss >> data;
cout << data; // data contains value only till the first space i.e Channel093902
cin>>data;
bErrorFlag = WriteFile(
hFile1, // open file handle
data.c_str(), // start of data to write
dwBytesToWrite, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL);
Is it possible for variable data to include spaces from the stringstream ??
OR
Is there any other way other than stringstream to get the data from such print statements and write to the file as such?
The >> operator will deliver the next 'word' in the stream into the string object you have given it. It breaks at the first white space as you have found. There are a couple of ways to achieve what you want. The most conformant is to open the output file as an ofstream:
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
std::string pwsPath { "[enter path here]" };
std::stringstream ss;
ss << "Channel " << pwsPath << " was not found.";
std::ofstream outFile("myFile.txt");
outFile << ss.rdbuf();
outFile.close();
std::ifstream inFile("myFile.txt");
cout << inFile.rdbuf();
return 0;
}
otherwise you can get the internal string from the ostringstream:
std::string myData = ss.str();
size_t dataLength = myData.length();
DWORD dwBytesWritten = 0;
BOOL bErrorFlag = WriteFile(
hFile1, // open file handle
myData.data(), // start of data to write
DWORD(dataLength), // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL);
Unless you have a good reason to use CreateFile and WriteFile, consider using std objects all the way.
Your code could look like this:
#include <iostream>
#include <fstream> // add this
#include <sstream> // remove this unless used elsewhere
// your pwsPath
std::wstring path{ L"Channel1" };
std::wofstream out{ L"MyFile.txt", std::wofstream::trunc };
// skip the stringstream completely
out << "Channel " << path << " was not found."
This creates the file but it doesn't write anything.
std::ofstream outstream;
FILE * outfile;
outfile = fopen("/usr7/cs/test_file.txt", "w");
__gnu_cxx::stdio_filebuf<char> filebuf(outfile, std::ios::out);
outstream.std::ios::rdbuf(&filebuf);
outstream << "some data";
outstream.close();
fclose(outfile);
I know there are other easy solutions to achieve the output, but i need to use this non-standard filebuf to lock a file while editing, so that other process can't open the file.
I don't know why this is not working.
std::ostream already has a constructor doing the right thing:
#include <ext/stdio_filebuf.h>
#include <iostream>
#include <fcntl.h>
int main() {
auto file = fopen("test.txt", "w");
__gnu_cxx::stdio_filebuf<char> sourcebuf(file, std::ios::out);
std::ostream out(&sourcebuf);
out << "Writing to fd " << sourcebuf.fd() << std::endl;
}
Remember that stdio_filebuf won't close the FILE* when it is destroyed, so remember to do that yourself if you need it.
I want to input some contents to a file, but I'd like to check first if a file with the name I wish to create exists. If so, I don't want to create any file, even if the file is empty.
My attempt
bool CreateFile(char name[], char content[]){
std::ofstream file(name);
if(file){
std::cout << "This account already exists" << std::endl;
return false;
}
file << content;
file.close();
return true;
}
Is there any way to do what I want?
Assuming it is OK that the operation is not atomic, you can do:
if (std::ifstream(name))
{
std::cout << "File already exists" << std::endl;
return false;
}
std::ofstream file(name);
if (!file)
{
std::cout << "File could not be created" << std::endl;
return false;
}
...
Note that this doesn't work if you run multiple threads trying to create the same file, and certainly will not prevent a second process from "interfering" with the file creation because you have TOCTUI problems. [We first check if the file exists, and then create it - but someone else could have created it in between the check and the creation - if that's critical, you will need to do something else, which isn't portable].
A further problem is if you have permissions such as the file is not readable (so we can't open it for read) but is writeable, it will overwrite the file.
In MOST cases, neither of these things matter, because all you care about is telling someone that "you already have a file like that" (or something like that) in a "best effort" approach.
you can also use Boost.
boost::filesystem::exists( filename );
it works for files and folders.
And you will have an implementation close to something ready for C++14 in which filesystem should be part of the STL (see here).
Try
ifstream my_file("test.txt");
if (my_file)
{
// do stuff
}
From: How to check if a file exists and is readable in C++?
or you could use boost functions.
Try this (copied-ish from Erik Garrison: https://stackoverflow.com/a/3071528/575530)
#include <sys/stat.h>
bool FileExists(char* filename)
{
struct stat fileInfo;
return stat(filename, &fileInfo) == 0;
}
stat returns 0 if the file exists and -1 if not.
As of C++17 there is:
if (std::filesystem::exists(pathname)) {
...
Looked around a bit, and the only thing I find is using the open system call. It is the only function I found that allows you to create a file in a way that will fail if it already exists
#include <fcntl.h>
#include <errno.h>
int fd=open(filename, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd < 0) {
/* file exists or otherwise uncreatable
you might want to check errno*/
}else {
/* File is open to writing */
}
Note that you have to give permissions since you are creating a file.
This also removes any race conditions there might be
I just saw this test:
bool getFileExists(const TCHAR *file)
{
return (GetFileAttributes(file) != 0xFFFFFFFF);
}
C++17, cross-platform: Using std::filesystem::exists and std::filesystem::is_regular_file.
#include <filesystem> // C++17
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
bool CreateFile(const fs::path& filePath, const std::string& content)
{
try
{
if (fs::exists(filePath))
{
std::cout << filePath << " already exists.";
return false;
}
if (!fs::is_regular_file(filePath))
{
std::cout << filePath << " is not a regular file.";
return false;
}
}
catch (std::exception& e)
{
std::cerr << __func__ << ": An error occurred: " << e.what();
return false;
}
std::ofstream file(filePath);
file << content;
return true;
}
int main()
{
if (CreateFile("path/to/the/file.ext", "Content of the file"))
{
// Your business logic.
}
}
The easiest way to do this is using ios :: noreplace.
I realize that ofstream doesn't work on Windows 7 hidden file.
Here is the quick test code.
#include <fstream>
#include <iostream>
#include <tchar.h>
#include <windows.h>
int main() {
{
std::ifstream file2(_T("c:\\a.txt"));
if (file2.is_open()) {
std::cout << "ifstream open" << std::endl;
} else {
std::cout << "ifstream not open!" << std::endl;
}
}
// SetFileAttributes(_T("c:\\a.txt"), FILE_ATTRIBUTE_NORMAL);
SetFileAttributes(_T("c:\\a.txt"), FILE_ATTRIBUTE_HIDDEN);
{
std::ofstream file(_T("c:\\a.txt"));
if (file.is_open()) {
std::cout << "ofstream open" << std::endl;
} else {
std::cout << "ofstream not open!" << std::endl;
}
}
getchar();
}
Here is the output I am getting
ifstream open
ofstream not open!
If I am using FILE_ATTRIBUTE_NORMAL, ofstream will be opened successfully.
I do not run the program as Administrator. But, I do use the following linker option.
Having to turn No for Enable User Account Control (UAC) is important, if we do not start the application as Administrator. OS will help us to write the actual file to C:\Users\yccheok\AppData\Local\VirtualStore\a.txt instead of protected C:\
Does ofstream fail on Windows 7 hidden file, is an expected behaviour?
Yes. As noted in the underlying CreateFile documentation, " If CREATE_ALWAYS and FILE_ATTRIBUTE_NORMAL are specified, CreateFile fails and sets the last error to ERROR_ACCESS_DENIED if the file exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM attribute."
Or more readable: CreateFile fails if both CREATE_ALWAYS and FILE_ATTRIBUTE_NORMAL are specified, and if the file has the FILE_ATTRIBUTE_HIDDEN and/or FILE_ATTRIBUTE_SYSTEM attribute.
It just so happens that ofstream calls CreateFile like this.