简体   繁体   English

如何将输入文本文件链接到 C++ 程序

[英]How to link input text file to C++ program

I'm trying to write a C++ program in CLion that opens, manipulates, and closes an input file and output file.我正在尝试在 CLion 中编写一个 C++ 程序,用于打开、操作和关闭输入文件和输出文件。 The input file is a .txt file, but when I execute the program, it can't find the input file even though it's in the same directory as the .cpp program it's executing from.输入文件是一个 .txt 文件,但是当我执行程序时,它找不到输入文件,即使它与它正在执行的 .cpp 程序位于同一目录中。 I was wondering if it was because I had to link the input file with CMake, but I'm unfortunately unfamiliar with configuring CMake.我想知道是不是因为我必须将输入文件与 CMake 链接,但不幸的是我不熟悉配置 CMake。 How can I link a .txt file to a C++ project?如何将 .txt 文件链接到 C++ 项目?

You don't "link" a text file to an executable file.您不会将文本文件“链接”到可执行文件。 In CLion, the default behavior is to build the project binary into a folder called cmake-build-debug .在 CLion 中,默认行为是将项目二进制文件cmake-build-debug到名为cmake-build-debug的文件夹中。 The text file you want to use as your input has to be in the same directory as the .exe file, not the .cpp , so what you would have to do in this case is prepend a ../ to the name of the text file (ie.. if your file is called input.txt , the C code would say something like您要用作输入的文本文件必须与.exe文件位于同一目录中,而不是.cpp ,因此在这种情况下,您必须在文本名称前添加../文件(即.. 如果您的文件名为input.txt ,则 C 代码会说类似

FILE* input_file = fopen("../input.txt", "r");

Remember to check for return codes, errors, etc.请记住检查返回代码、错误等。


The above solution is not good for a variety of reasons;由于多种原因,上述解决方案并不好; first of all, you're hard coding a filename, which is at best a huge limitation and at worst pointless.首先,您正在硬编码文件名,这充其量是一个巨大的限制,最糟糕的是毫无意义。 A better solution would be to pass in the name of the file via the command line.更好的解决方案是通过命令行传入文件名。

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if (argc != 2) { return EXIT_FAILURE; }

    FILE* inputFile = fopen(argv[1], "r");

    ...

    return EXIT_SUCCESS;
}

This solution is still not great, however, primarily because you should always sanitize your inputs, never just accepting string input from the user and simply using it right away.然而,这个解决方案仍然不是很好,主要是因为您应该始终清理您的输入,永远不要只接受用户的字符串输入并立即使用它。 This is a huge security liability.这是一个巨大的安全责任。 I also didn't check return codes or anything;我也没有检查返回代码或任何东西; I simply verified argc was two.我只是验证了argc是两个。 (Remember that a program will always have argc at least equal to 1 , since the first argument is the program's name.) (请记住,程序的argc始终至少等于1 ,因为第一个参数是程序的名称。)


To answer your actual question (the one you asked, though not necessarily the one you needed an answer to), you can use CMake's find_file function to "link" to a file.要回答您的实际问题(您提出的问题,但不一定是您需要回答的问题),您可以使用 CMake 的find_file函数“链接”到文件。 You're not actually linking anything, really, the find_file function simply returns the full path to a named file, but you can then pass this information as a parameter to your program as discussed previously.您实际上并没有链接任何东西,实际上, find_file函数只是返回命名文件的完整路径,但是您可以将此信息作为参数传递给您的程序,如前所述。

You can do more than might be immediately obvious with CMake, actually;实际上,您可以做的比使用 CMake 显而易见的事情要多得多; here is the documentation for their file-related functions.是与文件相关的功能的文档。

You can also execute your newly-built program from CMake directly, using the execute_process function.您还可以使用execute_process函数直接从 CMake 执行您新建的程序。 The function actually takes an INPUT_FILE parameter directly, so you can set a variable for the path of your input file, set it using the result of find_file , then directly execute the program with your input file using execute_process .该函数实际上直接采用INPUT_FILE参数,因此您可以为输入文件的路径设置一个变量,使用find_file的结果设置它,然后使用execute_process直接使用输入文件执行程序。

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

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