gpt4 book ai didi

c++ - new >> 我如何将一个包含 3 列且每列包含 100 个数字的文件读取到一个数组中?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:46 25 4
gpt4 key购买 nike

int exam1[100];// array that can hold 100 numbers for 1st column 
int exam2[100];// array that can hold 100 numbers for 2nd column
int exam3[100];// array that can hold 100 numbers for 3rd column

int main()
{
ifstream infile;

int num;
infile.open("example.txt");// file containing numbers in 3 columns
if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
}
while(!infile.eof()) // reads file to end of line
{
for(i=0;i<100;i++); // array numbers less than 100
{
while(infile >> [exam]); // while reading get 1st array or element
???// how will i go read the next number
infile >> num;
}
}
infile.close();
}

最佳答案

int exam1[100];// array that can hold 100 numbers for 1st column 
int exam2[100];// array that can hold 100 numbers for 2nd column
int exam3[100];// array that can hold 100 numbers for 3rd column

int main() // int main NOT void main
{
ifstream infile;

int num = 0; // num must start at 0
infile.open("example.txt");// file containing numbers in 3 columns
if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
return 1; // no point continuing if the file didn't open...
}
while(!infile.eof()) // reads file to end of *file*, not line
{
infile >> exam1[num]; // read first column number
infile >> exam2[num]; // read second column number
infile >> exam3[num]; // read third column number

++num; // go to the next number

// you can also do it on the same line like this:
// infile >> exam1[num] >> exam2[num] >> exam3[num]; ++num;
}
infile.close();

return 0; // everything went right.
}

我假设您每行总是有 3 个数字。如果您知道确切的行数,请将 while 替换为从 0 到行数的 for

关于c++ - new >> 我如何将一个包含 3 列且每列包含 100 个数字的文件读取到一个数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2706076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com