Re: [suse-programming-e] reading a file and display its content with cout
do you mean like this: " char mybuffer[]; fstream sumber("smb.conf"); if (!sumber){ cout << "Failed in opening the 'smb.conf' file\n"; exit(1); } sumber >> mybuffer; cout << mybuffer << endl; "? Please tell me more details. Could you give me a very simple sample? Thank you very much for your generousity. --- Alan Lenton <alan@ibgames.com> wrote:
Dear my friend.
I want to make a linux server administrating tool ( I am also a beginner in GNU C++).
if I open the file (smb.conf) with ifstream and
Prabu Subroto wrote: try to
display its content with cout, but it does not display its content only somewhat looksline only an address of the variable in the memory. like this: " psubroto@vh64:~/arsip/proyek/g++/BLiSS> ./liss Choose the server you would like to administrate : 1. Samba Server 1 Samba Server Administrating Menu --------------------------------
Workgroup name :bitp nama workgroup-nya : bitp 0xbfffef64 psubroto@vh64:~/arsip/proyek/g++/BLiSS> "
what is my mistake? [snip...]
void bsamba::menu(){ cout << "Samba Server Administrating Menu\n"; cout << "--------------------------------\n" << endl; cout << "Workgroup name :"; cin >> bwg;
cout << "nama workgroup-nya : " << bwg << endl; fstream sumber("smb.conf"); if (!sumber){ cout << "Failed in opening the 'smb.conf' file\n"; exit(1); } cout << sumber << endl; }
[snip...]
What you have asked it to print out is not the file, but the address of the stream. You need to use the stream (sumber) to read the file into a buffer and then write the buffer out to cout :)
alan
__________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. http://info.mail.yahoo.com/mail_250
do you mean like this: " char mybuffer[]; fstream sumber("smb.conf"); if (!sumber){ cout << "Failed in opening the 'smb.conf' file\n"; exit(1); } sumber >> mybuffer; cout << mybuffer << endl; "?
Please tell me more details. Could you give me a very simple sample? yes, but you are not allocating space for mybuffer. You must declare a size. In the case below, I create a file, test.dat, but you could use smb.conf Nore that the >> operator reads a word. If you want to read a line, use the getline() member function. Here is an example, below. #include <iostream> #include <fstream> using std::cin; using std::cout; using std::cerr; using std::fstream; using std::endl; using std::ios;
On Monday 31 January 2005 10:00, Prabu Subroto wrote: main () { fstream file1; char mybuffer[512]; file1.open( "/etc/samba/smb.conf", ios::in); if (!file1) { cerr << "Unable to open test.dat." << endl; exit(1); } while( !file1.eof() ) { file1.getline(mybuffer, sizeof(mybuffer)); cout << mybuffer << endl; } file1.close(); } -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
participants (2)
-
Jerry Feldman
-
Prabu Subroto