getting filename

Feb 3, 2010 at 10:54am
hi,
how can i get a filename?
like i have file name abc.txt , xyz.txt
how am i get name of the file?
abc and xyz
Feb 3, 2010 at 11:05am
Feb 3, 2010 at 11:49am
That question I feel needs more clarification, do you want either the user to enter a file and then you check if the file exists ? or do you want to open a file ? or something else ?
Feb 3, 2010 at 12:20pm
oh,
First:
i need to check if the file exist

Second:
i need to check file lvl means my file name contain lvl
Example:
abc_10.txt
10 would be the lvl
Feb 3, 2010 at 12:25pm
I assume you are getting input from user for that. Have a look at bazzy's above post because you probably want to store the filename as string and store a substring of that for the second test. as for the first test:

http://www.cplusplus.com/doc/tutorial/files/
http://www.cplusplus.com/reference/iostream/ifstream/
Last edited on Feb 3, 2010 at 12:27pm
Feb 3, 2010 at 1:25pm
I have a great way to know if the file exists in C++ or C

C++
1
2
3
4
5
6
7
8
9
bool fexists(char* fname) {
    ifstream f;
    f.open(fname);

    bool f_exists = f.is_open();

    f.close()
    return f_exists;
}


C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum {
    False = 0, True
};

typedef int boolean;

boolean fexists(char* fname) {
    FILE* f = fopen(fname, "r");

    boolean f_exists = (f == NULL) ? False : True;

    fclose(f);

    return f_exists;
}

Note: on UNIX you can use sys/stat.h

If a file can be opened, it exists. If it can't be opened, it either
a. doesn't exist, or
b. you don't have permission to read it, in which case it may as well not exist anyway, as far as you're concerned.
Last edited on Feb 3, 2010 at 1:26pm
Feb 3, 2010 at 6:29pm
i also thought of Chrisname idea. but i not using bool..lol i just use string:
1
2
3
4
5
6
if (f.is_open){
x=1
}
else{
x=0
}

Chrisname code is more standard and better. lol

but my prob is getting the filename.
i have read through the link u gave but still didt get any idea on getting a file name.
coz in my program i need to set user read/write permission through the file lvl.
the file lvl as i already mention
1
2
3
abc_10.txt
aaa_9.txt
bbb_8.txt

how i gonna get the number at back as the file lvl?
so i can set the user permission.

and 1 more question,
how i gonna know in the directory have how many file?
example:
i have files store is directory Files
inside Files i have
1
2
3
abc_10.txt
aaa_9.txt
bbb_8.txt


then how i get those filename in directory Files?
Last edited on Feb 3, 2010 at 6:44pm
Feb 3, 2010 at 6:51pm
Feb 3, 2010 at 6:55pm
i found this on microsoft website. izit working to solve my prob?

1
2
3
4
5
6
7
String^ fileName = "C:\\mydir\\myfile.ext";
String^ path = "C:\\mydir\\";
String^ result;
result = Path::GetFileName( fileName );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", fileName, result );
result = Path::GetFileName( path );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", path, result );
Feb 3, 2010 at 7:20pm
This isn't unmanaged C++, this is managed C++ which runs under CLR aka it uses .NET framework
You probably don't want to use this code.
chrisname's example is easy to use as well.
Feb 3, 2010 at 7:23pm
Ew, managed code.
Feb 4, 2010 at 4:57pm
hi there,
im using visual studio 2008 to do my program.
i tried dirent.h but its not working.
first it say cant find dirent.h directory.
so i search for dirent.h code and add to my header file.
now i run again with #include "dirent.h"
and i get more and more errors.
i keep finding dirent.h code to replace
but still keep giving me errors.
like:
1
2
cant find features.h
cant find sys/cdefs.h


can gimme a correct dirent.h file download link?
Feb 4, 2010 at 5:05pm
ops.. i get it already...

Thanks..
Feb 4, 2010 at 5:10pm
The reason is because ms visual studio doesn't have dirent, it's a POSIX addition to the C library. You could use MinGW, though.
Feb 4, 2010 at 6:44pm
ok
forget bout that. im done that already, anyway thx alot.

now i get a new problem.
now i can display my file like:
1
2
3
aaa_1.txt
bbb_2.txt
ccc_3.txt


i wan to get the value at the back
so i using getline() to get it but
if the user key in the correct filename then the user would able to read the file.
but if the user key in a wrong filename then my system will stuck.
My code:
1
2
3
cout << "Please key in your filename." << endl;
getline(cin, filename, '_');    <<== the error.
getline(cin, value);


the error part is when the user key in a filename without "_" then the system will stuck
my question is how to solve this part? Jus any clue on how to do it or the way to do it.
Thanks~!
Last edited on Feb 4, 2010 at 6:52pm
Topic archived. No new replies allowed.