473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Openning a file while in an object

DJ
I have been attempting to move some code that i have into an object from my
main program. Everything is working fine until i try to open an ifstream in
the object. since i want the file access across the object i have declared
it as follows:

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

/////////////////////////////////////////////////////////////////
elsewhere....in main()

myclass f("in.txt");

//////////////////////////////////////////////////////////////////

output for this instance of the myclass will cause:

ERROR

Is there something i am missing? Does it need to be static or something?
Like i said the code worked in the main program just fine.

Thanks for any help.

David
Jul 22 '05 #1
6 1157

"DJ" <ch*****@earthlink.net> wrote in message
news:iw*****************@newsread1.news.pas.earthl ink.net...
I have been attempting to move some code that i have into an object from my
main program. Everything is working fine until i try to open an ifstream
in
the object. since i want the file access across the object i have declared
it as follows:

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

/////////////////////////////////////////////////////////////////
elsewhere....in main()

myclass f("in.txt");

//////////////////////////////////////////////////////////////////

output for this instance of the myclass will cause:

ERROR

Is there something i am missing? Does it need to be static or something?
Like i said the code worked in the main program just fine.


No there is nothing wrong with the code (although I could suggest an
improvement or two). Your file must be failing to open for all the usual
reasons. Mostly likely being that the file doesn't exist.

The following program runs without any error message for me (in.txt exists
of course).

#include <iostream>
#include <fstream>
using namespace std;

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

int main()
{
myclass f("in.txt");
}

john
Jul 22 '05 #2
DJ
Thank you,
i dont know what is going on but since i installed .net 2003, i have been
having a lot of wierd errors. I closed the editor a bit ago and when i
openeed it again it ran ok. And now i can't get strings to work, so i
thought ah well maybe i should just reinstall version 6 of Visual Studio,
but i cant seem to load it over .NET, so im forced to dealwith BAD C++
instead of real c++.

See my latest post

Subject:Trouble with Strings

David

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

"DJ" <ch*****@earthlink.net> wrote in message
news:iw*****************@newsread1.news.pas.earthl ink.net...
I have been attempting to move some code that i have into an object from my main program. Everything is working fine until i try to open an ifstream
in
the object. since i want the file access across the object i have declared it as follows:

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

/////////////////////////////////////////////////////////////////
elsewhere....in main()

myclass f("in.txt");

//////////////////////////////////////////////////////////////////

output for this instance of the myclass will cause:

ERROR

Is there something i am missing? Does it need to be static or something?
Like i said the code worked in the main program just fine.


No there is nothing wrong with the code (although I could suggest an
improvement or two). Your file must be failing to open for all the usual
reasons. Mostly likely being that the file doesn't exist.

The following program runs without any error message for me (in.txt exists
of course).

#include <iostream>
#include <fstream>
using namespace std;

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

int main()
{
myclass f("in.txt");
}

john

Jul 22 '05 #3

"DJ" <ch*****@earthlink.net> wrote in message
news:S%****************@newsread3.news.pas.earthli nk.net...
Thank you,
i dont know what is going on but since i installed .net 2003, i have been
having a lot of wierd errors. I closed the editor a bit ago and when i
openeed it again it ran ok. And now i can't get strings to work, so i
thought ah well maybe i should just reinstall version 6 of Visual Studio,
but i cant seem to load it over .NET, so im forced to dealwith BAD C++
instead of real c++.


I use .NET, never had any problems with it. Are you sure you are picking up
the correct header files?

john
Jul 22 '05 #4
"DJ" <ch*****@earthlink.net> wrote in message news:<iw*****************@newsread1.news.pas.earth link.net>...
I have been attempting to move some code that i have into an object from my
main program. Everything is working fine until i try to open an ifstream in
the object. since i want the file access across the object i have declared
it as follows:

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

/////////////////////////////////////////////////////////////////
elsewhere....in main()

myclass f("in.txt");

//////////////////////////////////////////////////////////////////

output for this instance of the myclass will cause:

ERROR

Is there something i am missing? Does it need to be static or something?
Like i said the code worked in the main program just fine.

Thanks for any help.

David


While I think there could be some minor changes made to your code, it
looks correct to me and even works. Are you running this in the same
place as you ran the original program
Jul 22 '05 #5
DJ
Well the 2002 version didnt give me any problems but the 2003 has a bit and
2005 crashes for me, so there is the hierarchy:
6 is stable
2002 works but is limited,
2003 is inconsistant
2005 crashes but looks promissing

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

"DJ" <ch*****@earthlink.net> wrote in message
news:S%****************@newsread3.news.pas.earthli nk.net...
Thank you,
i dont know what is going on but since i installed .net 2003, i have been having a lot of wierd errors. I closed the editor a bit ago and when i
openeed it again it ran ok. And now i can't get strings to work, so i
thought ah well maybe i should just reinstall version 6 of Visual Studio, but i cant seem to load it over .NET, so im forced to dealwith BAD C++
instead of real c++.

I use .NET, never had any problems with it. Are you sure you are picking

up the correct header files?

john

Jul 22 '05 #6

"DJ" <ch*****@earthlink.net> wrote in message
news:HT***************@newsread3.news.pas.earthlin k.net...
Well the 2002 version didnt give me any problems but the 2003 has a bit
and
2005 crashes for me, so there is the hierarchy:
6 is stable
2002 works but is limited,
2003 is inconsistant
2005 crashes but looks promissing


I've used 2002 and 2003, no problems at all.

Have you tried using the debugger to find out exactly what was going wrong?

On the face of it I would blame your installation. This is pretty basic
stuff that isn't working for you.

john
Jul 22 '05 #7

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

Similar topics

19
by: Mel | last post by:
when downloading files from my site, when file types are known (i.e *.doc) browsers open the file for viewing. is there a way to disable that and just present the save as dialog (same as for...
5
by: Tedmond | last post by:
Dear all, How can I get all the titles of all openning windows? I found a API in win32 that called EnumWindows() but it returns only the windows handles, not titles. How can I get the list just...
4
by: Ohad Young | last post by:
Hi, I need to open (launch) an external application from my winform application. The application is not a dot.net application, for example the windows calculator. I'm using the...
2
by: andreguerra | last post by:
Hello. Im using a Console (Operator Panel) that has a built in web server. Im triyng to make a page that opens a file in the console server. Im using this code: var datafile = "DEFAULT.SKV"; var...
9
by: bj19138 | last post by:
How do you have your browser maximized when openning. When I open my Browser it always is minimized.?
1
by: mahesh123 | last post by:
Hi, I am using the VB6.0 and for report crystal reports 8.0. the reports are not openning(means its openning but immediately it is closed the report) when we installed the reports in other system....
34
by: xiao | last post by:
Does anyone tried to open a file with extension of .lat using C? why I cannot open these files using fopen? Any other way that I can try to open it? Thank you~
15
by: r90slash6 | last post by:
Hi Guys, I have code for click event on an Access form that opens an Excel file, refreshes data of a pivot table on the active worksheet and then filters records based on a criteria. Everything...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.