I am using MFC CFile.
In windows OS in my application I am locking the file by opening the file with share deny access.
In the same application or process I have to check whether the file is locked or not?
Right now, the only way I know is opening the file and checking GetLastError().
Is there any other solution?
Try to open the file first. If the file exists and is already open without share access, you get a sharing violation
CFileException::sharingViolation.
See CFileException for possible values of CFileException::m_cause
Example:
UINT open_flag = CFile::modeReadWrite | CFile::modeCreate | CFile::modeNoTruncate;
CFileException ex;
if(file.Open(filename, open_flag, &ex))
{
//success
}
else
{
if(ex.m_cause == CFileException::fileNotFound)
{
//file doesn't exit
}
else if(ex.m_cause == CFileException::sharingViolation)
{
//file exists and is locked
}
}
while reading a data from csv file, I got also same errors
In my case,
existed csv file with the same name was open. So, I closed that csv file and it was Okay.
later was the same issue again.
this time, I wrote inappropriate mode while creating
so I changed this code
file.Open(fileName, CFile::modeCreate | CFile::modeReadWrite)
to
file.Open(fileName, CFile::modeCreate | CFile::modeWrite)
Related
I have a function that i'm running on my windows against case sensitive remote server (SMB).
The function creates two files: "fileA", "FILEa".
When i'm trying to unlink the file "fileA" both of my files disappear from the folder.
Right after that i'm calling for unlink file "FILEa",and get error that the file does not exist.
My code:
void my_function(Path path)
{
Path filename1 = path;
filename1 + "_fileA";
Path filename2 = path;
filename2 +"_FILEa";
close(open(filename1, O_CREAT | O_EXCL, S_IREAD | S_IWRITE));
try
{
close(open(filename2, O_CREAT | O_EXCL, S_IREAD | S_IWRITE));
}
catch (Exception e)
{
unlink(filename1);
return;
}
unlink(filename1);
unlink(filename2);
}
On wireshark , after Client's unlink , the Client send SMB Find request to query the content of the folder and the server return FILEa in the list.
wireshark capture
It seems that windows has internal caching that handles Opens and fileA and FILEa are mapped to the same entry.
Edit:
View of my folder after creating the two files:
I'm using Windows and the code is in cpp.
Somebody knows why is this happening to me?
Or if there is other function beside unlink that could remove only the chosen file and not both of them?
Thank you.
This question already has an answer here:
Writting in text file error, QT
(1 answer)
Closed 4 years ago.
i have the following problem, i have an resource file called data.txt and i want to open it with write permission.
Im using QFile and QTextStream to work with it.
I can only open the File with ReadOnly Acces but not with ReadWrite or WriteOnly acces.
Export functions with similar code working fine its only not working on the resource file.
I already tried to change the front slashes to double backslashes, i runned hunderd times the qmake and rebuild, i restartet my computer and resotre the resource file.
I already checked alot of entries on Stack but wasnt able to find one that resolves my problem. (Most questions were spelling issues like only one backslash).
QFile file(":/savelocation/data.txt");
if (!file.exists())
{
qDebug()<<"File not exist";
}
file.open(QIODevice::ReadWrite | QIODevice::Text);
if (file.isOpen())
{
qDebug()<<"File is open";
QTextStream out(&file);
out<< "something" << endl;
}
else
{
qDebug()<<"File is not open";
}
file.close();
file.open(QIODevice::ReadOnly);
if (file.isOpen())
{
qDebug()<<"File is open as read only";
}
else
{
qDebug()<<"File is not open as read only";
}
file.close();
Actual result:
My Application output of the code:
File is not open
File is open as read only
Its only possible to open it as ReadOnly for me.
Before i implemented the if i got the following output:
QIODevice::write (QFile,":/savelocation/data.txt"): device not open
Expected result:
The file would be opened with write access.
Thanks in advance.
As per the documentation, resources are embedded in your binary file and are thus read-only, both conceptually and practically.
You will have to save your data to a writable location on the filesystem, for example under QDir::home() or the current working directory QDir::current() .
CFile file;
CFileException fe;
if (file.Open(strPath, CFile::modeCreate | CFile::modeReadWrite | CFile::typeBinary, &fe) )
{
}
This doesn't work. The file path provided by strPath already exist but it will not open this file and returns with error code 5. If I do remove the CFile::modeCreateflag than the file is opened fine. The documentation says if the file already exists 'CFile::modeCreate' will attach itself to it and truncate it's to zero but in reality it never opens the file in the first place. Is there an issue with my call?
I resolved this. The problem was that the file had 'ready only' attribute set. I changed it to normal before I open it and that fixed it.
SetFileAttributes(strPath, FILE_ATTRIBUTE_NORMAL);
I have a question about FILE_ATTRIBUTE_TEMPORARY marked files.
First of all, here is what I want to do:
I have a DLL, that takes a Filename, and opens that file internally and reads from it. I do not know how this file is handled inside.
The file I want to give to that DLL will be created by my process. It must be a temporary file and its data must be held only in RAM and must not be accessed by other processes. So I use the Win32 function CreateFile() with the FILE_ATTRIBUTE_TEMPORARY and the FILE_FLAG_DELETE_ON_CLOSE. This so far works, fine.
I have a tes code where I test if the file can be accessed a second time, while still opened. Here it is:
HANDLE WINHandle = CreateFile("TempFileWIN.txt", (GENERIC_WRITE | GENERIC_READ) ,(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE), 0, CREATE_ALWAYS, (FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE), 0);
ifstream ifs("TempFileWIN.txt", (ios::in | ios::trunc));
if(ifs.is_open())
{
cout << "Success!" << endl;
}
else if(ifs.fail())
{
cout << "Failed!" << endl;
}
I am using the fstream to test if the file could be opened with a stream. That code up there doesn't work. The output is "Failed!".
I know, that the file could be opened with the CreateFile a second time. I checked that out. But want to know if it is possible to open the file by an external DLL that works with (e.g.) a fstream.
I hope you can help me with this matter.
Best regards.
Edit:
Maybe a better question is how I can lock a file to my process and ensure, that it can never be accessed by an other process (even if my process is killed). The file must be openable with C++ fstream object.
If I were you, I would keep the handle of the open file, and pass it to the DLL code, and not use the filename, since you're likely to run into access restrictions at some point if you try to access a temporary, delete-on-close file using 'normal' file access.
It is possible to use a Windows handle in a fstream object as described in this answer: https://stackoverflow.com/a/476014/393701
I'm trying to open /usr/share/dict/words with the following code:
fstream f;
f.open("/usr/share/dict/words");
// why is this returning false?
bool open = f.is_open();
I'm wondering why f.is_open() is returning false?
More info: when I try a smaller test file containing on the order of 20 lines f.is_open() returns true. Perhaps f.open is trying to load the entire file into memory?
It does not work because you are opening the file for reading and writing. Unless you are running as root, you do not have permissions to write to this file.
If you open it just for reading it will work:
f.open("/usr/share/dict/words", fstream::in);
The fstream.open() function is declared thusly:
void open (const char *filename,
ios_base::openmode mode = ios_base::in | ios_base::out );
i.e. it opens the file for reading and writing. Your process probably does not have permissions to open that file for writing unless you're running as root. Open it for reading only with
f.open("/usr/share/dict/words", ios_base::in);