简体   繁体   English

如何在 windows 的命令行中读取 input.in 文件

[英]How do I read input .in files in command line on windows

I completed my code but the problem is I don't know how to test it based on instructions given.我完成了我的代码,但问题是我不知道如何根据给定的说明对其进行测试。

The instructor provided us with 3 input files with the following values:讲师为我们提供了3具有以下值的输入文件:

file1: 33 20文件 1:33 33 20

file2: 5 7文件 2: 5 7

file3: 18 15文件 3:18 18 15

I'm supposed to take these values and create event objects with these stored values.我应该采用这些值并使用这些存储的值创建事件对象。 Problem is the instructor is giving the testing method on Ubuntu and she displayed how to input file in command line like:问题是讲师在Ubuntu上给出了测试方法,她展示了如何在命令行中输入文件,如:

 ./sApp < simulationShuffled3.in

So i'm just really confused as to how I'm suppose to get it working.所以我真的很困惑我应该如何让它工作。 I am currently using Windows console, VStudios and sublime text with a terminal attachment.我目前正在使用带有终端附件的 Windows 控制台、VStudios 和 sublime text。

The code I'm currently using that's following from an example from my course notes is我目前正在使用的代码来自我的课程笔记中的一个例子是

while (getline(cin >> ws, aLine)) {   // while (there is data)
    stringstream ss(aLine);
    ss >> arrivalTime >> processingTime;
    Event newEvent = Event('A',arrivalTime,processingTime);
    eventPriorityQueue.enqueue(newEvent);
}

Read input from stdin in Windows [&/or] Linux从 Windows [&/or] Linux 中的stdin输入读取输入

You can do this in three ways:您可以通过三种方式执行此操作:

  1. In Command prompt (windows)在命令提示符 (windows) 中
  2. In Powershell (windows/linux)在 Powershell (windows/linux)
  3. In WSL & Linux在 WSL & Linux

1: in Windows Cmd 1: 在 Windows Cmd

type input.in | python file_requiring_input.py              # in python

type input.in | go run file_requiring_input.go              # in go

type input.in | node file_requiring_input.js                # in js

javac file_requiring_input.java && 
type input.in | java file_requiring_input                   # in java

g++ file_requiring_input.cpp && 
type input.in | a.exe                                       # in cpp

gcc file_requiring_input.c && 
type input.in | a.exe                                       # in c

Another way另一种方式

python file_requiring_input.py < input.in                   # in python

g++ file_requiring_input.cpp && 
a.exe < input.in                                            # in cpp

2: in Powershell 2:在Powershell

gc .\input.in | python file_requiring_input.py              # in python

g++ .\file_requiring_input.cpp ; 
gc .\input.in | .\a.exe                                     # in cpp

gc is short for Get-Content . gcGet-Content的缩写。 Other aliases of gc are cat and type . gc的其他别名是cattype which means that all three names can be used as replacement or gc这意味着所有三个名称都可以用作替换或 gc

3: use wsl for windows //Linux commands 3: 对 windows 使用wsl //Linux 命令

cat input.in | python file_requiring_input.py                # in python

g++ file_requiring_input.cpp && 
cat input.in | ./a.out                                       # in cpp

another way另一种方式

python file_requiring_input.py < input.in                    # in python

g++ file_requiring_input.cpp && 
./a.out < input.in                                           # in cpp

have edited the code testing everything out hopefully.已经编辑了代码以测试所有内容。 Do correct me if I am wrong or if anything more is to be added.如果我错了或者要添加更多内容,请纠正我。 Also input.in is more generally a text file so you can create input.txt file as you wish and it would work the same此外,input.in 更普遍地是一个文本文件,因此您可以根据需要创建 input.txt 文件,它的工作原理相同

Well, as this is an assignment, I'm not going to provide you with a ready-made code. 好吧,由于这是一项任务,因此我不会为您提供现成的代码。 Rather, I'll just point you to the right direction. 相反,我只会为您指出正确的方向。

In Windows, you can provide the arguments to your executable separated by space on Command Prompt like this: 在Windows中,您可以为可执行文件提供参数,例如在Command Prompt用空格分隔:

C:\Assignment> Test.exe file1.in file2.in file3.in

And, it'll work on Ubuntu as well. 而且,它也将在Ubuntu

So, you need to study Command Line Arguments , File Handling , reading from file; 因此,您需要研究Command Line ArgumentsFile Handling ,从文件读取; and, you'll have to convert these strings read from files to integers. 并且,您必须将这些从文件读取的字符串转换为整数。

Command Line Arguments: http://en.cppreference.com/w/c/language/main_function 命令行参数: http : //en.cppreference.com/w/c/language/main_function

File Handling: http://en.cppreference.com/w/cpp/io/basic_fstream 文件处理: http //en.cppreference.com/w/cpp/io/basic_fstream

Here's a minimal example for reading from a file ( std::ifstream ): 这是读取文件( std::ifstream )的最小示例:

I've a test file at C:\\Test\\Test.txt with the following contents: 我在C:\\Test\\Test.txt有一个测试文件,其内容如下:

11 22 11 22
12 23 12 23
23 34 23 34

Here's is main.cpp to test it: 这是main.cpp进行测试:

#include <iostream>
#include <fstream>
#include <string>

int main( int argc, char *argv[] )
{
    const std::string filename { R"(C:\Test\Test.txt)" };

    std::ifstream ifs { filename };
    if ( !ifs.is_open() )
    {
        std::cerr << "Could not open file!" << std::endl;
        return -1;
    }

    int arrivalTime    { 0 };
    int processingTime { 0 };

    while ( ifs >> arrivalTime >> processingTime )
    {
        std::cout << "Arrival Time : " << arrivalTime << '\n'
                  << "Processing Time : " << processingTime << std::endl;
    }

    ifs.close();
    return 0;
}

Output: 输出:

Arrival Time : 11 到达时间:11
Processing Time : 22 处理时间:22
Arrival Time : 12 到达时间:12
Processing Time : 23 处理时间:23
Arrival Time : 23 到达时间:23
Processing Time : 34 处理时间:34

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

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