PDA

Click to See Complete Forum and Search --> : Open a file


sail3005
Feb 2nd, 2001, 07:36 PM
Ok, i fugured out how to create a .txt file, but what i can't figure out is how to open one. I created the text file by using fstream.

Just something simple, if that is possible, i just would like to be able to open a .txt file from a path that the user enters, and then save the contents of the text file as a string so that i can print it out with cout.

Thanks

Feb 2nd, 2001, 07:49 PM
#include <fstream>
#include <cstdio>
#include <string>
using namespace std;
const READ = 0;
const WRITE = 1;
/*
ios::app -- Opens the file, and allows additions at the end
ios::ate -- Opens the file, but allows additions anywhere
ios::trunc -- Deletes everything in the file
ios::nocreate -- Does not open if the file must be created
ios::noreplace -- Does not open if the file already exists
*/
const ate = 4;
const app = 8;
const trunc = 16;
const nocreate = 32;
const nereplace = 64;
class file


{
public:
file();
~file();
void openfile(string filename, int readwrite, int mode);
void closefile(int readwrite);
void writefile(string text);
void renamefile(string from, string to);
void deletefile(string filename);
string getcontents();
protected:
ifstream ifile;
ofstream ofile;
};
file::file()


{
}
file::~file()


{
}
void file::openfile(string filename, int readwrite, int mode = nocreate)


{
switch (readwrite)


{
case READ:
ifile.open(filename.c_str(), mode);
break;
case WRITE:
ofile.open(filename.c_str(), mode);
break;
}
}
void file::closefile(int readwrite)


{
switch (readwrite)


{
case READ:
ifile.close();
break;
case WRITE:
ofile.close();
break;
}
}
string file::getcontents()


{
string str = "";
char f;
while(ifile.get(f))
str += f;
return str;
}
void file::writefile(string text)


{
ofile << text;
}
void file::renamefile(string from, string to)


{
rename(from.c_str(), to.c_str());
}
void file::deletefile(string filename)


{
remove(filename.c_str());
}


I wrote this to helpp me :)

use it like this:


file thefile;
string thestr;
cout << "filename: ";
cin >> thestr;
thefile.openfile(thestr, READ);
cout << thefile.getcontents();
thefile.closefile(READ);



:D

sail3005
Feb 2nd, 2001, 10:18 PM
thanks dennis. By the way, you saud in a previous post that you had borland c++ builder 5, how can you simply create a console c++ source file using that?

I try creating a new c++ source file, but it needs to be a part of project to compile, and when i add it to a project, even a console wizard, it always creates it as a windows program. Using winmain(). I just want to use main().

sail3005
Feb 2nd, 2001, 10:19 PM
so like in vc++ you would go file->new->c++ Source File

what would be the equivilant to that in borland?

sail3005
Feb 2nd, 2001, 10:33 PM
also, is the cstdio include in vc++, because it said that it couldn't find it.

Feb 2nd, 2001, 10:47 PM
borland would be new->console wizard, and make sure the "console" box is checked...

and try adding .h to cstdio(btw, cstdio is the same as stdio)(btw again, I looked in borlands include folder, and it's cstdio.h, in msvc++ it's cstdio)

sail3005
Feb 2nd, 2001, 11:11 PM
ahhh... Thanks a lot dennis

sail3005
Feb 2nd, 2001, 11:42 PM
hey dennis, i was just fiddling around, and i found this very simple way to open a text file:



#include <fstream.h>
#include <stdlib.h>
const char *FILENAME = "FILE.txt";


int main() {

ifstream fin(FILENAME);
char ch;
while (fin.get(ch))
cout << ch;
fin.close();
system("pause");
return 0;
}



What i am wondering is what is the difference between this short code, and the long code with the class that you gave me? Don't get me wrong, thanks a lot for the help, i am just wondering like what the actual difference is, and if that short code that i got looks OK?

Feb 2nd, 2001, 11:54 PM
not much difference at all,

if you look at the getcontents() function, you will see that the code is very very similar.

mine just handles the whole shebang rather than just reading :)

parksie
Feb 3rd, 2001, 07:21 AM
In this case, Microsoft are definitely correct, and Borland are wrong.

cstdio is the correct name for that header - all the c* headers do is to import them into the std namespace along with the STL.