2013-04-01 59 views
0

好的,所以目前我很難從文件中讀取整數。 (我使用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,因爲它只是計算行數。

在任何情況下,我想要做的是計算這個文件中的所有數字,然後創建一個「大小」數組的數量,然後將每個數字輸入到數組中。現在我想要這種方式的原因是因爲我想能夠接受任何數量的整數。有人可以幫我嗎?

我希望這個工作不使用向量。

+0

該程序的當前輸出是什麼? –

+0

@DanPichelman目前的輸出是2,因爲它所做的只是計算行數而不是個別整數。 –

回答

1
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); 
} 
+0

這很好,但是,它不允許我用新方法吱吱作響,因爲它不能改變「 int into int *「 –

+0

」你把int改成int *「是什麼意思? – qPCR4vir

1

編輯:這裏是++你想找的

我的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); 
} 
+0

這是一個返回指針的函數嗎? –

+0

對不起,我重新閱讀問題 –

+0

後編輯我的答案是否有可能它可以完成沒有var? –