简体   繁体   中英

C++ How should I send project that reads specific .txt files?

I have a c++ project that I would like to send to someone in executable form. The issue is the program must read from a .txt that I created (specific deliminators). Currently my program reads from a file path that is specific to my computer, parseFile("/Users/David/Desktop/FinalProject/Store.txt");

How could I package the .txt file and the executable file together, where the exec. reads specifically from the that .txt on anyone's machine?

Note: I am using Xcode

Change your programs to receive 'file path' as a parameter. Write a note(ReadMe) with the program to specify the file format and added a sample data file with the package

tl;dr: if you just put the text file in the same folder with your executable, you can open it with parseFile("Store.txt");

In most runtime implementations, there is a notion of a "working directory." When you open up an executable via the graphical shell (by double clicking it or something to that effect) the working directory is the same as the directory the executable is in.

Now, if you try to open a file in your program via a path that isn't fully qualified, then the path that gets used will be relative to the working directory.

A fully qualified path is a discrete path that points to a single entity in your filesystem. "/Users/David/Desktop/FinalProject/Store.txt" is one such example, as it starts at root ( / on *nix, DriveLetter:\\ on Windows) and says exactly which directories you need to traverse to get to your file.

A path that is not fully qualified (which basically means that it doesn't start at the root of your filesystem) can be used to perform relative file addressing. Most runtimes will assume that any path that is not fully qualified is meant to be relative to the working directory, which basically means that the path that actually gets opened is the result of concatenating your provided path to the end of the working directory.

As an example, if you opened your binary, which is stored as /Users/David/Desktop/FinalProject/a.exe , then the working directory would be set to /Users/David/Desktop/FinalProject/ . If your program then tried to open "Store.txt" , the runtime would see that you're trying to open a path that isn't fully qualified, so it would assume you meant to open a file relative to the working directory, which would then be /Users/David/Desktop/FinalProject/ + Store.txt , which would be /Users/David/Desktop/FinalProject/Store.txt .

The nice thing about this is that if you move your binary, the working directory moves too. if you move a.exe along with Store.txt to /Users/David/Desktop/FinalProject(copy)/ , then when you open /Users/David/Desktop/FinalProject(copy)/a.exe , the working directory will be /Users/David/Desktop/FinalProject(copy)/ now, and now when you call parseFile("Store.txt") , it will instead open up /Users/David/Desktop/FinalProject(copy)/Store.txt . This holds true when moving to other computers, too.

It's worth noting that if your binary is run from a command line utility, the working directory will often be the directory the command line shell is in, rather than the executable's directory. It is, however, a part of the C standard that the first command line parameter to main() should be the name of the executable, and most implementations supply you with the fully qualified path. With some minimal parsing, you can use that to determine what path to use as a base for addressing files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM