在 Visual Studio 的命令行中将文件作为 C 中的标准输入

发布于 2024-11-07 17:46:18 字数 181 浏览 5 评论 0原文

当我编写 C 程序并使用独立编译器(例如 MinGW)编译它时,我可以编写“myprogram.exe < test.txt”,标准输入是 test.txt。

我怎样才能在 Visual Studio 2010 中做到这一点?我知道项目属性和调试器中的“命令参数”,但我不知道在那里输入什么。它只是输入文件的路径还是其他什么?

When I write a C program and compile it using a standalone compiler, such as MinGW, I can write "myprogram.exe < test.txt" and the standard input is test.txt.

How can I do that in Visual Studio 2010? I'm aware of "Command Arguments" in Project properties and then debugger, but I don't know what to type there. Is it just the path of the input file or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心病无药医 2024-11-14 17:46:18

Visual Studio 确实支持此用例。对于您的 C++ 项目,请转到“属性”、“配置属性”、“调试”,然后在“命令参数”中键入“< test.txt”。

Visual Studio does support this use-case. For your C++ project, go to properties, Configuration Properties, Debugging, and in Command Arguments type "< test.txt".

随波逐流 2024-11-14 17:46:18

您无法直接在 Visual Studio 中执行此操作。 I/O 重定向是命令处理器的功能,而不是 VS 的功能。您可以打开命令提示符,导航到可执行文件所在的目录并发出命令:

myprogram.exe < test.txt

there(假设 test.txt 也在该目录中,如果没有,您可以始终使用完整路径名)。

更新:您可以通过让 VS 运行命令提示符并启动程序来执行您想要的操作。在配置属性|下调试时,将命令字段(通常是 $(TargetPath))中的内容替换为:

cmd.exe /c "$(TargetPath)" < source-file

将命令参数留空。不过我从来没有尝试过这个。这可能行不通。

You can't do that directly in Visual Studio. I/O redirection is a feature of the command processor, not VS. You can open a command prompt, navigate to the directory the executable lives in and issue the command:

myprogram.exe < test.txt

there (assuming test.txt is also in that directory, if not you can always use complete path names).

Update: You may be able to do what you want by having VS run the command prompt for you and start you program. Under Configuration Properties | Debugging, replace what's in the Command field (usually $(TargetPath)) with:

cmd.exe /c "$(TargetPath)" < source-file

Leave Command Arguments blank. I've never tried this, though. It might not work.

橘虞初梦 2024-11-14 17:46:18

转到项目属性,然后在“配置属性 > 调试”下的“命令参数”字段中编写如下内容:
<代码>< "$(TargetDir)\input.txt"

注意:

  • $(TargetDir) 扩展到项目的“Debug”文件夹,因此请确保 input.txt 在那里。您可以在此处放置任何路径,但请确保它是绝对路径。
  • 请务必使用双引号,否则如果路径包含空格,则会出现错误。

(使用带有 C++ 的 Visual Studio 2013)

Go to your project properties, and under "Configuration properties > Debugging", write something like the following in the "Command Arguments" field:
< "$(TargetDir)\input.txt"

Note:

  • $(TargetDir) expands to the "Debug" folder of your project, so make sure input.txt is there. You can put any path here, but make sure it's absolute.
  • Be sure to use the double quotation marks, otherwise you'll get an error if your path contains spaces.

(using Visual Studio 2013 with C++)

很酷不放纵 2024-11-14 17:46:18

这是命令行 shell 程序的功能,而不是编译器的功能。如果您在 msys 的 bash 下运行而不是使用默认的(糟糕的)Windows,那么您完全可以做到这一点DOS 外壳。

That is a function of the command-line shell program, not the compiler. You can do exactly that, if you run under msys's bash instead of using the default (crappy) Windows DOS shell.

盛夏已如深秋| 2024-11-14 17:46:18

在 Visual Studio 2022 中,您可以通过将

在窗口顶部查看您要更改的设置。我建议更改所有配置。

附言。重定向后,您的控制台将立即关闭,因此您必须使用调试器断点才能查看输出。我在这一点上被困了一个小时。
这是它的工作原理的惊人解释(防止控制台窗口在 Visual Studio 中关闭)

https://i.sstatic.net/ivjQC.png

In Visual Studio 2022 you can redirect input to your program by placing <test.txt in debugging\command argument.
Just pay attention to where your file is placed. It will work only if your main and test files are in the same directory.

Look at which setting you are changing at the top of the window. I would suggest changing all configurations.

PS. After redirection, your console will be closed immediately so you have to use a debugger breakpoint to see the output. I was stuck at this point for an hour.
This is amazing explanation how it works (Preventing console window from closing in Visual Studio)

https://i.sstatic.net/ivjQC.png

小姐丶请自重 2024-11-14 17:46:18

对于 Visual Studio 中的项目,右键单击该项目并选择属性。
在属性窗口中,您可以键入要用作输入的物理文件的绝对路径。

别的
您应该能够从 Visual Studio 的控制台运行您的应用程序

“开始”->“程序”->Microsoft Visual Studio 2xxx->Visual Studio 命令提示符

程序:

    class Program
{
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            foreach (var item in args)
            {
                Console.WriteLine("{0}", item);
            }
        }
    }
}

Visual Studio 2010
控制台应用程序参数设置

For a project within visual studio, Right Click on the Project and select Properties.
Within the properties window you can type the absolute path of the physical file which you want to be used as input.

Else
You should be able to run your app from console of Visual studio

Start->Programs->Microsoft Visual Studio 2xxx->Visual Studio Command Prompt

Program :

    class Program
{
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            foreach (var item in args)
            {
                Console.WriteLine("{0}", item);
            }
        }
    }
}

Visual Studio 2010
Console Application Argument settings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文