If i were to make a program, or have a piece of a program, write numbers to a file, and lets say I close the program. The program should print the numbers when someone starts a program.
So what i want the program to do is :
[program is opened, (by someone)]
[information, if any is there, is printed]
...code...
[numbers are written to a file]
[program is closed, (by someone)]
I've tried to do this before multiple times, but the only catch is trying to both write and read from a file in the same program
any help would be nice, thanks
[edit] the problem has been solved, thanks to Moschops for all that help, you rok d00d [/edit]
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
usingnamespace std;
int main () {
int x;
int y;
x = 44;
y = 51;
char bufferx [33];
char buffery [33];
itoa (x,bufferx,10);
itoa (y,buffery,10);
ifstream inputFile ("example.txt");
if (inputFile.is_open()){
cout << bufferx << endl;
cout << buffery << endl;
}
else cout << "Unable to open file";
x = 47;
y = 52;
ofstream outputFile ("example.txt");
if (outputFile.is_open())
{
outputFile << bufferx << endl;
outputFile << buffery << endl;
outputFile.close();
}
else cout << "Unable to open file";
return 0;
}
is the code I have now(sorry for it being so space consuming) , slightly edited it to make it print values before writing values to a file. There is one problem, from this code, as many of you may know, printing bufferx and buffery before they are declared is a bad thing, so I was wondering how I could separately print line one (which would be bufferx) and line two (which would be buffery) of the file before declaring x and y. Also, can the two values not be printed with a loop? thanks again, I appreciate all of your help!