GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-May-2007, 21:09
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road
Angry

Problem write to file


Hello all expert programmer, i have a C++ program where write the record to a file but after i executed the program, the file cannot be opened by this statement ofstream outtpfile("D:\\Test.txt, ios:ut); but can opened by this statement:
outtofile.open("D:\\Test.txt, ios:ut))


Below is my code:

CPP / C++ / C Code:

#include<iostream>
#include<fstream>
using std::fstream;
using std::ofstream;
#include<cassert>
#include<string>
using namespace std;

int main(int argc, char *argv[])
{
        int account;
	string name;
	float balance;

	ofstream outtofile("C:\\Test.txt", ios::out);
	/* Default constructor - to open the 
	   file and create object simulatenously
    */

	//outtofile.open("D:\\Test.txt", ios::out);
    // The file can be opened if th estatement above
	// is not a comment. 
	// Anothe method to open a file
    

	//assert(!outtofile);
	if (!outtofile)
	{
		cout << "Enter the accout, name and "
			 << "balance ";
		cout << "\n\nAccount\tName\tBalance\n";
		outtofile << "Account\tName\tBalance\n\n";	
		while(cin >> account >> name >> balance)
		{// End by Ctrl + z
			outtofile << account << "\t"
				      << name << "\t"
					  << balance << "\n";
		}
	}
	else
	{
		cerr << "File could not be opened";
		abort();
	}

	outtofile.close();
	return 0;
}

		


I looking for someone who kind enough to help me.

Thanks for your help.

Your help is greatly appreciated by me and others.
Last edited by LuciWiz : 10-May-2007 at 06:05. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 09-May-2007, 21:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem write to file


Quote:
Originally Posted by Peter_APIIT

Below is my code:

CPP / C++ / C Code:


    if (!outtofile)
    {
        cout << "Enter the account, name and balance ";
.
.
.
    }
    else
    {
        cerr << "File could not be opened" << endl;
        abort();
    }	
Your logic is backwards: The test for the state of the stream returns "false" if it is invalid. So:

CPP / C++ / C Code:
    if (outofile) {
        // this is the stuff if it is OK
    }
    else {
        // this is the stuff if it is bad
    }


Regards,

Dave
  #3  
Old 09-May-2007, 23:16
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road

Re: Problem write to file


I don't understand this statement :
if (!outtofile) - ?

In C,
CPP / C++ / C Code:
if (!fp+NULL) -- The pointer return by file pointer is not NULL
{
       Enter account;
}
else
{
      perror("Error");
}

Thanks for your explanation.
Last edited by LuciWiz : 10-May-2007 at 06:05. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #4  
Old 09-May-2007, 23:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem write to file


Quote:
Originally Posted by Peter_APIIT
I don't understand this statement :
if (!outtofile) - ?

For an object of the ofstream class, the program knows the state of the stream by the results of the if() statement.

The expression if(outtofile) evaluates as "false" if the stream is not OK, so the expression if(!outtofile) evaluates as "true" if the stream is not OK.

Notice tha there are several different tests to accomplish pretty much the same thing. Some people prefer more descriptive alternatives:

CPP / C++ / C Code:
    if (outtofile.fail()) { // same as if (!outtofile)
        // this stuff if it's bad
    }
    else {
        // this stuff if it's OK
    }

or

CPP / C++ / C Code:
    if (!outtofile.fail()) { // same as if(outtofile)
        // this stuff if it's good
    }
    else {
        // this stuff if it's bad
    }

These tests can (should) be done not only after attempts to open the file, but after attempts to read from the file. The stream will enter a fail state if an attempt to read from the file fails (trying to use >> to read an int when the next chars in the file were not digits, or trying to read past the end of the file).


Quote:
Originally Posted by Peter_APIIT
In C,
if (!fp+NULL) -- The pointer return by file pointer is not NULL

If fp is a pointer to FILE, then the expression if (fp == NULL) after an attempt to open the file evaluates to "true" if the file wasn't opened successfully. In C that expression is exactly equivalent to if(!fp).

Or, you could say if(fp) to mean the same thing as if (fp != NULL) and either one of these would evaluate to "true" if the file was opened OK.



So, in C you could have

CPP / C++ / C Code:
    if (fp) { /* same as if (fp != NULL) */
        /* this stuff if the file was opened successfully */
    }
    else {
        /* this stuff if the file was not opened successfully */
    }

etc.

Regards,

Dave
  #5  
Old 10-May-2007, 02:26
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road
Smile

Re: Problem write to file


CPP / C++ / C Code:
If (!outtofile.fail())
{
     // Doing good stuff
}
else
{
    // Doing bad stuff
}
I prefer this method because it's easier to understand.


Thanks for your help and patient. A million thanks to you.

Hope GOD will blessed you.
Last edited by LuciWiz : 10-May-2007 at 06:06. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #6  
Old 10-May-2007, 08:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem write to file


Quote:
Originally Posted by Peter_APIIT
CPP / C++ / C Code:
If (!outtofile.fail())
{
     // Doing good stuff
}
else
{
    // Doing bad stuff
}
I prefer this method because it's easier to understand.
I think you are right, and I probably should have showed this first. The other ways are correct and you will see them in lots of published code.

C and C++ programmers sometimes get lazy and use notation that requires less typing, even though it is not necessarily the "best" style for presenting to other people. Getting used to other people's code is, in many respects, harder than learning the parts of the language that seem "natural" to you.

Regards,

Dave
  #7  
Old 10-May-2007, 23:05
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road

Re: Problem write to file


Thanks for your replies. Hope GOD will blessed you.
__________________
Linux is the best OS in the world.
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
File write / read problem Tomb332 CPP / C++ Forum 2 12-Jul-2006 01:15
CGI write to file problem DReaMer66 Apache Web Server Forum 1 16-Mar-2005 19:23
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.