所以ü意味着ü要讀取文件和複製內容到char矩陣[] [] ??,你可以使用while循環讀取字符,並通過線每1024個字節(1 KB)分裂,我的意思是這樣做:
#include <fstream.h>
int main()
{
int n=0, m=0, MAX_LINES=1025, i=0, j=0;
char character, Matrix[1024][1025];
fstream file;
file.open("file.txt", ios::in);
while (file.get(character))// loop executes until the get() function is able to read contents or characters from the file
{
Matrix[n][m]=character; // copy the content of character to matrix[n][m], here n is used to work as lines and m as normal arrays.
m++; // finished copying to matrix[n][m] then m++ else the program will overwrite the contents to the same array.
If (m>=1024)// if string[n][m] reached the limit which is 1024.
{
Matrix[n][m]='\0'; //termimate that line
m=0;// start with a new column
if (n<MAX_LINES)// if n is less than 1024 then
n++;// write to a next line because m can support only 1024 chars.
}
}
Matrix[n][m]='\0';// this will terminate the whole string not just one line.
file.close();
for (i=0; i<1025; i++)
{
for (j=0; j<=1024 || Matrix[i][j]!='\0'; j++)
cout<<Matrix[i][j];
}
return 0;
}
此代碼將讀取1024×1024個字符,但如果txt文件是小於1024(M)的字符,while循環將退出和Matrix [n]的[M] = '\ 0';語句被執行。
編輯:正如大衛問我寫的整個代碼與主()對不起兄弟我忘了,代碼中的錯誤是變量n和m初始化到1025和1024,所以程序跳過寫入矩陣作爲矩陣[1024] [1025]不能存儲更多的字符...我認爲這會有所幫助,好的兄弟...
我很想加入我的觀點,並加入你的所有,但這取決於數據如何佈局文本文件。發佈樣本。 – 2014-11-03 17:57:05
'std :: vector'? – clcto 2014-11-03 17:59:16
@MarcoA。我不知道這意味着什麼,但我喜歡它 – 2014-11-03 18:00:32