473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
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++.
Jun 18 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
If you fail to specify the full path, the file is assumed to be in the PWD (present working directory).
Jun 18 '07 #3
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
4,677 Expert Mod 4TB
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
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
4,677 Expert Mod 4TB
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
113 100+
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
4,677 Expert Mod 4TB
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
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
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
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
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
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
104 100+
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
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
9,208 Expert Mod 8TB
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
104 100+
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
113 100+
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

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

Similar topics

11
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...
4
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...
29
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...
6
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...
22
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...
4
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...
7
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...
8
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...
14
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.