简体   繁体   中英

Reading .txt file in C++ using redirect

I am attempting to read a .txt file. I am tasked with using only:

   #include <iostream>

I must use redirect.

Multiple lines of data, such as:

AddItem 3 1 1 4 7.75 7.62 0.69 0.025 4.97 2 0 8

exist in the file.

I need to read in the "AddItem" as a Char array, and the values as ints and doubles. Here is my code.

#include <iostream>
using namespace std;

void emptyString(char* x, int size) {
   for (int i = 0; i < size; i++)
       x[i] = '\0';
}

int main() {
    char command[10];
    int quantity, code, brand, type, option, option2;
    double height, length, width, weight, price;

    while (!cin.eof()) {// while end of file is not reached
        emptyString(command, 10);
        cin >> command 
        >> quantity 
        >> code 
        >> brand 
        >> height 
        >> length
        >> width
        >> weight
        >> price
        >> type
        >> option
        >> option2;
    
    if (!cin.eof()) {
        cout << command << ", "
            << quantity << ", "
            << code << ", "
            << brand << ", "
            << height << ", "
            << length << ", "
            << width << ", "
            << weight << ", "
            << price << ", "
            << type << ", "
            << option << ", "
            << option2 << ", "
            << endl;
        }
    }
    return 0;
}

When I run the file it never ends. ctrl+z is supposed to stop it, but that does nothing for me in Visual Studio 2015.

My output looks like this: 输出

I would try something like this:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using std::cout; using std::endl;
using std::cin; using std::getline;
using std::string; using std::istringstream;
using std::vector;

void process_command(const string& cmd, const vector<double>& args);

int main()
{
  string line, command;
  while (getline(cin, line)) { //read input one line at a time
    istringstream ss(line); //use stringstream to read from the line
    ss >> command;

    vector<double> args;
    double cur_arg;
    while (ss >> cur_arg) //read arguments until we hit the end
      args.push_back(cur_arg); //add argument to vector

    //process the command                                                       
    process_command(command, args);
  }

  return 0;
}

void process_command(const string& cmd, const vector<double>& args)
{
  cout << "Command: " << cmd << "\n"
       << "Arguments: ";
  for (auto s : args)
    cout << s << " ";
  cout << endl;
}

Example input:

AddItem 3 1 1 4 7.75 7.62 0.69 0.025 4.97 2 0 8
DoStuff 37 -123 0.235
DoMoreStuff 7 1e6 23 0.418

Example output:

Command: AddItem
Arguments: 3 1 1 4 7.75 7.62 0.69 0.025 4.97 2 0 8 
Command: DoStuff
Arguments: 37 -123 0.235 
Command: DoMoreStuff
Arguments: 7 1e+06 23 0.418 

Maybe that will give you some ideas.

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