newbie, going crazy: can't open file with ifstream...

G

giff

hi all

this is the code:

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
std::ifstream iff("test.txt");

if (iff.is_open()) printf("Hello World!\n");
return 0;
}


I'm using visual c++ 6 and the file test.txt that is on the same
directory of the program doesn't want to get open = it never prints out
hello world

does anyone know why?
 
V

Victor Bazarov

giff said:
this is the code:

#include <fstream>
#include <iostream>

What's that for?
using namespace std;

What's that for?
int main()
{
std::ifstream iff("test.txt");

if (iff.is_open()) printf("Hello World!\n");
return 0;
}


I'm using visual c++ 6 and the file test.txt that is on the same
directory of the program doesn't want to get open = it never prints out
hello world

does anyone know why?

It can't find your 'test.txt'. Are you sure it is where the program is
looking for it? Try this:

...
std::ifstream iff(__FILE__);

instead. See what happens. Most likely the program looks for that file
not where you think it does.

V
 
R

roberts.noah

giff said:
hi all

this is the code:

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
std::ifstream iff("test.txt");

if (iff.is_open()) printf("Hello World!\n");
return 0;
}


I'm using visual c++ 6 and the file test.txt that is on the same
directory of the program doesn't want to get open = it never prints out
hello world

does anyone know why?

Try running from the command line instead of inside the devel
environment and see if the problem goes away. Also keep in mind that
the exe will actually be in some subdirectory of the project directory
unless you set it to be someplace else.

If that fixes the problem then it is a case of the environment setting
the current working directory to something you don't expect.
 
G

giff

Victor Bazarov ha scritto:
What's that for? nothing



What's that for? nothing



It can't find your 'test.txt'. Are you sure it is where the program is
looking for it? Try this:

...
std::ifstream iff(__FILE__);

instead. See what happens. Most likely the program looks for that file
not where you think it does.

well, with __FILE__ it worked... the file I'm trying to open is in the
same directory of the executale shouldn't be the first directory the
environment checks?

thanks for your help
 
G

giff

(e-mail address removed) ha scritto:
Try running from the command line instead of inside the devel
environment and see if the problem goes away.

same results

Also keep in mind that
the exe will actually be in some subdirectory of the project directory
unless you set it to be someplace else.

the file I want to open and the exe are in the same dir

maybe I'm just too tired and should stop sitting in front of the pc ;)

thanks
 
T

TB

giff skrev:
hi all

this is the code:

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
std::ifstream iff("test.txt");

if (iff.is_open()) printf("Hello World!\n");
return 0;
}


I'm using visual c++ 6 and the file test.txt that is on the same
directory of the program doesn't want to get open = it never prints out
hello world

does anyone know why?

Try an absolute path.
 
V

Victor Bazarov

giff said:
[..]
well, with __FILE__ it worked... the file I'm trying to open is in the
same directory of the executale shouldn't be the first directory the
environment checks?

That's beyond the scope of this newsgroup. Ask in the newsgroup dedicated
to your environment.

V
 
G

giff

Victor Bazarov ha scritto:
giff said:
[..]
well, with __FILE__ it worked... the file I'm trying to open is in the
same directory of the executale shouldn't be the first directory the
environment checks?

That's beyond the scope of this newsgroup. Ask in the newsgroup dedicated
to your environment.

right, thanks again
 
T

TB

giff skrev:
Victor Bazarov ha scritto:


well, with __FILE__ it worked... the file I'm trying to open is in the
same directory of the executale shouldn't be the first directory the
environment checks?

thanks for your help

Output __FILE__ to see where it resides.
 
R

roberts.noah

giff said:
Victor Bazarov ha scritto:

well, with __FILE__ it worked... the file I'm trying to open is in the
same directory of the executale shouldn't be the first directory the
environment checks?

thanks for your help

Looks like you didn't follow my suggestion to use the command line. I
wasn't talking about the command line in the VS environment but the
command line in the system itself.

__FILE__ is the source file in question so it is obviously looking in
the source directory. This isn't normal unless you are within the VS
environment.
 
G

giff

(e-mail address removed) ha scritto:
Looks like you didn't follow my suggestion to use the command line. I
wasn't talking about the command line in the VS environment but the
command line in the system itself.

that's what I did, I run the exe from the windows command line
(start->run->cmd)

but it does not open the file (test.txt)

I tried with an absolut path too...
 
V

Victor Bazarov

[..]
Looks like you didn't follow my suggestion to use the command line. I
wasn't talking about the command line in the VS environment but the
command line in the system itself.

__FILE__ is the source file in question so it is obviously looking in
the source directory. This isn't normal unless you are within the VS
environment.

Explaining anything about VS environment is off-topic here. Please
refrain from that. There are 'microsoft.public.vc.*' newsgroups for
that.
 
P

pcnate

The code looks OK, but looks like you are using c, if you can, use try
something like this.
#include <fstream>
#include <iostream>

using namespace std;

ifstream iff;

void main()
{
iff.open("test.txt");

if(!iff.fail())
cout << "Hello World!\n";
}
 
R

roberts.noah

Victor said:
[..]
Looks like you didn't follow my suggestion to use the command line. I
wasn't talking about the command line in the VS environment but the
command line in the system itself.

__FILE__ is the source file in question so it is obviously looking in
the source directory. This isn't normal unless you are within the VS
environment.

Explaining anything about VS environment is off-topic here. Please
refrain from that. There are 'microsoft.public.vc.*' newsgroups for
that.

Just for that...

The MS Visual Studio creates directories based on your build by
default. You can change this by going to Project->XX Properties, where
XX is the name of your project. This results in a dialog with many
options including one under "Configuration Propreties\General" called
"Ouput Directory." This is where object files get placed. Another
option under "Linker\General" called "Output File" can be used to
change where the exe is placed and what it is called.

There are two plugins I use at work to help me in my productivity:
VAssist and Ref++. Vassist improves the syntax hilighting and symbol
completion and Ref++ implements certain basic refactors in an easily
undoable operation. Neither works flawlessly and I find Vassist often
does things I don't want without telling me but in general they improve
the interface a great deal.

Would you like me to continue or are you done?
 
V

Victor Bazarov

Victor said:
[..]
Looks like you didn't follow my suggestion to use the command line. I
wasn't talking about the command line in the VS environment but the
command line in the system itself.

__FILE__ is the source file in question so it is obviously looking in
the source directory. This isn't normal unless you are within the VS
environment.

Explaining anything about VS environment is off-topic here. Please
refrain from that. There are 'microsoft.public.vc.*' newsgroups for
that.


Just for that...

[...off-topic removed...]

Would you like me to continue or are you done?

Plonk. Now I am done. _You_ can continue.
 
B

BobR

giff wrote in message ...
(e-mail address removed) ha scritto:

that's what I did, I run the exe from the windows command line
(start->run->cmd)

That is NOT 'command line'.
start-->programs-->MS-DOS prompt
....is closer. Now you at least are in a terminal window with a DOS prompt.
Mr. Roberts was probably talking about shutting down windows, and going to
'MS DOS mode'.

// --- copy and paste this to "Test1.cpp" ---
#include <fstream> // C++
#include <iostream>
#include <ostream> // std::endl
#include <string>

int main(){
std::eek:fstream out( "Test1.txt" );
if( not out ){
std::cout << "Error opening ofstream file!\n"<<std::endl;
return EXIT_FAILURE;
}
out<<"Hello World"<<std::endl;
out.close();
std::ifstream iff( "Test1.txt" );
if( not iff ){
std::cout << "Error opening ifstream file!\n"<<std::endl;
return EXIT_FAILURE;
}
std::string line;
std::getline( iff, line);
std::cout <<"Hello World!\n"<<std::endl;
std::cout << line <<std::endl;
return 0;
} // main()end
// ----------
// --- compile as console program ---

If that does not work, you need to get a newer compiler! VC++ 6 is OLD!
 
B

benben

That is NOT 'command line'.
start-->programs-->MS-DOS prompt
...is closer. Now you at least are in a terminal window with a DOS prompt.
Mr. Roberts was probably talking about shutting down windows, and going to
'MS DOS mode'.

I doubt that!

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top