473,796 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Open a file with a constructor

I am trying to open a file in the classes contructor. The file is
created and the program even thinks it is open and tries to write to
it with no errors, but when I look at the file it is zero
bytes..nothing was written to it. Here is part of my code if anyone
can help me. I do appreciate it.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "stdio.h"

using namespace std;

class file_handler
{
private:
ofstream outfile;
public:

char InventoryNumber[16];
char ProductName[21];
char ProductCode[4];
int OnHandQuantity;
int BackOrderQuanti ty;
int OnOrderQuantity ;
float InventoryEvalua tion;
float AverageCost;
float SellingPrice;

file_handler();
file_handler(st ring file);
~file_handler() ;
int write_file();
file_handler::~ file_handler()
{
outfile.close() ;
}

file_handler::f ile_handler(str ing file)
{
outfile.open(fi le.c_str(),ios: :out|ios::app|i os::binary);
}

file_handler::w rite_file()
{
//writing file stuff
}

int main()
{
file_handler newfile("invent ory.dat")
}
Jul 22 '05 #1
1 3929

"Brian" <co*****@gmail. com> wrote in message
news:6a******** *************** **@posting.goog le.com...
I am trying to open a file in the classes contructor. The file is
created and the program even thinks it is open and tries to write to
it with no errors, but when I look at the file it is zero
bytes..nothing was written to it. Here is part of my code if anyone
can help me. I do appreciate it.


[snip]

No, there is no problem with the code you've posted that I can see. The
problem is probably somewhere in the code you haven't posted. Perhaps you
never get to call write_file, or perhaps there is a bug in write_file.

If you want problems with code fixed, it is always a good idea to post
complete programs, preferably cut down to the part that isn't working.

john
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3427
by: tornado | last post by:
hi all, This is quite puzzling me why is that ofstream constructor is unable to open a file for writing while, a ofstream "open" method can ! The c++ book by Herbert Schildt which i m readin mentions that ofstream default constructor can open a file. But when i tried to open a file it failed to open. Then i tried it by open method which succeded. I m using g++ on Redhat linux. Any insights will be appreciated.
4
6803
by: Otik | last post by:
Hi, I'm trying to open a file with read access only, e.g. FileStream fs = new FileStream("C:\foo.txt", FileMode.Open, FileAccess.Read); The file is open by another process and the FileStream constructor throws an IOException. I thought that it was possible to open a file with read access even if it's being used by another process. Is that not true? Thanks, Otik
8
1791
by: Peter A. Schott | last post by:
Been reading the docs saying that file should replace open in our code, but this doesn't seem to work: # Open file for writing, write something, close file MyFile = file("MyFile.txt", "w") MyFile.write("This is a test.") MyFile.close() However, using: MyFile = open("MyFile.txt", "w")
115
14193
by: TheAd | last post by:
At this moment I use MsAccess and i can build about every databound application i want. Who knows about a serious open source alternative? Because Windows will be a client platform for some time, i prefer a solution that (also) supports Windows. On the net I found a number of products that i looked at, but none of them gave me the impression of a serious candidate at this moment (KNoda, Gnome DB Manager, InterBase...). 2 additional...
3
34544
by: trellow | last post by:
Hello, I am writing an application that needs to read a file that is already open by another process for writing. When I do the following: FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); I get the following exception:
9
13430
by: Charles F McDevitt | last post by:
I'm trying to upgrade some old code that used old iostreams. At one place in the code, I have a path/filename in a wchar_t string (unicode utf-16). I need to open an ifstream to that file. But the open() on ifstream only takes char * strings (mbcs?). In old iostreams, I could _wopen() the file, get the filedesc, and call attach() on the ifstream.
7
1257
by: Drew | last post by:
I have a small VB.NET application that captures left clicks, right clicks and key presses and automatically logs them to a csv file every 1 minute. The issue is that if I want to look at the log file, I get an error because the app is trying to write to the log file and it is open. How can I circumvent this issue? Thanks, Drew
5
11224
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end. Ocz, if the file is not exist(e.g. another day), it will creates the file first.
4
2094
by: nickyeng | last post by:
i have main program, which user key in the filename in command line argument. i pass the argument to another class constructor to handle it. I have the filename correct, but it still give me "can't open terrain file" message. here is my code: Constructor(char* filename =""){ ifstream terrainfile; terrainfile.open(filename, ios::in);
20
5624
by: Author | last post by:
a .net 1.1 app has a class whose constructor opens a db connection to sql svr two thousand. this class has more than a dozen of methods. most of them don't do db stuff. I am wondering if this design is going to be a problem, bcoz each time this class is instantiated, a db conn is open. The worst thing is that I haven't seen anywhere in the code the db conn is closed. I write about this bcoz I see that this app leaves more than one...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.