I need code to open up a file
Thank you for your help
I need code to open up a file
Thank you for your help
include <fstream>
main()
{
ifstream inputfile("inp.txt"); // for input
ofstream outputfile("out.txt"); // for output
fstream both("both.txt", ios::in | ios:ut) // for input output.
}
Thank You
But i was also looking for the code to put each line into a data array once opened
// just a simple class
class sth {
public:
int id;
int year;
}
main()
{
ifstream file("file1.txt", ios::in);
if (file.fail()) {
cout << "file cannot be opened";
return 0;
}
sth *array = new sth[10];
// input ten sth from the file. I assume there are ten records
// in file
for (i = 0; i < 10; i++) {
file >> array[i].id;
file >> array[i].year;
}
file.close();
ofstream file2("file2.txt", ios:ut);
if (file2.fail()) {
cout << "file cannot be opened";
return 0;
}
// writes the array back to another file.
for (i = 0; i < 10; i++) {
file2 << array[i].id;
file2 << array[i].year;
}
file2.close();
delete [] array;
return 0;
}
Another ex:
main()
{
sth *array = new sth[40];
ifstream file("file");
while (!file.eof()) {
file >> array[i].id;
if (!file.eof())
file >> array[i].year;
i++;
}
file.close();
delete [] array;
}