I've got a problem with deleting/overwriting a file using my program which is also being used(read) by my program. The problem seems to be that because of the fact my program is reading data from the file (output.txt) it puts the file in a 'in use' state which makes it impossible to delete or overwrite the file.
I don't understand why the file stays 'in use' because I close the file after use with fclose();
this is my code:
bool bBool = true
while(bBool){
//Run myprogram.exe tot generate (a new) output.txt
//Create file pointer and open file
FILE* pInputFile = NULL;
pInputFile = fopen("output.txt", "r");
//
//then I do some reading using fscanf()
//
//And when I'm done reading I close the file using fclose()
fclose(pInputFile);
//The next step is deleting the output.txt
if( remove( "output.txt" ) == -1 ){
//ERROR
}else{
//Succesfull
}
}
I use fclose() to close the file but the file remains in use by my program until my program is totally shut down.
What is the solution to free the file so it can be deleted/overwrited?
In reality my code isn't a loop without an end ; )
Thanks in advance!
Marco
Update
Like ask a part of my code which also generates the file 'in use'. This is not a loop and this function is being called from the main();
Here is a piece of code:
int iShapeNr = 0;
void firstRun()
{
//Run program that generates output.txt
runProgram();
//Open Shape data file
FILE* pInputFile = NULL;
int iNumber = 0;
pInputFile = fopen("output.txt", "r");
//Put all orientations of al detected shapes in an array
int iShapeNr = 0;
int iRotationBuffer[1024];//1024 is maximum detectable shapes, can be changed in RoboRealm
int iXMinBuffer[1024];
int iXMaxBuffer[1024];
int iYMinBuffer[1024];
int iYMaxBuffer[1024];
while(feof(pInputFile) == 0){
for(int i=0;i<9;i++){
fscanf(pInputFile, "%d", &iNumber);
fscanf(pInputFile, ",");
if(i == 1) {
iRotationBuffer[iShapeNr] = iNumber;
}
if(i == 3){//xmin
iXMinBuffer[iShapeNr] = iNumber;
}
if(i == 4){//xmax
iXMaxBuffer[iShapeNr] = iNumber;
}
if(i == 5){//ymin
iYMinBuffer[iShapeNr] = iNumber;
}
if(i == 6){//ymax
iYMaxBuffer[iShapeNr] = iNumber;
}
}
iShapeNr++;
}
fflush(pInputFile);
fclose(pInputFile);
}
The while loop parses the file. The output.txt contains sets of 9 variables, the number of sets is unknown but always in sets of 9.
output.txt could contain for example: 0,1,2,3,4,5,6,7,8,8,7,6,5,4,1,2,3,0
update 2
code:
void runProgram(){
//Check if output.txt exists, if so delete it
if(fileExists("output.txt") == 1){
//Delete output.txt
if( remove( "output2.txt" ) == -1 ){
//errormessage
}else{
//succesfull
}
}
//start program
ShellExecute( NULL, TEXT("open"), TEXT("program.exe"), NULL, NULL, SW_SHOWMAXIMIZED);
while(fileExists("output.txt") == 0);
//Close program
int iCheck = system("taskkill /IM program.exe");
if(iCheck != 0){
//error could not shut down
}
}
sorry for using pre again but I don't get the formatting of this site :(
Will it be due to maximum disk space has been reached and there's still data in the file
streams buffer; fclose'ing a file stream flushes it (writes all the data in the buffer), the write operation will fail since maximum disk space is reached.
I suggest you to scope down the problem, by calling fclose() directly after fopen().
If it success, then something is wrong in the code between fclose() and fopen().
There is probably other places in your code where you don't call fclose, leaking the file. Even in this code, if an error occurs between fopen and fclose (or a return statement, or a continue statement, etc...) you'll leak the file. Please, switch to RAII idiom.
Edit: include this into your code:
struct PoorMansFile {
FILE *_file;
PoorMansFile(const char* str1, const char* str2) : _file(fopen(str1,str2)) {}
~PoorMansFile() { if(_file) fclose(_file); }
operator FILE*() const { return _file; }
};
int fclose(PoorMansFile& file)
{
if(!file)
return 0;
int t = fclose(file._file);
file._file = 0;
return t;
}
and replace each
FILE* file = NULL;
file = fopen(str1, str2);
with:
PoorMansFile file(str1, str2);
Tell us if it helps;
The file could still be in use by the CRT or OS - for example, the OS may buffer writes to the disk. fflush() will only flush CRT buffers, not OS buffers.
Just a shot in the dark here...
What is inside runProgram()? Does that function wait until the program has finished before returning? I wonder if the program that is writing the data is, in fact, still running... it's difficult to tell from here, but thought I'd throw it out there!
After reading all answers and comments I could not think of any reason of OP's problem.
Is this multi threaded or reentrant routine?
What will happen if fopen twice and fclose twice on the same file? Is this could be the cause of the problem?
In this thought I suggest two more checks.
printf all fopen/fclose call
after fclose reset file variable to NULL
f = fopen("", ""); printf("fopen => %p", f);
fclose(f); printf("fclose => %p", f); f = 0;
If you are inconvenient with printf debugging you can use OutputDebugString.
extern void __stdcall OutputDebugStringA(const char*) (MS VC only)
Related
I am making a device that moves back and fourth and needs to store its last position so that upon power up, the last stored value can be grabbed from the last line of the file on an SD card, and it can resume operation. This file will then be destroyed and re-written. For this particular application homing and other methods can not be used because it must start in the spot it last was. Due to position tracking via encoder, there is no positional memory otherwise.The file is setup to be a single data column seperated by commas.
Currently I am successfully writing to the SD card as position changes, and reading the entire file to be printed on the Serial monitor. However, I really only need the last value. The length of the file will always be different do to system operation.
I have read a lot of different solutions but none of them seem to work for my application.
I can read the entire file using:
void read_file() {
// open the file for reading:
myFile = SD.open("test8.txt");
if (myFile) {
Serial.println("test8.txt:");
// read from the file until there's nothing else in it:
// read from the file until there's nothing else in it:
while (myFile.available()) {
String a = "";
for (int i = 0; i < 9; ++i)
{
int j;
char temp = myFile.read();
if (temp != ',' && temp != '\r')
{ //a=temp;
a += temp;
}
else if (temp == ',' || temp == '\r') {
j = a.toInt();
// Serial.println(a);
Serial.println(j);
break;
}
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test8.txt");
}
}
This gives me a stream of the values separated by 0 like this:
20050
0
20071
0
20092
0
20113
0
20133
0
Ideally I just need 20133 to be grabbed and stored as an int.
I have also tried:
void read_file_3() {
// open the file for reading:
myFile = SD.open("test8.txt");
if (myFile) {
Serial.println("test8.txt:");
// read from the file until there's nothing else in it:
Serial.println(myFile.seek(myFile.size()));
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
This only returns "1", which does not make any sense to me.
Update:
I have found a sketch that does what I want, however it is very slow due to the use of string class. Per post #6 here: https://forum.arduino.cc/index.php?topic=379209.0
This does grab the last stored value, however it takes quite awhile as the file gets bigger, and may blow up memory.
How could this be done without the string class?
void read_file() {
// open the file for reading:
myFile = SD.open("test8.txt");
if (myFile) {
while (myFile.available())
{
String line_str = myFile.readStringUntil(','); // string lavue reading from the stream - from , to , (coma to comma)
int line = line_str.toInt();
if (line != 0) // checking for the last NON-Zero value
{
line2 = line; // this really does the trick
}
// Serial.print(line2);
// delay(100);
}
Serial.print("Last line = ");
Serial.print(line2);
// close the file:
myFile.close();
// SD.remove("test3.txt");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
Any help would be greatly appreciated!
seek returns true if it succesffuly goes to that position and false if it does not find anything there, like for instance if the file isn't that big. It does not give you the value at that position. That's why you see a 1, seek is returning true that it was able to go to the position (myFile.size()) and that's what you're printing.
Beyond that, you don't want to go to the end of the file, that would be after your number. You want to go to a position 5 characters before the end of the file if your number is 5 digits long.
Either way, once you seek that position, then you still need to use read just like you did in your first code to actually read the number. seek doesn't do that, it just takes you to that position in the file.
EDIT: Since you edited the post, I'll edit the answer to go along. You're going backwards. You had it right the first time. Use the same read method you started with, just seek the end of the file before you start reading so you don't have to read all the way through. You almost had it. The only thing you did wrong the first time was printing what you got back from seek instead of seeking the right position and then reading the file.
That thing you looked up with the String class is going backward from where you were. Forget you ever saw that. It's doing the same thing you were already doing in the first place only it's also wasting a lot of memory and code space in the process.
Use your original code and just add a seek to skip to the end of the file.
This assumes that it's always a 5 digit number. If not then you may need a little bit of tweaking:
void read_file() {
// open the file for reading:
myFile = SD.open("test8.txt");
if (myFile) {
Serial.println("test8.txt:");
/// ADDED THIS ONE LINE TO SKIP MOST OF THE FILE************
myFile.seek(myFile.size() - 5);
// read from the file until there's nothing else in it:
// read from the file until there's nothing else in it:
while (myFile.available()) {
String a = "";
for (int i = 0; i < 9; ++i)
{
int j;
char temp = myFile.read();
if (temp != ',' && temp != '\r')
{ //a=temp;
a += temp;
}
else if (temp == ',' || temp == '\r') {
j = a.toInt();
// Serial.println(a);
Serial.println(j);
break;
}
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test8.txt");
}
}
See, all I've done is take your original function and add a line to seek the end to it.
Is there an easy way to check if a file is empty. Like if you are passing a file to a function and you realize it's empty, then you close it right away? Thanks.
Edit, I tried using the fseek method, but I get an error saying 'cannot convert ifstream to FILE *'.
My function's parameter is
myFunction(ifstream &inFile)
Perhaps something akin to:
bool is_empty(std::ifstream& pFile)
{
return pFile.peek() == std::ifstream::traits_type::eof();
}
Short and sweet.
With concerns to your error, the other answers use C-style file access, where you get a FILE* with specific functions.
Contrarily, you and I are working with C++ streams, and as such cannot use those functions. The above code works in a simple manner: peek() will peek at the stream and return, without removing, the next character. If it reaches the end of file, it returns eof(). Ergo, we just peek() at the stream and see if it's eof(), since an empty file has nothing to peek at.
Note, this also returns true if the file never opened in the first place, which should work in your case. If you don't want that:
std::ifstream file("filename");
if (!file)
{
// file is not open
}
if (is_empty(file))
{
// file is empty
}
// file is open and not empty
Ok, so this piece of code should work for you. I changed the names to match your parameter.
inFile.seekg(0, ios::end);
if (inFile.tellg() == 0) {
// ...do something with empty file...
}
Seek to the end of the file and check the position:
fseek(fileDescriptor, 0, SEEK_END);
if (ftell(fileDescriptor) == 0) {
// file is empty...
} else {
// file is not empty, go back to the beginning:
fseek(fileDescriptor, 0, SEEK_SET);
}
If you don't have the file open already, just use the fstat function and check the file size directly.
C++17 solution:
#include <filesystem>
const auto filepath = <path to file> (as a std::string or std::filesystem::path)
auto isEmpty = (std::filesystem::file_size(filepath) == 0);
Assumes you have the filepath location stored, I don't think you can extract a filepath from an std::ifstream object.
when the file is empty the tellg will give you value 0 if its empty so focus on that and it is the simplest way to find an empty file, if you just create the file it will give you -1.
outfile.seekg(0,ios::end);
if(file.tellg()<1){
//empty
}else{
file.clear(); // clear all flags(eof)
file.seekg(0,ios::beg);//reset to front
//not empty
}
If your use case offer the possibility to check for emptiness before opening the file,C++17 provides you is_empty
#include <filesystem>
if (!std::filesystem::is_empty("path.txt")) {
///Open and use the file
}
char ch;
FILE *f = fopen("file.txt", "r");
if(fscanf(f,"%c",&ch)==EOF)
{
printf("File is Empty");
}
fclose(f);
How about (not elegant way though )
int main( int argc, char* argv[] )
{
std::ifstream file;
file.open("example.txt");
bool isEmpty(true);
std::string line;
while( file >> line )
isEmpty = false;
std::cout << isEmpty << std::endl;
}
use this:
data.peek() != '\0'
I've been searching for an hour until finaly this helped!
pFile = fopen("file", "r");
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
if (size) {
fseek(pFile, 0, SEEK_SET);
do something...
}
fclose(pFile)
if (nfile.eof()) // Prompt data from the Priming read:
nfile >> CODE >> QTY >> PRICE;
else
{
/*used to check that the file is not empty*/
ofile << "empty file!!" << endl;
return 1;
}
I have problem running and debugging this piece of code:
bool readSectionHeaders(char* path, int numOfSections, int peSectionsOff, IMAGE_SECTION_HEADER* out) {
bool retr = false; //return value
//open file
FILE* file;
file = fopen (path, "rb");
if(file == NULL) {
perror("WRG"); //TODO
return false;
}
do { //do while(false) only for easier error correction
//seek to first section
fseek(file, peSectionsOff, SEEK_SET);
//read all sections
unsigned int count;
IMAGE_SECTION_HEADER sectionHeaders[numOfSections];
count = fread(sectionHeaders, sizeof(IMAGE_SECTION_HEADER), numOfSections, file);
//check Bytes count
if(count != sizeof(IMAGE_SECTION_HEADER)*numOfSections) {
break;
}
//copy sections
memcpy(out, sectionHeaders, count);
//exit successfully
retr = true;
} while(false);
//exit
fclose(file);
return retr;
}
What is strange is that it returns false even when it reads the file. I tried to debug it and here is the strangest part.
I go line by line until this one
if(file == NULL) {
Then even though file is not NULL it skips perror and moves to
return false;
But does not return at all.
I again go line by line until
retr = true;
where it seems to do something, however retr remains false.
Then it closes file and returns with false.
I have never come across something like this.
I tried cleaning project, rebuilding, even deleting files and redownloading them from subversion. Before using this function, I use similar one - I read PE headers. So I though a problem could be with reading the file but it doesn§t explain debug behavior.
After returning from function, I use perror and it writes No error.
I use mingw with QtCreator.
Thanks in advance.
I would do something more like this, if it is able to load the whole array then it will return true.
std::ofstream file(path, std::ios::binary);
if(!file) {
std::cerr << "failed to load file" << std::endl;
return false;
}
file.seekg (peSectionsOff, ios::beg);
IMAGE_SECTION_HEADER sectionHeaders[numOfSections];
size_t size=sizeof(sectionHeaders)*numOfSections;
return //return true if the whole buffer is filled
file.readsome(static_cast<char*>(sectionHeaders), size) == size;
UNTESTED
this might be helpful
http://en.cppreference.com/w/cpp/io
Ok, this was both my and mingw's problem. I've re-installed whole QtSDK with no effect. Then I installed different version of mingw and set qt creator up to use it. Now debugger works without problem. I'm not sure what happened but cerr << "TEST"; also stopped working with the old mingw and this is 100% correct.
As 111111 suggested, problem was with if break clause. I thought read returns number of bytes read and this was simply not true :).
Now it is working, thanks to 111111 for his suggestion :).
we are running into an odd issue when trying to parse an input file. the idea is that this file can include other files, which must be parsed as well. We are doing this recursively in a function defined as
int parse_inp(const char* filename)
The main file parses no problem, but recursive calls cannot open their file streams.
int parse_inp(const char* filename)
{
char buffer[BUFFER_MAX+1];
char* token;
std::string tok;
int keywordSection;
bool end_of_file;
int cardNum;
...
int i;
std::string tempop;
double tempd1, tempd2;
SetSegmentCard2 tempSetSegmentCard2;
int offset;
printf("%s\n", filename);
std::ifstream inp;
inp.clear();
inp.open(filename, std::ios::in);
if(!inp.good() || !inp.is_open())
{
char path1[256];
getcwd(path1,256);
printf("CWD: %s\n", path1);
fflush(NULL);
printf("Unable to open '%s'\n", filename);
return 0;
}
std::set<std::string> unrecognized;
std::string line;
while(inp.good() && !inp.eof())
{
getline(inp, line);
strcpy(buffer, line.c_str());
if (isComments(buffer)) //skip the comments line
continue;
if (buffer[0]=='*') //this is a keyword line
{
token = strtok(buffer," \n");
keywordSection = is_inp_keyw(token);
if (keywordSection==0)
unrecognized.insert(token);
cardNum = 0;
continue;
}
//a data line
tempop="";
char* found = NULL;
char path_buffer[100] = "Dyna3DWriter\\";
int pos = 0;
switch(keywordSection)
{
case 0: //not recognized
//end of last keyword, not recognizable word
break;
case 1: //KEYWORD
//"KEYWORD didn't do anything
break;
case 2: //TITLE
break;
case 3: //INCLUDE
token = strtok(buffer, "\n");
inp.clear();
parse_inp(token);
break;
...
}
}
if(inp.is_open())
{
inp.close();
inp.clear();
}
}
The recursive files never parse. I looked around a lot and most issues seemed to be either that the fail bit was set (which is why we are calling inp.clear() a lot), or that we are making the wrong assumption about the current working directory.
To test the second theory, we added in:
if(!inp.good() || !inp.is_open())
{
char path1[256];
getcwd(path1,256);
printf("CWD: %s\n", path1);
fflush(NULL);
printf("Unable to open '%s'\n", filename);
return 0;
}
And our working directory and file name are both correct. We see the same behavior when using fopen(filename, "r") --- a call to perror("fopen") results in:
fopen: no such file or directory
EDIT: Filled in more code
Are you sure the filename does not contain any garbage or bad character that would lead to this issue?
If the error is file not found, that means the filename is wrong in some way.
Could it come from a bad declaration of buffer? We don't see it in your code.
Another possibility is that you use strtok again in your initialization before opening the file. You must avoid using strtok that is based on global storage for recursive method like this. You should use strtok_r instead.
If your recursive function is called very deeply you can easily overload the OS limit on the number of open files.
I used to run my Gentoo Linux with a per-process file limit of 250 with exceptions for programs that needed a lot more.
Windows has limits that vary depending on how much memory is available to the system and how many objects have already been created system-wide.
The smart way to do this is to have two functions. The first function is the one everyone else calls and it does the setup, including opening the file. The second function is the recursive function and it takes nothing but a reference to a std::ifstream object.
EDIT:
I see that I misunderstood your question, and you aren't recursively opening the same file. I will leave my above paragraph anyway.
Is there an easy way to check if a file is empty. Like if you are passing a file to a function and you realize it's empty, then you close it right away? Thanks.
Edit, I tried using the fseek method, but I get an error saying 'cannot convert ifstream to FILE *'.
My function's parameter is
myFunction(ifstream &inFile)
Perhaps something akin to:
bool is_empty(std::ifstream& pFile)
{
return pFile.peek() == std::ifstream::traits_type::eof();
}
Short and sweet.
With concerns to your error, the other answers use C-style file access, where you get a FILE* with specific functions.
Contrarily, you and I are working with C++ streams, and as such cannot use those functions. The above code works in a simple manner: peek() will peek at the stream and return, without removing, the next character. If it reaches the end of file, it returns eof(). Ergo, we just peek() at the stream and see if it's eof(), since an empty file has nothing to peek at.
Note, this also returns true if the file never opened in the first place, which should work in your case. If you don't want that:
std::ifstream file("filename");
if (!file)
{
// file is not open
}
if (is_empty(file))
{
// file is empty
}
// file is open and not empty
Ok, so this piece of code should work for you. I changed the names to match your parameter.
inFile.seekg(0, ios::end);
if (inFile.tellg() == 0) {
// ...do something with empty file...
}
Seek to the end of the file and check the position:
fseek(fileDescriptor, 0, SEEK_END);
if (ftell(fileDescriptor) == 0) {
// file is empty...
} else {
// file is not empty, go back to the beginning:
fseek(fileDescriptor, 0, SEEK_SET);
}
If you don't have the file open already, just use the fstat function and check the file size directly.
C++17 solution:
#include <filesystem>
const auto filepath = <path to file> (as a std::string or std::filesystem::path)
auto isEmpty = (std::filesystem::file_size(filepath) == 0);
Assumes you have the filepath location stored, I don't think you can extract a filepath from an std::ifstream object.
when the file is empty the tellg will give you value 0 if its empty so focus on that and it is the simplest way to find an empty file, if you just create the file it will give you -1.
outfile.seekg(0,ios::end);
if(file.tellg()<1){
//empty
}else{
file.clear(); // clear all flags(eof)
file.seekg(0,ios::beg);//reset to front
//not empty
}
If your use case offer the possibility to check for emptiness before opening the file,C++17 provides you is_empty
#include <filesystem>
if (!std::filesystem::is_empty("path.txt")) {
///Open and use the file
}
char ch;
FILE *f = fopen("file.txt", "r");
if(fscanf(f,"%c",&ch)==EOF)
{
printf("File is Empty");
}
fclose(f);
How about (not elegant way though )
int main( int argc, char* argv[] )
{
std::ifstream file;
file.open("example.txt");
bool isEmpty(true);
std::string line;
while( file >> line )
isEmpty = false;
std::cout << isEmpty << std::endl;
}
use this:
data.peek() != '\0'
I've been searching for an hour until finaly this helped!
pFile = fopen("file", "r");
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
if (size) {
fseek(pFile, 0, SEEK_SET);
do something...
}
fclose(pFile)
if (nfile.eof()) // Prompt data from the Priming read:
nfile >> CODE >> QTY >> PRICE;
else
{
/*used to check that the file is not empty*/
ofile << "empty file!!" << endl;
return 1;
}