繁体   English   中英

从文件中计算和读取整数

[英]Counting and Reading integers from a file

好的,所以目前我很难尝试从文件中读取整数。 (我使用的是C ++)

    int main(void)
{
    int size = 0;
    string name;
    string line;
    while(getline(cin,line)) //get the line from input//
    {
        size++;
    }
    cout << size;
}

这是目前的代码,文件的输入是

2 3 17 1 9 23 8 4 22 15 10 

8 7 14

我使用visual studio 2010并设置了调试器以从文件中输入并输出到另一个文件。

输出只是2,因为它只计算行数。

在任何情况下,我要做的是计算此文件中的所有数字,然后创建一个“大小”数组,然后将每个数字输入到数组中。 现在我想要这种方式的原因是因为我希望能够接受任意数量的整数。 有人可以帮我吗?

我希望这可以不使用矢量工作。

编辑:这是你要找的c ++

我的c ++技能有点生疏所以请原谅,但这应该有效

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;
#define MAX_LINES 25
#define MAX_IN_LINE 50

int main() {
  //open our file
  ifstream myfile ("test.txt");
  //initialize our array's
  int myarray[MAX_LINES][MAX_IN_LINE];
  //used for knowing how many numbers are stored in each array
  int totalargs[MAX_LINES];
  //rest of variables
  string line,tempnum;
  int end=0;
  int firstarg=0,secondarg=0;
  int num;

  //set all of our arrays to be zero'd out
  memset(myarray,0,MAX_LINES*MAX_IN_LINE);
  memset(totalargs,0,MAX_LINES);

  //make sure file is opened
  if (myfile.is_open()) {
    //get a line
    getline(myfile,line);
    //the first line is the number, so set it to num
    num=atoi(line.c_str());

    while(!myfile.eof()) {
      getline(myfile,line);
      //if there is a , in the line we have gotten
      while((end=line.find(' ',0))!=string::npos) {
        //get the number before the ,
        tempnum=line.substr(0,end);
        myarray[firstarg][secondarg]=atoi(tempnum.c_str());
        secondarg++;
        //erase the part of the line we have gotten
        line.erase(0,end+1);
      }
      //we will have an extra number at the end after that loop
      //this gets that last number
      tempnum=line.substr(0,line.length());
      myarray[firstarg][secondarg]=atoi(tempnum.c_str());
      //set the number of args to our array
      totalargs[firstarg]=secondarg;
      firstarg++;
      //reset arg.
      secondarg=0;
    }
  } else {
    cout << "cannot open";
  }

  //this is extra, but it just shows you your variables and that
  //they really do have numbers in them.
  cout << "num: " << num << endl;
  for (int x=0;x<firstarg;x++) {
    cout << "Array " << x+1 << ": " << myarray[x][0];
    for (int y=1;y<=totalargs[x];y++) {
      cout << "," << myarray[x][y];
    }
    cout << endl;
  }
}

另一个更简单的解决方案是

vector<int> numbers;
ifstream fin("infile.txt");
int x;
while( fin >> x ) {
    numbers.push_back(x);
}
std::ifstream in("myFile.txt");
int i, size=0;
while(in>>i) // counting int at the input
{
    size++;
}
in.seekg (0, in.beg);
int arr=new int[size];
for(size_t i=0;i<size;++i)
  in>>arr[i];

...
delete[]arr;

为什么不用矢量?

std::ifstream in("myFile.txt");
int i;
std::vector<int> v;
while(in>>i) // counting int at the input
{
    v.push_back(i);
}

暂无
暂无

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

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