I need to be able to save and open files for a simple boot-up text editor. Can someone tell me how to use the header files ifstream.h and ofstream.h ?
Printable View
I need to be able to save and open files for a simple boot-up text editor. Can someone tell me how to use the header files ifstream.h and ofstream.h ?
Here's a simple tutorial from GameDev,
http://www.gamedev.net/reference/art...rticle1127.asp
Hope it helps!:)
<fstream.h> is deprecated. Use <fstream>
C-Style:
PHP Code:
#include <stdio.h>
int load(char* sz)
{
char szBuffer[1024];
FILE* f = fopen(sz, "r");
if(!f)
return -1;
while(fgets(szBuffer, 1024, f))
{
printf("%s\n", szBuffer);
}
fclose(f);
return 0;
}
int save(char* sz, char* szSave)
{
FILE* f = fopen(sz, "r");
if(!f)
return -1;
fprintf(f, szSave);
fclose(f);
return 0;
}
but he was asking for C++:
Code:#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream in("input.txt");
ofstream out("output.txt");
string str;
char c = in.get();
while(c != EOF)
{
str += c;
cout << c;
out.put(c);
c = in.get();
}
cout << endl;
cout << str << endl;
return 0;
}