在C ++中读取输入文件

时间:2019-04-23 16:32:42

标签: c++ fstream

我想用C ++读取输入文件,其中包括以下几行

- numberOfStates
- numberOfSymbols
- numberOfFinalStates
- list of final states (one per line)
- numberOfTransitions
- listOfTransitions (one per line. The transitions include two ints and one char)

重要的是要说每个输入文件中的数字都不同。我必须为每个文件读取不同数量的行。

这是一个示例inputFile

10 
3 
1 
9 
12 
0 1 f 
0 3 f 
1 2 a 
2 9 f 
3 4 f 
3 8 f 
4 5 b 
5 6 f 
6 7 a 
8 9 f 

在读取输入文件时如何声明每个变量?

这就是我被困住的地方..真的不知道该怎么办

ifstream fin("inputFile.txt");

    int numberOfStates;
    int numberOfSymbols;
    int numberOfFinalStates;
    // I'm not sure how to declare the next variables because they will vary in size each time 


    while (fin >> numberOfStates >> numberOfSymbols >> numberOfFinalStates)
    {
        cout << numberOfStates << numberOfSymbols << numberOfFinalStates << endl;
    }

如果可能,我想使用向量。

3 个答案:

答案 0 :(得分:0)

while (fin >> name >> var1 >> var2 >> var3)
{
    cout << name << var1 << var2 << var3 << endl;
}

您总是用相同的变量重写,您需要按照问题中的说明将值放入向量中

您还需要检查每个输入是否正确,当前您未检测到无效输入

当然,您需要检查是否已打开文件


示例:

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

int main()
{
  std::ifstream fin("inputFile.txt");

  if (!fin.is_open()) {
    std::cerr << "cannot open inputFile.txt" << std::endl;
    return -1;
  }

  int numberOfStates, numberOfSymbols, numberOfFinalStates;

  if ((! (fin >> numberOfStates >> numberOfSymbols >> numberOfFinalStates))
      || (numberOfStates < 0)
      || (numberOfSymbols < 0)
      || (numberOfFinalStates < 0)) {
    std::cerr << "invalid file" << std::endl;
    return -1;
  }

  // final states are int, so memorize them in a vector of int
  // because their number is known I can size it rather than 
  // to 'push_back' each value
  std::vector<int> finalStates(numberOfFinalStates);

  for (int & ft : finalStates) {
    if (! (fin >> ft)) {
      std::cerr << "invalid file reading final states" << std::endl;
      return -1;
    }
  }

  int numberOfTransitions;

  if (!(fin >> numberOfTransitions) || (numberOfTransitions < 0))  {
    std::cerr << "invalid file reading the number of transitions" << std::endl;
    return -1;
  }

  // you say a transition contains 2 int and a char,
  // I define a structure for
  // i1 i2 and c are 'poor' names but I don't know their goal
  struct Transition {
    int i1, i2; 
    char c;
  };

  // the transitions saved in a vector
  std::vector<Transition> transitions(numberOfTransitions);

  for (Transition & tr : transitions) {
    if (!(fin >> tr.i1 >> tr.i2 >> tr.c)) {
      std::cerr << "invalid file reading transitions" << std::endl;
      return -1;
    }
  }

  // print to check

  std::cout << "numberOfStates=" << numberOfStates << std::endl;
  std::cout << "numberOfSymbols=" << numberOfSymbols << std::endl;
  std::cout << "numberOfFinalStates=" << numberOfFinalStates << "\nfinalStates:";
  for (int ft : finalStates) 
     std::cout << ' ' << ft;
  std::cout << std::endl;
  std::cout << "numberOfTransitions=" << numberOfTransitions
    << "\ntransitions:" << std::endl;
  for (const Transition & tr : transitions)
    std::cout << '\t' << tr.i1 << ' ' << tr.i2 << ' ' << tr.c << std::endl;

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall a.cc
pi@raspberrypi:/tmp $ cat inputFile.txt
10 
3 
1 
9 
12 
0 1 f 
0 3 f 
1 2 a 
2 9 f 
3 4 f 
3 8 f 
4 5 b 
5 6 f 
6 7 a 
8 9 f 
pi@raspberrypi:/tmp $ ./a.out
invalid file reading transitions

出现错误是因为文件仅包含10个转换,而不是预期的12个,因此将文件修改为包含10个转换:

pi@raspberrypi:/tmp $ ./a.out
numberOfStates=10
numberOfSymbols=3
numberOfFinalStates=1
finalStates: 9
numberOfTransitions=10
transitions:
    0 1 f
    0 3 f
    1 2 a
    2 9 f
    3 4 f
    3 8 f
    4 5 b
    5 6 f
    6 7 a
    8 9 f
pi@raspberrypi:/tmp $ 

答案 1 :(得分:0)

如果您有可变数量的东西(在运行时定义),请使用数组:

std::vector<Type>   store;

要将事物添加到向量中,请使用push_back()(还有其他方法,但让我们对初学者来说比较简单)。

store.push_back(value);

要读取多个内容并将它们存储在矢量中,只需使用循环即可。

for(int loop = 0; loop < numberOfThings; ++loop) {
    Type  temp;
    fin >> temp;
    store.push_back(temp);
}

那么这个神秘的Type是什么?您在此处使用适当的类型名称。对于“ Finale State”,它将是一个整数(int),而对于“ Transition”,它将是一个与(int/int/char)相匹配的类类型。

 std::vector<int>   finalState;
 for(int loop = 0; loop < finalState; ++loop) {
     int nextFinal;
     find >> nextFinal;
     finalState.push_back(nextFinal);
 }

 ......
 std::vector<Transition>  Transitions;
 ... Just like above.

答案 2 :(得分:0)

有点晚了,但是无论如何我都会发布它,以展示如何创建自己的流运算符并在创建复合类时使用它们。

echo testing | ./kafka-console-producer.sh --broker-list xxx.yyy.com:2181 --topic topicx