By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

how to open a txt file just by knowing its name..

P: 52
In the c++ fopen() function we need to give the full path of the file along with its name.
But how to open a file if we just know the name like "xyz.txt" and dont know the location of file.
Jun 18 '07 #1
Share this Question
Share on Google+
18 Replies


P: 55
I think we need a pointer.

In Linux environment, we have wild card to this job.

In CLI, when we need to search a file under certain directories, we can type " tab ", to let the os to display the file under this directories.

I don't know how to do that in C++.
Jun 18 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
If you fail to specify the full path, the file is assumed to be in the PWD (present working directory).
Jun 18 '07 #3

P: 52
If you fail to specify the full path, the file is assumed to be in the PWD (present working directory).
but if the file is not in PWD then..it will give error
Jun 18 '07 #4

sicarie
Expert Mod 2.5K+
P: 4,677
but if the file is not in PWD then..it will give error
Have you tried feeding it the absolute path name?

ie, "C:\\Documents and Settings\\username\\Desktop\\file.txt"
or
"/home/user/Desktop/file.txt"

The example in the first one is called backwhacking and has two slashes because \ is considered an escape character - things like \n get read as a newline character, so you do \\ to represent \ .
Jun 18 '07 #5

P: 52
Have you tried feeding it the absolute path name?

ie, "C:\\Documents and Settings\\username\\Desktop\\file.txt"
or
"/home/user/Desktop/file.txt"

The example in the first one is called backwhacking and has two slashes because \ is considered an escape character - things like \n get read as a newline character, so you do \\ to represent \ .
well i think you didnt get my question how can i give the absolute path name..if i dont know the location of file
Jun 18 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
well i think you didnt get my question how can i give the absolute path name..if i dont know the location of file
Oooooooooooooooooh. I didn't. Sorry bout that.

Hmmm, what OS are you using? You'll have to get into the API of that specific OS and use the internal file searching tools. I know an un-elegant bruteforce way to do this in Linux, but I'm sure there's a better way...

What are you trying to do? Is this something you can force the file to be in a certain location? Or can you include in the documentation of the program that the file must be located in a specific place?
Jun 18 '07 #7

archonmagnus
100+
P: 113
You'd have to interface the OS. You could make use of #define macros to check for which OS and use the following commands to return the full path of a file:

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. // For *nix based systems
  3. string filename = system("locate <filename>");
  4.  
  5. // For Windows/DOS based systems
  6. string filename = system("dir \"C:*<filename>\" /s /b");
  7.  
Where <filename> is the user query. Of course this would bomb if there were more than one instance of <filename> on the disk (in differing directories)...

Archon
Jun 19 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
You'd have to interface the OS. You could make use of #define macros to check for which OS and use the following commands to return the full path of a file:

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. // For *nix based systems
  3. string filename = system("locate <filename>");
  4.  
  5. // For Windows/DOS based systems
  6. string filename = system("dir \"C:*<filename>\" /s /b");
  7.  
Where <filename> is the user query. Of course this would bomb if there were more than one instance of <filename> on the disk (in differing directories)...

Archon
Yeah, that's about what I was thinking as well, though I would attempt to avoid the 'system()' command. I believe its return value is only an int - pass/complete as 0 and fail as anything else. The way I was thinking would be to use system("find") but have it piped to either a file or a local value. Ugly, but functional.

I know there is a much better way in the Win32 API - (that is part of the reason it was created), but I'm not sure what the better way is in Linux (though I'm sure it exists).
Jun 19 '07 #9

P: 15
I think findfirst/findnext can give you what you need (on Windows at least). But I am not sure what you try to do. Maybe some explanation on the use case might help.


Yeah, that's about what I was thinking as well, though I would attempt to avoid the 'system()' command. I believe its return value is only an int - pass/complete as 0 and fail as anything else. The way I was thinking would be to use system("find") but have it piped to either a file or a local value. Ugly, but functional.

I know there is a much better way in the Win32 API - (that is part of the reason it was created), but I'm not sure what the better way is in Linux (though I'm sure it exists).
Jun 19 '07 #10

P: 52
I think findfirst/findnext can give you what you need (on Windows at least). But I am not sure what you try to do. Maybe some explanation on the use case might help.
I am concerned only for windows.so please tell me about the better way ..using Win32 api..
Jun 19 '07 #11

P: 52
Yeah, that's about what I was thinking as well, though I would attempt to avoid the 'system()' command. I believe its return value is only an int - pass/complete as 0 and fail as anything else. The way I was thinking would be to use system("find") but have it piped to either a file or a local value. Ugly, but functional.

I know there is a much better way in the Win32 API - (that is part of the reason it was created), but I'm not sure what the better way is in Linux (though I'm sure it exists).
can you please explain the code..
Jun 19 '07 #12

P: 52
Oooooooooooooooooh. I didn't. Sorry bout that.

Hmmm, what OS are you using? You'll have to get into the API of that specific OS and use the internal file searching tools. I know an un-elegant bruteforce way to do this in Linux, but I'm sure there's a better way...

What are you trying to do? Is this something you can force the file to be in a certain location? Or can you include in the documentation of the program that the file must be located in a specific place?
I cant force the file to be in a certain location..otherwise there would have been no problme.the file location depends where the user has stored the file.
Jun 19 '07 #13

P: 52
I think findfirst/findnext can give you what you need (on Windows at least). But I am not sure what you try to do. Maybe some explanation on the use case might help.
findfirst /findfirst are Delphi related things..
how to use them in C++..i tried to use but its giving errors..
Jun 19 '07 #14

arunmib
100+
P: 104
findfirst /findfirst are Delphi related things..
how to use them in C++..i tried to use but its giving errors..
Well the names of the apis are "FindFirstFile" and "FindNextFile" and not just findfirst and findnext

For more information about the APIs refer to : http://msdn2.microsoft.com/en-us/library/Aa364418.aspx
Jun 19 '07 #15

P: 52
Well the names of the apis are "FindFirstFile" and "FindNextFile" and not just findfirst and findnext

For more information about the APIs refer to : http://msdn2.microsoft.com/en-us/library/Aa364418.aspx
thnks i already visited the msdn site but
these functions search only a particular directory specified in the function call.
but file can be anywhere ..then it means i will have to search each and every directory and for that i need the list of all the directories...
how to get that..
Jun 19 '07 #16

weaknessforcats
Expert Mod 5K+
P: 9,197
these functions search only a particular directory specified in the function call.
but file can be anywhere ..then it means i will have to search each and every directory and for that i need the list of all the directories...
how to get that..
You can't have it all ways. If the file is not in the PWD, then you need to supply the full path. Otherwise, you have to search the disc.

Have you considered mapping you files names with a path and keeping this info in a container? You could look up the file name and retrieve the path.
Jun 19 '07 #17

arunmib
100+
P: 104
thnks i already visited the msdn site but
these functions search only a particular directory specified in the function call.
but file can be anywhere ..then it means i will have to search each and every directory and for that i need the list of all the directories...
how to get that..
If you don't mind searching the entire drive (for eg : D:\) means you can construct a simple file search prog with those two APIs and recursive function concept. An simple algo for it can be like,

Search for the file in the path, if the found element is a directory, then call the search file function recursively..Else if the found element is a file compare it with the standard file name you are looking for...

Hope this helps....
Jun 19 '07 #18

archonmagnus
100+
P: 113
Yeah, that's about what I was thinking as well, though I would attempt to avoid the 'system()' command. I believe its return value is only an int - pass/complete as 0 and fail as anything else. The way I was thinking would be to use system("find") but have it piped to either a file or a local value. Ugly, but functional.

I know there is a much better way in the Win32 API - (that is part of the reason it was created), but I'm not sure what the better way is in Linux (though I'm sure it exists).
Oh yeah... I knew something didn't look right about that. I guess I should have tested the code first, eh? Thanks for the correction, Sicarie.
Jun 19 '07 #19

Post your reply

Sign in to post your reply or Sign up for a free account.