reading and writing integers
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int integerInput[10];
int integerOutput[10];
fstream input_output;
// assign random values to the array
for(int i = 0; i < sizeof(integerInput) / sizeof(int); i++)
{
integerInput[i] = rand() % 100;
}
// output the contents of the array to the console for all to see
for(i = 0; i < sizeof(integerInput) / sizeof(int); i++)
{
cout << integerInput[i] << ' ';
}
cout << endl;
// begin writing the contets of the array into a file
input_output.open("test.dat", ios::out);
for(i = 0; i < sizeof(integerInput) / sizeof(int); i++)
{
input_output << integerInput[i] << ',';
}
input_output.close();
// start the reading of variables from a file
input_output.open("test.dat", ios::in);
i = 0;
while(!input_output.eof())
{
input_output >> integerOutput[i++];
}
input_output.close();
// prints out the contents of the values that were read from a file
for(i = 0; i < sizeof(integerInput) / sizeof(int); i++)
{
cout << integerOutput[i] << ' ';
}
return 0;
}I did this once, but forgot how to do this specific task. Pretty much what I'm trying to do is write in a bunch of integer values into a file (done) from an array and then read it back out to a different integer array and then show that array to the user on the console (in progress...) Whenever I try to read all of those values out, I get the first array output to the console and then the nothing, the program seems to just stop there :confused: . Any ideas why this is happening or how I can read out those values out of the file and input them into an integer array? Thanks.
# 1 Re: reading and writing integers
You forgot the separator:
...
// start the reading of variables from a file
input_output.open("test.dat", ios::in);
i = 0;
char sep;
while(!input_output.eof())
{
input_output >> integerOutput[i++]>>sep;
}
input_output.close();
...
}
# 2 Re: reading and writing integers
Thanks bud.
# 3 Re: reading and writing integers
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
fstream input_output;
char sep;
double doubleInput[10];
double doubleOutput[10];
// assign random values to the array
for(int i = 0; i < sizeof(doubleInput) / sizeof(double); i++)
{
doubleInput[i] = (double)(i * 5.0);
}
// output the contents of the array to the console for all to see
for(i = 0; i < sizeof(doubleInput) / sizeof(double); i++)
{
cout << doubleInput[i] << ' ';
}
cout << endl;
// begin writing the contets of the array into a file
input_output.open("test.txt", ios::out);
for(i = 0; i < sizeof(doubleInput) / sizeof(double); i++)
{
input_output << doubleInput[i] << ',';
}
input_output.close();
// start the reading of variables from a file
input_output.open("test.txt", ios::in);
i = 0;
while(!input_output.eof())
{
input_output >> doubleOutput[i++] >> sep;
}
input_output.close();
// prints out the contents of the values that were read from a file
for(i = 0; i < sizeof(doubleOutput) / sizeof(double); i++)
{
cout << doubleOutput[i] << ' ';
}
cout << endl;
return 0;
}Gab, how would you do the same thing for doubles. In this instance, the only thing that gets printed out is just a bunch of nonsense values. I checked the text file where they are stored and it's empty :confused: . The code is almost exactly the same as the above one for integers, so I guessed that "test.txt" should contain atleast something :shrug: .
# 4 Re: reading and writing integers
Hummm, you must be doing something else wrong (e.g. test.txt still open in some editor that locks it or so). Your code works perfectly for me (VC++7.0).
# 5 Re: reading and writing integers
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
fstream input_output_dou;
char sep;
double doubleInput[10];
double doubleOutput[10];
// assign random values to the array
for(int i = 0; i < sizeof(doubleInput) / sizeof(double); i++)
{
doubleInput[i] = (double)(i * 5.0);
}
// output the contents of the array to the console for all to see
for(i = 0; i < sizeof(doubleInput) / sizeof(double); i++)
{
cout << doubleInput[i] << ' ';
}
cout << endl;
// begin writing the contets of the array into a file
input_output_dou.open("test2.txt", ios::out);
for(i = 0; i < sizeof(doubleInput) / sizeof(double); i++)
{
input_output_dou << doubleInput[i] << ',';
}
input_output_dou.close();
// start the reading of variables from a file
input_output_dou.open("test2.txt", ios::in);
i = 0;
while(!input_output_dou.eof())
{
input_output_dou >> doubleOutput[i++] >> sep;
}
input_output_dou.close();
// prints out the contents of the values that were read from a file
for(i = 0; i < sizeof(doubleOutput) / sizeof(double); i++)
{
cout << doubleOutput[i] << ' ';
}
cout << endl;
return 0;
}Strange. This worked as well. I changed the name of the stream values and it worked out. Maybe you're right about not closing a stream or something :shrug: . Because before I used the same stream to read and write integers and doubles, I didn't think that there would be a difference, oh well.
