integers.txt has the following numbers: 1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int numbers[10];
int i;
ofstream outf;
outf.open("integers.txt");
ifstream inf;
inf.open("in.txt");
for(int i = 0; i < numbers[i]; i++);
{
outf >> numbers[10];
}
inf << numbers[10];
outf.close();
inf.close();
return 0;
}
I want the program to output the integers from integers.txt file to an array and from the array to in.txt file. I get the following error: no match for 'operator>>' in 'outf >> numbers[10]'
You have swapped your file stream types.
You want to read in from integers.txt, but you opened an ofstream
on the file. An ofstream
only lets you output to the file, not read from it, and therefore only has the <<
operator defined, not >>
. You want to open an ifstream
on integers.txt so that you can read input from the file, and probably open an ofstream
on in.txt.
ifstream inf;
inf.open("integers.txt");
ofstream outf;
outf.open("in.txt")
//read in from inf (aka integers.txt)
//output to outf (aka in.txt)
You are not using ifstream
and ofsteam
correctly. ifstream
is for reading and ofstream
is for writing the content to the file. However there are some more issues in your code ie
numbers
numbers[10]
) it gives an error that stack around the variable 'numbers' is corrupted
Following code will do the task for you:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int numbers[10] = {0};
int i = 0;
ofstream outf;
outf.open("in.txt");
ifstream inf;
inf.open("integers.txt");
while (inf >> numbers[i])
{
inf >> numbers[i];
outf << " " << numbers[i];
i++;
}
outf.close();
inf.close();
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.