Rodric MacOliver
asked on
Check If a Variable FileName exists
Hi folks, I'm trying to check if a file that has a variable name exists in a certain path, but have not been successful until now.
The code is below so that you guys can better understand what I'm trying to do... So any help would be much appreciated...
The Idea is to create files named as "HourDayMonthYear.txt", where each has 2 numbers to represent them, and afterwards to check if the file exists.
Any help would be much appreciated.
Cheers.
The code is below so that you guys can better understand what I'm trying to do... So any help would be much appreciated...
//---------------------------------------------------------------------------
#include <time.h>
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <sstream>
#include <vcl.h>
#pragma hdrstop
#include <windows.h>
#include <iostream>
#include <fstream>
#include <cstdlib> //Needed for exit()
#include "Shlwapi.h"
#include "Teste1.h"
#include <sys/stat.h>
using namespace std;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int hour;
int day;
int month;
int year;
hour= 98; //Ignores the 9 in the first case so it means 8
day= 28;
month= 99; //Ignores the 9 in the first case so it means 9
year= 14;
//working fine
stringstream ss;
//address and name of the files
//create file name
ss << "C:/Users/rodrigo/Desktop/Calendar/" << hour<< day<< month<< year<<".txt";
string address;
address = ss.str();
//Working after changing the argument to the below one
ofstream fout(address.c_str());
fout<<hour<<day<<month<<year;
fout.close();
}
//---------------------------------------------------------------------------
The Idea is to create files named as "HourDayMonthYear.txt", where each has 2 numbers to represent them, and afterwards to check if the file exists.
Any help would be much appreciated.
Cheers.
The filename that gets created by your code is this :
"C:/Users/rodrigo/Desktop/ Calendar/9 8289914.tx t"
so the "9"s are not ignored as stated in your comment. That's the reason you see a file not found
I'm not sure if you are using the extra "9"s as a placeholder but a better way would be to pad with leading zeros
Something like
hope this helps
"C:/Users/rodrigo/Desktop/
so the "9"s are not ignored as stated in your comment. That's the reason you see a file not found
I'm not sure if you are using the extra "9"s as a placeholder but a better way would be to pad with leading zeros
Something like
int hour;
int day;
int month;
int year;
hour= 8;
day= 28;
month= 9;
year= 14;
//working fine
stringstream ss;
//address and name of the files
//create file name
//pad with leading 0s
ss << "C:/Users/rodrigo/Desktop/Calendar/" << setw(2)<<setfill('0')<<hour<< day<< setw(2)<<setfill('0')<<month<< year<<".txt";
string address;
address = ss.str();
//Working after changing the argument to the below one
ofstream fout(address.c_str());
fout<<hour<<day<<month<<year;
fout.close();
hope this helps
remember to
#include <iomanip>
I've padded only for the hour and month. You will need to do it for the day and year as well
ASKER
trinitrotoluene, I know that it is not ignore, I didn't create the code for that yet, what I need it a way to check if a file,
in this case:
"C:/Users/rodrigo/Desktop/ Calendar/" << hora << dia << mes << ano<<".txt"
Exists or not.
in this case:
"C:/Users/rodrigo/Desktop/
Exists or not.
Use FileExists() function.
if(FileExists(address))
// do something
ASKER
chaau, I get the following errors using the FileExists() function:
[bcc32 Error] Teste1.cpp(47): E2034 Cannot convert 'string' to 'UnicodeString'
[bcc32 Error] Teste1.cpp(47): E2342 Type mismatch in parameter 'FileName' (wanted 'const UnicodeString', got 'string')
[bcc32 Error] Teste1.cpp(47): E2034 Cannot convert 'string' to 'UnicodeString'
[bcc32 Error] Teste1.cpp(47): E2342 Type mismatch in parameter 'FileName' (wanted 'const UnicodeString', got 'string')
Use this:
if(FileExists(UnicodeString(address)))
// do something
ASKER
chaau, didn't work.
Errors:
[bcc32 Error] Teste1.cpp(47): E2285 Could not find a match for 'UnicodeString::UnicodeStr ing(string )'
[bcc32 Error] Teste1.cpp(47): E2031 Cannot cast from 'string' to 'UnicodeString'
Errors:
[bcc32 Error] Teste1.cpp(47): E2285 Could not find a match for 'UnicodeString::UnicodeStr
[bcc32 Error] Teste1.cpp(47): E2031 Cannot cast from 'string' to 'UnicodeString'
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
chaau, now only one error:
[bcc32 Error] Teste1.cpp(47): E2235 Member function must be called or its address taken
[bcc32 Error] Teste1.cpp(47): E2235 Member function must be called or its address taken
if(FileExists(UnicodeStrin g(address. c_str())))
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why don't you just use the Format function:
Open in new window
BTW, I prefer to start with Year Month Day Hour. In this case the file names will have better sort order.To check if the file exists you can use either Win32 API GetFileAttributes() function or Delphi's own FileExists() function
Open in new window