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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • happiness4all
    New Member
    • May 2007
    • 52

    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.
  • Peterwkc
    New Member
    • Apr 2007
    • 55

    #2
    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++.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      If you fail to specify the full path, the file is assumed to be in the PWD (present working directory).

      Comment

      • happiness4all
        New Member
        • May 2007
        • 52

        #4
        Originally posted by weaknessforcats
        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

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by happiness4all
          but if the file is not in PWD then..it will give error
          Have you tried feeding it the absolute path name?

          ie, "C:\\Docume nts and Settings\\usern ame\\Desktop\\f ile.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 \ .

          Comment

          • happiness4all
            New Member
            • May 2007
            • 52

            #6
            Originally posted by sicarie
            Have you tried feeding it the absolute path name?

            ie, "C:\\Docume nts and Settings\\usern ame\\Desktop\\f ile.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

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by happiness4all
              well i think you didnt get my question how can i give the absolute path name..if i dont know the location of file
              Ooooooooooooooo ooh. 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?

              Comment

              • archonmagnus
                New Member
                • Jun 2007
                • 113

                #8
                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:

                [CODE=cpp]#include <string>
                // For *nix based systems
                string filename = system("locate <filename>");

                // For Windows/DOS based systems
                string filename = system("dir \"C:*<filename> \" /s /b");
                [/CODE]
                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

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Originally posted by archonmagnus
                  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:

                  [CODE=cpp]#include <string>
                  // For *nix based systems
                  string filename = system("locate <filename>");

                  // For Windows/DOS based systems
                  string filename = system("dir \"C:*<filename> \" /s /b");
                  [/CODE]
                  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).

                  Comment

                  • plemoine
                    New Member
                    • Jun 2007
                    • 15

                    #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.


                    Originally posted by sicarie
                    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).

                    Comment

                    • happiness4all
                      New Member
                      • May 2007
                      • 52

                      #11
                      Originally posted by plemoine
                      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..

                      Comment

                      • happiness4all
                        New Member
                        • May 2007
                        • 52

                        #12
                        Originally posted by sicarie
                        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..

                        Comment

                        • happiness4all
                          New Member
                          • May 2007
                          • 52

                          #13
                          Originally posted by sicarie
                          Ooooooooooooooo ooh. 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..other wise there would have been no problme.the file location depends where the user has stored the file.

                          Comment

                          • happiness4all
                            New Member
                            • May 2007
                            • 52

                            #14
                            Originally posted by plemoine
                            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..

                            Comment

                            • arunmib
                              New Member
                              • May 2007
                              • 104

                              #15
                              Originally posted by happiness4all
                              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 "FindFirstF ile" and "FindNextFi le" and not just findfirst and findnext

                              For more information about the APIs refer to : http://msdn2.microsoft .com/en-us/library/Aa364418.aspx

                              Comment

                              Working...