2017-04-22 77 views
0

我正在研究一個程序,該程序將讀取文件的第一個數字,創建一個大小的數組,然後讀取第二個文件,然後創建另一個大小的數組。然後它將用第一個文件中的值填充第一個數組,然後用第二個文件中的值填充第二個數組。我已經解決了大部分問題,但我無法弄清楚如何在文件中獲取第一個數字。有人能幫我嗎?文件讀取和存儲數字

int main() 
{ 

    //int a[][],b[][],i,j,k,s,r; 

    ifstream inFile1,inFile2; 
    inFile1.open ("mat1.txt"); 
    inFile2.open ("mat2.txt"); 

    inFile1 >> n; 
    for(i=0;i<n;++i) 
    for(j=i;j<n;++j) 
    inFile1 >> a[i][j]; 

    inFile2 >> n; 
    for(i=0;i<n;++i) 
    for(j=i;j<n;++j) 
    inFile2 >> b[i][j]; 


    inFile1.close(); 
    inFile2.close(); 
    return 0; 
} 
+0

看起來像你讀每個文件的'n'(假設'open'找到文件)。當然,你必須首先聲明變量。你在哪裏看到這個問題? –

+0

如果您在編譯時不知道數組的大小,那麼您需要動態聲明它,或考慮使用向量。 – JGroven

+0

我明白了,但我不知道代碼如何去做。 –

回答

0

我並不完全清楚你的問題想什麼,但我假設喲你想從兩個不同的文件中讀取一個n×n數組,其中n在文件被打開之前是未知的。下面的代碼應該這樣做。

使用注意事項的new聲明一個尺寸,其僅在運行時已知的陣列,並且略微笨拙索引i*n1 + j等,這是因爲C++標準(或至少與先前的)不支持可變長度數組......事實證明,它們對我的編譯器工作正常,但我知道不能保證它們可以在所有編譯器上工作(請參閱here)。另一種方法是使用矢量而不是數組。

此代碼中還有很多重複,所以你可能會更好地把重複的東西放在一個函數中。

/* 
// mat1.txt 
3 
1 2 3 
4 5 6 
7 8 9 
*/ 

/* 
// mat2.txt 
4 
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 
*/ 

#include <iostream> 
#include <fstream> 

using namespace std; 

int main() 
{ 
    int n1, n2; 

    ifstream inFile1,inFile2; 
    inFile1.open ("mat1.txt"); 
    inFile2.open ("mat2.txt"); 

    inFile1 >> n1; 
    int* file1contents = new int[n1*n1]; 
    for(int i=0; i<n1; ++i) { 
     for(int j=0; j<n1; ++j) { 
      inFile1 >> file1contents[i*n1 + j]; 
     } 
    } 

    inFile2 >> n2; 
    int* file2contents = new int[n2*n2]; 
    for(int i=0; i<n2; ++i) { 
     for(int j=0; j<n2; ++j) { 
      inFile2 >> file2contents[i*n2 + j]; 
     } 
    } 

    // Print file contents to check it worked 
    cout << "File 1, size: " << n1 << ", contents:" << endl; 
    for(int i=0; i<n1; ++i) { 
     for(int j=0; j<n1; ++j) { 
      cout << file1contents[i*n1 + j] << ","; 
     } 
     cout << "\n"; 
    } 
    cout << endl; 

    cout << "File 2, size: " << n2 << ", contents:" << endl; 
    for(int i=0; i<n2; ++i) { 
     for(int j=0; j<n2; ++j) { 
      cout << file2contents[i*n2 + j] << ","; 
     } 
     cout << "\n"; 
    } 
    cout << endl; 

    delete [] file1contents; 
    delete [] file2contents; 

    inFile1.close(); 
    inFile2.close(); 
    return 0; 
} 
+0

這確實是我需要知道的。謝謝Kabdulla –

+0

@BonnieDeeds不用擔心。如果它回答您的問題,請考慮將其標記爲已接受/正在提交。 – kabdulla

0

這是你如何從文件中讀取,你可以用這個撥弄讀它的整個文件或只是部分:

與fgets是幹什麼用從文件中讀取

const int maxString = 1024; // put a limit for your buffer 

    printf("reading file\n"); 
    char buf[maxString]; /* make a buffer to store what your reading from 
          the file*/ 
    FILE * fr = fopen(fn, "r"); // open the file 
    while(fgets(buf, maxString, fr)) /* fgets reads the file and writes it 
             to buffer*/ 
    { 
     fputs(buf, stdout); /*see what it is reading*/ 
    } 

    fclose(fr); // close the file 
    remove(fn); // if you want to remove the file when your done reading 

或可能做這樣的事情:

string line; 
//while there is a line to get 
while(getline(fileIn, line)) 
{ 
    firstCharacter = line[0]; // access the first character 
}