Mad_Angel
asked on
Program cannot find an input file if the output file is saved outside of the program's directory
Hello,
I have a code that takes an input file, does calculations to it and outputs it into an output file. There are 2 input files, one is asked to be provided by the user, the other one is in the ImpFiles folder in the program directory. When i run the code and save the output file in the same forder then it works fine, but when i try to save it on the desktop for example, it gives me my built in error. Heres my code:
void PropertyEstimatesElasticDl g::OnBnCli ckedOk()
{
CString inputFileFourier;
CString temp;
CString inputFileMaterial;
CString outputCalculatedFile;
GetDlgItemText(IDC_INPUT_F OURIER_ELA STIC, inputFileFourier);
GetDlgItemText(IDC_INPUT_M ATERIAL_EL ASTIC, inputFileMaterial);
GetDlgItemText(IDC_PROPERT Y_OUTPUT_E LASTIC, outputCalculatedFile);
char buffer[256];
//Open the file where the Fourier coefficients representing the microstrucutre are
double F[4];
F[0]=1; //F000 is always egual to 1
ifstream Fcoefficient (inputFileFourier);
if (! Fcoefficient.is_open()){
MessageBox("Error opening file.", "Fourier coefficient file.", MB_OK | MB_ICONEXCLAMATION);
return;
}
Fcoefficient>>F[1]>>F[2]>> F[3];
Fcoefficient.close();
double coef[24];
double consC[3], consS[3];
ifstream elastic (inputFileMaterial);
while ( elastic.eof() ){
elastic.getline(buffer,100 );
}
elastic>>consC[0]>>consC[1 ]>>consC[2 ]>>consS[0 ]>>consS[1 ]>>consS[2 ];
ifstream coeff ("ImpFiles/coefficients_el astic.txt" );
if ( ! coeff.is_open())
{
MessageBox("Error opening file.", "coefficients_elastic file", MB_OK | MB_ICONEXCLAMATION);
return;
}
while ( coeff.eof() ){
coeff.getline(buffer,100);
}
for (int i=0; i<24;i++) coeff>>coef[i];
ofstream outputfile (outputCalculatedFile);
if (outputfile.is_open()){
//do calculation
}
else
{
MessageBox("Could not open the output file.", "Error openning file", MB_OK | MB_ICONEXCLAMATION);
return;
}
outputfile.close();
OnOK();
}
The error that I'm getting is the coefficients_elastic.txt could not be opened
Thank You
I have a code that takes an input file, does calculations to it and outputs it into an output file. There are 2 input files, one is asked to be provided by the user, the other one is in the ImpFiles folder in the program directory. When i run the code and save the output file in the same forder then it works fine, but when i try to save it on the desktop for example, it gives me my built in error. Heres my code:
void PropertyEstimatesElasticDl
{
CString inputFileFourier;
CString temp;
CString inputFileMaterial;
CString outputCalculatedFile;
GetDlgItemText(IDC_INPUT_F
GetDlgItemText(IDC_INPUT_M
GetDlgItemText(IDC_PROPERT
char buffer[256];
//Open the file where the Fourier coefficients representing the microstrucutre are
double F[4];
F[0]=1; //F000 is always egual to 1
ifstream Fcoefficient (inputFileFourier);
if (! Fcoefficient.is_open()){
MessageBox("Error opening file.", "Fourier coefficient file.", MB_OK | MB_ICONEXCLAMATION);
return;
}
Fcoefficient>>F[1]>>F[2]>>
Fcoefficient.close();
double coef[24];
double consC[3], consS[3];
ifstream elastic (inputFileMaterial);
while ( elastic.eof() ){
elastic.getline(buffer,100
}
elastic>>consC[0]>>consC[1
ifstream coeff ("ImpFiles/coefficients_el
if ( ! coeff.is_open())
{
MessageBox("Error opening file.", "coefficients_elastic file", MB_OK | MB_ICONEXCLAMATION);
return;
}
while ( coeff.eof() ){
coeff.getline(buffer,100);
}
for (int i=0; i<24;i++) coeff>>coef[i];
ofstream outputfile (outputCalculatedFile);
if (outputfile.is_open()){
//do calculation
}
else
{
MessageBox("Could not open the output file.", "Error openning file", MB_OK | MB_ICONEXCLAMATION);
return;
}
outputfile.close();
OnOK();
}
The error that I'm getting is the coefficients_elastic.txt could not be opened
Thank You
> ifstream coeff ("ImpFiles/coefficients_el astic.txt" );
You must use fully qualified path names. Something like:
c:\myappfolder\ImFiles\coe fficients_ elastic.tx t
To achieve this, first save application folder name in a variable, and add before your filename. To obtain exe's folder use WINAPI's GetModuleFileName()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getmodulefilename.asp
You must use fully qualified path names. Something like:
c:\myappfolder\ImFiles\coe
To achieve this, first save application folder name in a variable, and add before your filename. To obtain exe's folder use WINAPI's GetModuleFileName()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getmodulefilename.asp
>>You must use fully qualified path names
That's not true. Relative paths will work also.
Where exactly does the program reside and where exactly is the input file stored. BTW, what about using
bool GetInputFileName ( string& strName) {
BYTE Filter [ 100] = "Text (*.txt)\0*.txt\0\0";
char acBuf [ MAX_PATH];
OPENFILENAME ofName;
ZeroMemory ( &ofName, sizeof( OPENFILENAME));
ZeroMemory ( acBuf, MAX_PATH);
ofName.hwndOwner = NULL;
ofName.lStructSize = sizeof( OPENFILENAME);
ofName.lpstrFile = acBuf;
ofName.nMaxFile = MAX_PATH;
ofName.lpstrInitialDir = NULL;
ofName.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_LONGNAMES;
ofName.lpstrDefExt = "txt";
ofName.lpstrFilter = ( char *) Filter;
ofName.nFilterIndex = 1;
ofName.lpstrTitle = "Choose InputFile";
if ( !GetOpenFileName ( &ofName))
return ( false);
strName = acBuf
return ( true);
}
//...
string strName;
GetInputFileName(strName);
ifstream Fcoefficient (strName.c_str());
if (! Fcoefficient.is_open()){
MessageBox("Error opening file.", "Fourier coefficient file.", MB_OK | MB_ICONEXCLAMATION);
return;
}
That's not true. Relative paths will work also.
Where exactly does the program reside and where exactly is the input file stored. BTW, what about using
bool GetInputFileName ( string& strName) {
BYTE Filter [ 100] = "Text (*.txt)\0*.txt\0\0";
char acBuf [ MAX_PATH];
OPENFILENAME ofName;
ZeroMemory ( &ofName, sizeof( OPENFILENAME));
ZeroMemory ( acBuf, MAX_PATH);
ofName.hwndOwner = NULL;
ofName.lStructSize = sizeof( OPENFILENAME);
ofName.lpstrFile = acBuf;
ofName.nMaxFile = MAX_PATH;
ofName.lpstrInitialDir = NULL;
ofName.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_LONGNAMES;
ofName.lpstrDefExt = "txt";
ofName.lpstrFilter = ( char *) Filter;
ofName.nFilterIndex = 1;
ofName.lpstrTitle = "Choose InputFile";
if ( !GetOpenFileName ( &ofName))
return ( false);
strName = acBuf
return ( true);
}
//...
string strName;
GetInputFileName(strName);
ifstream Fcoefficient (strName.c_str());
if (! Fcoefficient.is_open()){
MessageBox("Error opening file.", "Fourier coefficient file.", MB_OK | MB_ICONEXCLAMATION);
return;
}
Try to run you program from explorer and from inside your compiler IDE, your "current directory" won't be the same. So, if you have an "aplication directory" at the same root as your .exe file, I recommend you again to use GetModuleFileName to avoid any confussion when trying to access that folder.
That allows you to change to any user directory without lossing the "path" to your own directory.
That allows you to change to any user directory without lossing the "path" to your own directory.
ASKER
I can't get the GetModuleFileName() to work, i tried using CString but it doesnt allow me, can you post an example please?
PS jkr, I don't want the user to provide me with coefficients_elastic.txt, i want the program to know that it's in the folder ImpFiles which is inside of the program directory.
Thanks
PS jkr, I don't want the user to provide me with coefficients_elastic.txt, i want the program to know that it's in the folder ImpFiles which is inside of the program directory.
Thanks
>>i want the program to know that it's in the folder ImpFiles which is inside of the program directory.
Are you executing the application from VisualStudio? If so, try
ifstream coeff ("../ImpFiles/coefficients _elastic.t xt");
since jaime_olivares is right about the IDE issue.
Are you executing the application from VisualStudio? If so, try
ifstream coeff ("../ImpFiles/coefficients
since jaime_olivares is right about the IDE issue.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use so:
ifstream coeff (appDir+"ImpFiles/coeffici ents_elast ic.txt");
ifstream coeff (appDir+"ImpFiles/coeffici
ASKER
I'm executing the program from both VisualStudio and the built file.
With ifstream coeff ("../ImpFiles/coefficients _elastic.t xt"); it doesn't find the file even when i save the outpul file in the program directory where it was run from.
With ifstream coeff ("../ImpFiles/coefficients
So,
your paths are
c:\somepath\MyProject\mypr og.exe
c:\somepath\MyProject\ImpF iles\coeff icients_el astic.txt
?
your paths are
c:\somepath\MyProject\mypr
c:\somepath\MyProject\ImpF
?
ASKER
I got it, jaime-olivares' tested code worked for me.
Thanks
Thanks
ASKER
Thanks