|
You can check using the following code -
bool bPathExist = true;
CFileException Ex;
if (!File.Open (FileName, CFile::modeCreate, &Ex))
{
if (Ex.m_cause == CFileException::badPath)
{
bPathExist = false;
}
}
and to make sure that each directory exist in the whole path, use Api -
::MakeSureDirectoryPathExists
|
|
|
|
|
|
Unfortunately there is no POSIX or standard method for doing this. boost::filesystem is as close as it gets.
However you may try using this:
http://msdn2.microsoft.com/en-us/library/1w06ktdy(VS.80).aspx[^]
example usage:
TCHAR *file = _T("c:\\test.txt");
if((-1 != _access(file, 0)))
_stprintf(_T("%s file exists.\n"), file);
else
_stprintf(_T("%s file does not exist.\n"), file);
Best Wishes,
-Randor (David Delaune)
|
|
|
|
|
but I will use it in a .c file.
When the codes were compiled,it would post an error,that ATL only can be used in .cpp file!
|
|
|
|
|
I will have a try imediately.
Thank you very much.
|
|
|
|
|
try PathIsDirectory()
Regards,
Pankaj Sachdeva
"There is no future lies in any job"
"but"
"future lies in the person who holds the job"
|
|
|
|
|
kcynic wrote: but I will use it in a .c file.
When the codes were compiled,it would post an error,that ATL only can be used in .cpp file!
_access() has nothing to do with ATL, or whether it's used in a .c or .cpp file.
Posting the actual error message, as well as the code snippet that produced it, will go a long way towards helping you. Anything short of that, good luck.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
TCHAR* fpath=_T("C:\\WINDOWS");
if(PathIsDirectoryW(fpath)==TRUE)
cout<<"Is a directory"<
|
|
|
|
|
kcynic wrote: if(PathIsDirectoryW(fpath)==TRUE)
Have you tried:
if (PathIsDirectoryW(fpath) != FALSE)
... kcynic wrote: ...but it does not work.
Which means what?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
use this code this will work actually this function returns some value but not TRUE if direstory exists else it returns 0
TCHAR* fpath=_T("C:\\WINDOWS");<br />
if(PathIsDirectory(fpath))<br />
cout<<"Is a directory"<<endl;<br />
Regards,
Pankaj Sachdeva
"There is no future lies in any job"
"but"
"future lies in the person who holds the job"
|
|
|
|
|
I you want to use of a directory and also your are not sure its exist or no use SetCurrentDirectory and check return value of this function.
|
|
|
|
|
kcynic wrote: I use GetFileAttributes().
But it need a wide character set string as its paramter,otherwise,it does not work.
It will work with either Unicode or ANSI builds.
kcynic wrote: Now,I want to using one byte char string as the path,
Most paths are comprised of more than one byte. Perhaps you meant something else.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Im trying to create a simple program the reads a string of integers from the board and store them in an array, then out put to the screen each integer from that string. It compiles fine but then crashes with an error. This is my code:
<br />
<br />
#include "stdafx.h"<br />
#include conio.h<br />
#include iostream<br />
using namespace std;<br />
<br />
<br />
int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
int aArray[4];<br />
int n;<br />
cout << "Input 5 digits: "; cin >> aArray[4];<br />
for ( n=0; n<0; n++ )<br />
{<br />
cout << "Integer: " << aArray[n] << endl;<br />
}<br />
<br />
_getch();<br />
return 0;<br />
}<br />
this is the error
Run-Time Check Failure #2 - Stack around the variable 'aArray' was corrupted.
|
|
|
|
|
The last element of aArray if aArray[3], but not aArray[4].
And what do you mean by:
Herboren wrote: for ( n=0; n<0; n++ )
|
|
|
|
|
forgive me I had noticed that earlier I corrected it. Edit:
<br />
<br />
#include "stdafx.h"<br />
#include <<conio.h>><br />
#include <<iostream>><br />
using namespace std;<br />
<br />
int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
int aArray[4];<br />
int n;<br />
<br />
<br />
cout << "Input 5 digits: "; cin >> aArray[0];<br />
<br />
for ( n=0; n<5; n++ )<br />
{<br />
cout << "Integer: " << aArray[n] << endl;<br />
}<br />
_getch();<br />
return 0;<br />
}<br />
of course knowing the array is 5 integers long '01234'. Basically the program is supposed to read a string of integers from the user, for example, input '12345' all together on the first line. Then each value within that string is stored in an array therefore if I wanted to print the first value of that array the value would come out as the '1' and so on. Now when you build the code it asks for an input the downside to this is it asks for 5 elements as a string or individual, in 5 different columns. so if you typed '12345' you would have to press enter and repeat that 4 more times, which is what im trying to avoid. The operation of the program is supposed to ask for the string of integers once and place each element of the string into an array therefor the string '12345' would be placed in aArray[4] thus giving me 5 elements in in one column. smell my drift?
|
|
|
|
|
The program is working fine but the logic in your code is not correct.
All you need to do is to parse the string to extract it's digits, store them in array and then print.
|
|
|
|
|
Herboren wrote: cin >> aArray[4];
NO
You have to prepare a for loop to input the 4 numbers one after the other.
Russell
|
|
|
|
|
Herboren wrote: Im trying to create a simple program the reads a string of integers
...
int aArray[4];
These two seem to contradict each other. You either meant to say, "Im trying to create a simple program the reads an array of integers," or you need to declare:
Herboren wrote: for ( n=0; n<0; n++ )
This loop will not do what you expect, unless you expect it to execute 0 times.
string aArray[4]; In any case, consider:
int aArray[4];
cin >> aArray[0];
cin >> aArray[1];
cin >> aArray[2];
cin >> aArray[3]; or
for (int n = 0; n < 3; n++)
cin >> aArray[n]; Notice the benefits of the latter example?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Russel *Sigh*, if you have a little more to tell me than just "NO" i might actually listen to you. Crow even turning it to a string wouldnt that mean its still in char format and not int. I should be able to add an unspecified amount to each element of the string inside the array. Like adding a 1 to 12345 making it 22345.
<br />
#include "stdafx.h"<br />
#include <conio.h><br />
#include <sstream><br />
#include <string><br />
#include <iostream><br />
#include <cstdlib><br />
using namespace std;<br />
<br />
<br />
int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
int aArray[4];<br />
int u,d,t,q,c,a;<br />
string st;<br />
<br />
cout << "Input 5-digits: "; cin >> st; cout << st << endl; _getch();<br />
istringstream ss(st);<br />
<br />
<br />
ss >> u;<br />
aArray[0] = u;<br />
if ( ss.peek() != '\n' )<br />
{<br />
cerr<<"Invalid format"<<endl;<br />
return EXIT_FAILURE;<br />
}ss.ignore();<br />
<br />
ss >> d;<br />
aArray[1] = d;<br />
if ( ss.peek() != '\n' )<br />
{<br />
cerr<<"Invalid format"<<endl;<br />
return EXIT_FAILURE;<br />
}ss.ignore();<br />
<br />
ss >> t;<br />
aArray[2] = t;<br />
if ( ss.peek() != '\n' )<br />
{<br />
cerr<<"Invalid format"<<endl;<br />
return EXIT_FAILURE;<br />
}ss.ignore();<br />
<br />
ss >> q;<br />
aArray[3] = q;<br />
if ( ss.peek() != '\n' )<br />
{<br />
cerr<<"Invalid format"<<endl;<br />
return EXIT_FAILURE;<br />
}ss.ignore();<br />
<br />
ss >> c;<br />
aArray[4] = c;<br />
cout << u << d << t << q << c << " "<br />
<< aArray[0] <br />
<< aArray[1]<br />
<< aArray[2]<br />
<< aArray[3]<br />
<< aArray[4]<br />
<< endl;<br />
cout << aArray[0] << endl;<br />
_getch();<br />
return 0;<br />
}<br />
<br />
-- modified at 10:49 Tuesday 18th September, 2007
|
|
|
|
|
Herboren wrote: *Sigh*, dont worry bout it dude, I must not be making myself clear enough I dont know any other basic way to put it. I got better results somewhere else. sheesh!
That's fine, but remember, you're the one asking for help, not us.
Herboren wrote: Crow even turning it to a string...
I was not suggesting using one type over another. I was merely pointing out that the type you do use ultimately determines what API/classes you need. For example, if you say you want to add a bunch of numbers together, but provide a code snippet that uses string s, there's obviously going to be extra work involved.
Herboren wrote: I should be able to add an unspecified amount to each element of the string inside the array. Like adding a 1 to 12345 making it 22345.
And how does the code snippet you've shown do any of this? What exactly are you wanting the user to input (there's a big difference between five numbers or digts, and a 5-digit number)?
This all may sound pedantic, but if we can't understand what it is that you want, how could you possibly expect an answer?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Forgive me if I may have sounded crude. I have researched google dreamincode codeguru among other search engines and did not find my answer /cry, sorry im just frustrated. This is role of my program. Think of it as a uname/pword program which leads to a basic encryption or start of it:
--role--
Please input a 5 digit number: 78265
Your input was 78265, is this correct? y
--role--
Now, knowing user input was a 5 digit number, that should then be broken down into and array of integers:
78265 Each element of the 5 digit number would be placed into an array after user input ie: 7,8,2,6,5
|
|
|
|
|
Much clearer. How about something like:
string strNumber = "78265";
int nDigits[5];
nDigits[0] = strNumber[0] - '0';
nDigits[1] = strNumber[1] - '0';
nDigits[2] = strNumber[2] - '0';
nDigits[3] = strNumber[3] - '0';
nDigits[4] = strNumber[4] - '0';
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
strNumber cannot be declared in the program. The strNumber has to ask for users input, because the number could be anything that user specifies. smell my drift? . There has to be a way to store the users input '12345' in an array after they type it in.
-- modified at 15:09 Tuesday 18th September, 2007
Here is antoher extended role
--role
Please type in a five digit number: 79135 //Ask for user input
The number you typed was'79135' is this correct? y //Is input correct y/n
The first number '7' from 79135 will be stored in aArray[0]. //store 7 in array0
The second number '9' from 79135 will be stored in aArray[1]. //store 9 in array0
The third number '1' from 79135 will be stored in aArray[2]. //store 1 in array0
The fourth number '3' from 79135 will be stored in aArray[3]. //store 3 in array0
The fifth number '5' from 79135 will be stored in aArray[4]. //store 5 in array0
Each element of the 5 digit number from user input have been stored individually in the array[5]
--role--
|
|
|
|
|
Herboren wrote: strNumber cannot be declared in the program. The strNumber has to ask for users input, because the number could be anything that user specifies. smell my drift?
Yes, and what I showed was merely an example. I'm not in the position of providing all of the cin /cout statements that you require. Even new developers know that string strNumber = "79135" can be turned into a user input-able statement with but one line of code. How a value gets assigned to a variable, either via the keyboard or as a string literal, is irrelevant.
Herboren wrote: The first number '7' from 79135 will be stored in aArray[0]. //store 7 in array0
The second number '9' from 79135 will be stored in aArray[1]. //store 9 in array0
The third number '1' from 79135 will be stored in aArray[2]. //store 1 in array0
The fourth number '3' from 79135 will be stored in aArray[3]. //store 3 in array0
The fifth number '5' from 79135 will be stored in aArray[4]. //store 5 in array0
Why would you want all five digits stored in array0 ? Furthermore, what is array0 ?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
it was supposed to say
//store in aArray[0] //example
//store in aArray[1] //example
//store in aArray[2] //example
//store in aArray[3] //example
//store in aArray[4] //example
this was only an example, this has nothing to do with my code this was just a role just so you could better understand what I was trying to say.
<quote>Yes, and what I showed was merely an example. I'm not in the position of providing all of the cin/cout statements that you require.
I know it was an exmaple and im not asking you to tell what the cin and cout is. I know I can grab sets of digits as a string, I know basically everything you just told me. I dont know any other basic way to put it, i have role played it for you twice now and you still dont seem to understand what im saying. Reverting back to the 2nd question I asked yesterday, The problem I am having is storing each e-l-e-m-e-n-t of the "string" into an a-r-r-a-y.
|
|
|
|