Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 20:14

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Working with files

Discussion on Working with files within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
o0Zero0o1's Avatar
 
elite*gold: 0
Join Date: Feb 2010
Posts: 117
Received Thanks: 15
Working with files

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
o0Zero0o1 is offline  
Old 12/07/2012, 22:12   #2
 
elite*gold: 0
Join Date: Oct 2005
Posts: 4
Received Thanks: 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);
}
dreamdok is offline  
Thanks
1 User
Old 12/07/2012, 22:24   #3
 
o0Zero0o1's Avatar
 
elite*gold: 0
Join Date: Feb 2010
Posts: 117
Received Thanks: 15
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.
o0Zero0o1 is offline  
Old 12/07/2012, 23:32   #4
 
elite*gold: 0
Join Date: Oct 2005
Posts: 4
Received Thanks: 2
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;

}
dreamdok is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
Working v15-16-17 files
02/27/2011 - Flyff Trading - 0 Replies
: as the title says I am selling unbugged working files with source edits including security to prevent PE and CE plus having all the instances working items unbugged and working maps none crashing and monsters too. I'm working on adding more features to the game and When bought I can also give updates of the work I am doing to the files for your server and i can edit the source files including level cap for your servers needs. Updates can include the 3rd jobs when i finish adding them to...
THE 100% WORKING BOT FILES AS OF MAY 15
05/16/2007 - Silkroad Online - 35 Replies
Animus's two downloadable files SOFTMOD http://rapidshare.com/files/31372440/SoftM...Mod2 _May_v4.rar BOT 1.62 http://rapidshare.com/files/30417731/sroboten1.62 .exe
.rar files not working for me
08/30/2006 - Conquer Online 2 - 5 Replies
why won't .rar files open for me ? :sex:



All times are GMT +2. The time now is 20:14.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.