C++ 将文件输入到C++;
我有两个文件,我应该把它们都输入数组。 其中之一是:C++ 将文件输入到C++;,c++,arrays,C++,Arrays,我有两个文件,我应该把它们都输入数组。 其中之一是: 2 3 4 5 7 8 第二个是 2 4 5 6 7 8 2 3 4 5 7 8 (要长得多,但没关系)。我需要有一个包含前6个数字的数组,然后我要检查第二个文件中的前6个数字是否与第二个数组中的数字相同,是否与接下来的6个数字相同,依此类推(比如检查彩票中奖者)。 我想我应该把一个文件中的数字加载到多个数组中,但我不知道怎么做,而且我在任何地方都找不到 到目前为止,我拥有的第一个数组的代码是: #include<iostream&
2
3
4
5
7
8
第二个是
2
4
5
6
7
8
2
3
4
5
7
8
(要长得多,但没关系)。我需要有一个包含前6个数字的数组,然后我要检查第二个文件中的前6个数字是否与第二个数组中的数字相同,是否与接下来的6个数字相同,依此类推(比如检查彩票中奖者)。
我想我应该把一个文件中的数字加载到多个数组中,但我不知道怎么做,而且我在任何地方都找不到
到目前为止,我拥有的第一个数组的代码是:
#include<iostream>
#include<fstream>
using namespace std;
int main(){
int numbers[6];
int count = 0;
ifstream inputFile;
inputFile.open("Numbers.txt");
while (count < 20 && inputFile >> numbers[6]){
count++;
inputFile.close();
for (count=0; count < 20; count++)
cout << numbers[count];}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
整数[6];
整数计数=0;
ifstream输入文件;
打开(“Numbers.txt”);
而(计数<20&&inputFile>>数字[6]){
计数++;
inputFile.close();
用于(计数=0;计数<20;计数++)
cout这是从第一个文件读取数字的示例。可以将此示例添加到,以从第二个文件读取数字,并将其与从第一个文件读取的数字进行比较
更新:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int numbers[6];
int count = 0;
ifstream inputFile;
inputFile.open("Numbers.txt");
// get each number from the file until the end-of-file bit
// is retrieved.
while ((inputFile >> numbers[count]) && (count < 6)){
// iterate count by one
count++;
}
inputFile.close();
// run through the array of numbers and ouput each index
for(int i = 0; i < 6; i++) {
cout << "numbers[" << i << "] = " << numbers[i] << "\n";
}
}
输出:cout
更新:
错误输入:
输出
您的主要问题是在循环中执行不应该存在的操作。循环应该为从文件中读入的每个值运行一次。完成后,您应该关闭文件并打印结果
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int numbers[6];
int count = 0;
ifstream inputFile;
inputFile.open("Numbers.txt");
if(!inputFile.is_open()) // always check for errors
{
std::cerr << "ERROR opening input file:" << std::endl;
return 1; // error
}
// make sure count < 6 so you don't overflow your array
while(count < 6 && inputFile >> numbers[count])
{
count++;
// inputFile.close(); // don't close the file yet!!
//for(count = 0; count < 20; count++) // don't output yet!!
// cout << numbers[count];
}
// now close your file and output what you have
inputFile.close();
for(count = 0; count < 6 /* not 20!! */; count++) // don't output yet!!
cout << numbers[count] << '\n';
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
整数[6];
整数计数=0;
ifstream输入文件;
打开(“Numbers.txt”);
如果(!inputFile.is_open())//始终检查错误
{
再次检查你的代码和这本书…如果你有一本书,它应该解释得很清楚。我还建议你改用Rd(data.in)对于输入文件。也请把准确的代码放出来,以便我们可以向您解释,如果您仍然不理解它。另外,顺便说一句,对于第二个文件,我猜您是通过创建一个新文件来输入的?(不确定)没有太多解释,只是这个代码,我逐个字符地查看了它,我没有看到任何区别(当然除了文件名之外,但这肯定是正确的)。不知道怎么回事。你所说的Rd(data.in)是什么意思?inputFile>>number[6]
你不应该在eof()
上循环:getline()
也不是这样工作的。这段代码在inputFile.getline()上给我“没有重载函数的实例”错误@Galik从来不知道关于eof()
,我在学校的教授甚至教我们在while循环中测试eof,我可能应该要求退款
错误是因为最近在javaScanner
课程中工作。从我所看到的情况来看,大学里有很多教授在教授坏习惯。我强烈建议你获得一些推荐阅读。按照教授的指示去做,但要从专家那里学习如何正确地做事。好吧,我得到了什么现在是:数字[0]=-858993460,数字[1]=-858993460,等等。
numbers[0] = 2
numbers[1] = 3
numbers[2] = 4
numbers[3] = 5
numbers[4] = 7
numbers[5] = 8
d
3
4
5
7
8
numbers[0] = 0
numbers[1] = 0
numbers[2] = -1527900896
numbers[3] = 32627
numbers[4] = -1527901752
numbers[5] = 32627
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int numbers[6];
int count = 0;
ifstream inputFile;
inputFile.open("Numbers.txt");
if(!inputFile.is_open()) // always check for errors
{
std::cerr << "ERROR opening input file:" << std::endl;
return 1; // error
}
// make sure count < 6 so you don't overflow your array
while(count < 6 && inputFile >> numbers[count])
{
count++;
// inputFile.close(); // don't close the file yet!!
//for(count = 0; count < 20; count++) // don't output yet!!
// cout << numbers[count];
}
// now close your file and output what you have
inputFile.close();
for(count = 0; count < 6 /* not 20!! */; count++) // don't output yet!!
cout << numbers[count] << '\n';
return 0;
}