ベストは、ファイルを1行ずつ読み込み、各行要素を配列に入れます(印刷するだけの場合は配列に格納しないでください).C++ストリームの代わりにc関数を使用しています大規模なデータのために、彼らはfaster.The機能が正常に動作しますfgetc_unlockedお使いのシステムに大きなdata.Ifのために使用された場合fscanfはより高速であるfgetc関数を使用する必要がありますので、あなたはそれが
-5 -6 2 -1 4
1 2 3 4
が入力のようなものであると仮定しFGETCし交換する必要がありますinput.txtに格納されます。あなたのdirにinput.txtを作成して、同じディレクトリに次のコードを実行してください。後で番号を使用する方法を変更することができます
#include<iostream>
#include<cstdio>
using namespace std;
#define GC fgetc // replace with fgetc_unlocked if it works in your system(Linux)
//This function takes a line of f and put all integers into A
//and len is number of elements filled
void getNumsFromLine(FILE* f, int *A, int& len){
register char ch=GC(f);
register int n=0;
register bool neg = false;
len=0;
while(ch!='\n'){
while(ch !='-' && (ch>'9' || ch<'0')) ch=GC(f);
if(ch=='-') {
neg = true;
ch = GC(f);
}
while(ch>='0' && ch<='9'){
n = (n<<3)+(n<<1)+ch-'0';
ch = GC(f);
}
if(neg) {
n=-n;
neg=false;
}
A[len++]=n;
n=0;
}
}
int main(){
FILE* f=fopen("input.txt", "r");
int A[10][2],len;
for(int i=0; i<2; i++){
getNumsFromLine(f, A[i], len);
for(int j=0; j<len; j++) cout << A[i][j] <<" ";
cout << endl;
}
fclose(f);
return 0;
}
「数字を分割する」とはどういう意味ですか?それらを文字列に変換することを意味しますか? – CroCo