Write a File and Load It

Posts 1–8 of 8 · Page 1 of 1
Write a File and Load It
Hello I need help to write a file containing a variable and at start load it,
Im suing that but I dont know why dont writes in the File
Thanks!
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
int var;
fstream File("C:\File.txt");
	for (int x=0;x<10;++x){
		cout << "Enter Value " << endl;
		cin >> var; 
		File << var << endl;
	}
	File.close();
}
Quote Originally Posted by alvaritos View Post
Hello I need help to write a file containing a variable and at start load it,
Im suing that but I dont know why dont writes in the File
Thanks!
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
int var;
fstream File("C:\File.txt");
	for (int x=0;x<10;++x){
		cout << "Enter Value " << endl;
		cin >> var; 
		File << var << endl;
	}
	File.close();
}
do:

Code:
ofstream File("C:\File.txt"); 

if( File.good() ){
 File << var << endl;
} else {
 cout<<"Error!"<<endl;
}

that should work, to read files I always do this:

ifstream File(C:\File.txt");

char *buffer;

if(File.good()){
   File.seekg (0, ios::end);
  length = File.tellg();
  File.seekg (0, ios::beg);
  buffer = new char [length];

  File.read (buffer,length);  // buffer = file
  File.close();

} else {
 cout<<"Error"<<endl;
}
Hope that helps
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
	int var;
	ofstream File("C:\File.php");
		if (File.good() ){
			File << var << endl;
		}
		File.close();
}
Dont writes anything,
^ *facepalm*

KK first off the above code 'var' is not initalized so it wont have any value and prob give you a memory error.

2nd when using file paths its important to rmbr that the character '\' is a special character and in your case "C:\File.txt" would be 'C' , ':' , '\f', 'i', 'l', 'e', '.' , 't', 'x', ,'t'. If you are going to use the backslash it must be "C:\\File.txt" or use normal slash "C:/File.txt"

Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
int var;
fstream File("C:/File.txt");
	for (int x=0;x<10;++x){
		cout << "Enter Value " << endl;
		cin >> var; 
		File << var << endl;
	}
	File.close();
}
Quote Originally Posted by zhaoyun333 View Post
^ *facepalm*

KK first off the above code 'var' is not initalized so it wont have any value and prob give you a memory error.

2nd when using file paths its important to rmbr that the character '\' is a special character and in your case "C:\File.txt" would be 'C' , ':' , '\f', 'i', 'l', 'e', '.' , 't', 'x', ,'t'. If you are going to use the backslash it must be "C:\\File.txt" or use normal slash "C:/File.txt"

Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
int var;
fstream File("C:/File.txt");
	for (int x=0;x<10;++x){
		cout << "Enter Value " << endl;
		cin >> var; 
		File << var << endl;
	}
	File.close();
}
Nope, that's wrong, it's not fstream it ofstream output file stream
Quote Originally Posted by .::SCHiM::. View Post
Nope, that's wrong, it's not fstream it ofstream output file stream
Actually SCHiM fstream calls ifstream and ofstream of the same file so you can input/output with the file with only one call.
Thnaks to all it now works,
No, he's correct. fstream inherits from istream and ostream. The default parameter in the constructor provides for both input and output functionality.
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us