简体   繁体   English

C ++我应该如何发送读取特定.txt文件的项目?

[英]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. 我有一个C ++项目,我想以可执行形式发送给某人。 The issue is the program must read from a .txt that I created (specific deliminators). 问题是程序必须从我创建的.txt中读取(特定的分隔符)。 Currently my program reads from a file path that is specific to my computer, parseFile("/Users/David/Desktop/FinalProject/Store.txt"); 当前,我的程序从特定于我计算机的文件路径parseFile(“ / Users / David / Desktop / FinalProject / Store.txt”)读取;

How could I package the .txt file and the executable file together, where the exec. 我如何将.txt文件和可执行文件打包在一起,在哪里执行。 reads specifically from the that .txt on anyone's machine? 专门从任何人的机器上的那个.txt读取?

Note: I am using Xcode 注意:我正在使用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 使用该程序编写注释(ReadMe)以指定文件格式,并在程序包中添加示例数据文件

tl;dr: if you just put the text file in the same folder with your executable, you can open it with parseFile("Store.txt"); tl; dr:如果只是将文本文件与可执行文件放在同一文件夹中,则可以使用parseFile("Store.txt");打开它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. "/Users/David/Desktop/FinalProject/Store.txt"就是这样一个示例,因为它始于根目录( /在* nix上为/ ,在Windows上为DriveLetter:\\ ),并确切说明了需要遍历哪些目录才能到达文件。

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/ . 例如,如果您打开二进制文件(存储为/Users/David/Desktop/FinalProject/a.exe ,则工作目录将设置为/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 . 如果您的程序随后尝试打开"Store.txt" ,则运行时将看到您正在尝试打开一个不完全限定的路径,因此它将假定您打算打开相对于工作目录的文件。然后将是/Users/David/Desktop/FinalProject/ + Store.txt ,它将是/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 . 如果移动a.exe沿Store.txt/Users/David/Desktop/FinalProject(copy)/ ,那么当你打开/Users/David/Desktop/FinalProject(copy)/a.exe ,工作目录将是/Users/David/Desktop/FinalProject(copy)/现在,现在调用parseFile("Store.txt") ,它将打开/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. 但是,C标准的一部分是main()的第一个命令行参数应该是可执行文件的名称,并且大多数实现都为您提供了完全限定的路径。 With some minimal parsing, you can use that to determine what path to use as a base for addressing files. 通过一些最少的解析,您可以使用该解析来确定用作文件寻址基础的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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