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.
18 3053
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++.
If you fail to specify the full path, the file is assumed to be in the PWD (present working directory).
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
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 \ .
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
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?
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: - #include <string>
-
// For *nix based systems
-
string filename = system("locate <filename>");
-
-
// For Windows/DOS based systems
-
string filename = system("dir \"C:*<filename>\" /s /b");
-
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
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: - #include <string>
-
// For *nix based systems
-
string filename = system("locate <filename>");
-
-
// For Windows/DOS based systems
-
string filename = system("dir \"C:*<filename>\" /s /b");
-
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).
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).
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..
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..
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.
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..
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
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..
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.
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....
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: BoonHead, The Lost Philosopher |
last post by:
I think the .NET framework is great!
It's nice, clean and logical; in contradiction to the old Microsoft.
It only saddens me that the new Microsoft still doesn't under stand there own...
|
by: Yuri Vorontsov |
last post by:
Hallo!
We have troubles (post XP SP2) to open local folders from the web application:
- the web application allows users to select a local file (input type=file)
- the system DOES NOT upload...
|
by: wayne |
last post by:
Hey there... I'm having some problems passing url parameters with an
open.window command. I'm not terribly familiar with java script but
here is the code below.
When executed it opens the...
|
by: Kenneth Courville |
last post by:
Hello, I'm looking for assistance with the Access object model. I know
this is VB, but I'm building an Office Add-using C# directed at Access
2002. I'm literate in VB, so you can reply in VB... I...
|
by: fniles |
last post by:
I have a .CSV file (comma delimited) that I want to open using OLEDB, but I
get the error "External table is not in the expected format."
If I save the .CSV file to an .XLS file, I can open the...
|
by: Jon Davis |
last post by:
Newbie to WCF here. I am getting the error "unable to open its
IChannelListener", but the service is running as an admin, and on a
different port from anything else. If it matters, I'm trying to...
|
by: p.wallstedt |
last post by:
Hi all!
I have a small but rather annoying problem with pyserial. I want to
open a file on disk for reading and then open a com-port, write lines
from the file to the port and then read...
|
by: Ron |
last post by:
I am building a dynamic image loading class. I have a list of properties
that have associated images in a specified directory. The problem they are
multiple formats which are not known at...
|
by: SimeonD |
last post by:
Hi
I have an access database called Sales.Mdb
In vb.net 2005, I'd like to open it. Which I can do.
What I can't figure out is how to figure out if Sales.Mdb is open already.
If so, I want to open...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |