There is a text file with data. It lies in the folder with the exe-shnik. How can I download it from there? And then you have to throw it into the root of the HDD for verification. VS2012

Answer:
Found the GetCurrentDirectory(int iBufferSize, LPSTR Buffer) function GetCurrentDirectory(int iBufferSize, LPSTR Buffer) , if anyone needs to be =)

  • one
    Is it just impossible to specify the file name (without the path)? - skegg
  • Found the GetCurrentDirectory function (int iBufferSize, LPSTR Buffer) if anyone should be =) - RomanNVKZ
  • GetCurrentDirectory can sometimes give surprises. And very unusual. For example, if you use the dialog to open / save files, it changes the current directory. And this even if no file was opened - just opened a dialog and moved to another directory. - KoVadim

3 answers 3

Well, for starters, in the function of opening a file, you simply specify the name of the file you want to open and it will open the file in the same directory where your executable is located. But VS has some kind of fool. If you run the program by the executable itself, then it will open the file that lies next to your executable file. But if you run your program from VS itself, then your file should not be in the folder with the Debug file, but in the main folder of the project itself. So choose where you put the file to check and how you will check.

  • Thank you so much, I will try. =) By the way, yes, there is a fool. For example, when launching an application from VS, I get run-time check # 0, and if I run it directly, then all is normal. - RomanNVKZ
  • Yes, it works. Thank. - RomanNVKZ

In main (or _tmain, depending on the version) there are two parameters - argv and argc. They contain a list of input parameters and their number. But the fact is that the path + the name of your exe file is stored under the zero number. Then it all depends on which library you use, but in the simplest case, you can copy everything to the last slash and add the name of your file.

 std::string s(argv[0]); std::string f = s.substr(0, s.find_last_of("\\/") + 1) + "Your file.txt; 

upd

For a more correct definition of the name, it is recommended to use GetModuleFileName

  • one
    @KoVadim "But the fact is that the path + the name of your exe file is stored under the zero number." - what is it, in Microsoft came up with? I have no hand at hand, I can not check. In unix-like'ah there is the first word of the command. If there is an ekzeshnik directory in %path% there is no way, as I think, there can be. Yes, and the extension. .exe also may not be. - alexlz
  • one
    In linuska, the situation is somewhat different. There is an option where the file name with the path can not be obtained (well, except for hacks). Here is a pickup and discussion. - KoVadim
  • @KoVadim I did not read these discussions. The program printing argv [0] has been translated (a.exe) and placed in ~/.wine/drive_c/windows . Then wine cmd.exe , in it typed the command a and got the answer а , but not c:\windows\a.exe . I think that in native Windows it will be exactly the same. - alexlz
  • and let me poured aviation gas into the car and will claim that it spoils the engine. - KoVadim pm
  • one
    @KoVadim I do not understand. But - let us (in Russia, like, aviation gasoline is not produced - very few piston aircraft remain, and the plants are closed-reprofiled). - alexlz 2:58 pm

ExtractFilePath(Application->ExeName) + fileName - This construct is used in C ++ Builder. As a result, AnsiString returned with the full path to the file.

  • one
    Yes, that’s only for TS VS2012. And there it will not work. - KoVadim