简体   繁体   中英

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:

file1: 33 20

file2: 5 7

file3: 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:

 ./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.

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

You can do this in three ways:

  1. In Command prompt (windows)
  2. In Powershell (windows/linux)
  3. In WSL & Linux

1: in 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

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 . Other aliases of gc are cat and type . which means that all three names can be used as replacement or gc

3: use wsl for windows //Linux commands

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

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:

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

And, it'll work on Ubuntu as well.

So, you need to study Command Line Arguments , File Handling , reading from file; 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

File Handling: http://en.cppreference.com/w/cpp/io/basic_fstream

Here's a minimal example for reading from a file ( std::ifstream ):

I've a test file at C:\\Test\\Test.txt with the following contents:

11 22
12 23
23 34

Here's is main.cpp to test it:

#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
Processing Time : 22
Arrival Time : 12
Processing Time : 23
Arrival Time : 23
Processing Time : 34

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