Working with files

12/06/2012 16:28 o0Zero0o1#1
Hiya

If i have for example

Code:
#include<iostream.h>
int main()
{
	int s1=2;
	int p1=3;
	int S,P;

	s1+=1;
	p1+=1;
	S=s1;
	P=p1;
	cout<< "This is S:" <<S<<endl;
	cout<<"This is P:"<<P<<endl;
	return 0;
}
How can i open file,write the 'S' and 'P' into it,close it , than open it and print the data in it?

By far all i can do is :
using the fstream library
Code:
ofstream file_name;
        file_name.open ("file_name.txt");
		file_name << "The S is:  "<<S<<"\n The P is:   "<<P<<"";
		file_name.close();
thanks in advance
12/07/2012 22:12 dreamdok#2
I used stdio...not a fan of iostream :P. heres a quick solution that you may be in favor of:

Code:
#include <stdio.h>

void main()
{

//initialise our FILE descriptor variable
FILE *f = NULL;

//Our variables
	int s1=2;
	int p1=3;
	int S,P;

//variables for use when opening the file...
	int written_S = 0;
	int written_P = 0;


//open the file "filename.txt", for reading and writing.
f = fopen("FILENAME.txt","w");



	s1+=1; 
	p1+=1; 
	S=s1; // S will now equal 3
	P=p1; // P will now equal 4

	//Lets print our variables, to check what they are, 
	//the variable-argument %i means integer, and S will be put here
	//the same happens to P
	printf("s equals %i\n", S); //S equals 3
	printf("p equals %i\n", P);  // P equals 4

	//This prints the two integers.
	fprintf(f, "%i%i\n", S,P); //34 will be printed in the file.

	fclose(f);

	// So now we have written our file, lets reopen it and find what we have written
	f = fopen("FILENAME.txt","r");

	//lets scan the file for two integers together %1d means SINGLE DIGIT
	//store them in written_S, and written_P
	fscanf(f,"%1d%1d\n",&written_S, &written_P);

	//Lets print the variables we have found
	printf("The found variables are: %i and %i!\n", written_S, written_P);

	fclose(f);
}
12/07/2012 22:24 o0Zero0o1#3
Yeah but i'm sort of avoiding the stdio , and gotta be using iostream ... or fstream?Do you have any clue how to do the same using fstream?

BTW: Nice explanation,very detailed,thanks for sparing time.
12/07/2012 23:32 dreamdok#4
Heres one that uses the iostream library...another quick example with a workaround.

Code:
#include <iostream>
#include <fstream>

using namespace std;
void main()
{

//initialise our FILE descriptor variable
ofstream myfile_write;
ifstream myfile_read;

//Our variables
	int s1=2;
	int p1=3;
	int S,P;


//open the file "filename.txt", for reading and writing.
myfile_write.open("FILENAME.txt");



	s1+=1; // S will now equal 3
	p1+=1; // P will now equal 4
	S=s1;
	P=p1;

	//Lets print our variables, to check what they are, 
	cout << "s equals " << S << endl; //S equals 3
	cout << "p equals " << P << endl;  // P equals 4

	//This prints the two integers to the file
	myfile_write << S;
	myfile_write << P;


	//close the file
myfile_write.close();

char digit_as_character[2];
int  digit_as_integer[2];


myfile_read.open("FILENAME.txt");


//This reads two characters then converts them into integers.
for(int i = 0; i<2; i++)
{
	if(!myfile_read.eof())
	{
	digit_as_character[i] = myfile_read.get();
	digit_as_integer[i] = atoi(&digit_as_character[i]);
	}else{
		cout << "failed to read?" << endl;
	}

}


	//Lets print the variables we have found
cout <<"The found variables are " << digit_as_integer[0] << " and " << digit_as_integer[1] << endl;

}