473,854 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream can't open file

Hi there,

Im trying a simple file IO operation in Visual Studio .NET 2003 and it
can't seem to open the file.
If I run the exe in the debug directory it works fine but if I click
the start button (blue arrow) then it fails to open the file.

any ideas?

source:

#include <iostream>
#include <fstream>

using namespace std;
int main(){

ifstream in;
in.open("test.t xt");
char str[80];

if(in.is_open() ){
cout << "open ok" << endl;
in >> str;
cout << str;
cout << "printed?" << endl;
}
else
cout << "could not open file" << endl;

for(;;);

return 0;

}

Sep 30 '05 #1
3 15071
Try to copy your file from debug directory to the parent directory of
debug (where the project file lives).

Best,
Gang

Murasama wrote:
Hi there,

Im trying a simple file IO operation in Visual Studio .NET 2003 and it
can't seem to open the file.
If I run the exe in the debug directory it works fine but if I click
the start button (blue arrow) then it fails to open the file.

any ideas?

source:

#include <iostream>
#include <fstream>

using namespace std;
int main(){

ifstream in;
in.open("test.t xt");
char str[80];

if(in.is_open() ){
cout << "open ok" << endl;
in >> str;
cout << str;
cout << "printed?" << endl;
}
else
cout << "could not open file" << endl;

for(;;);

return 0;

}

Sep 30 '05 #2
Thanks a lot, that worked.

As soon as I read your post I immediately remembered about the "working
directory" settings in the project properties.

Thanks again.

Sep 30 '05 #3
Murasama wrote:
#include <iostream>
#include <fstream>

using namespace std;
int main(){

ifstream in;
in.open("test.t xt");
char str[80];

if(in.is_open() ){
cout << "open ok" << endl;
in >> str;
cout << str;
cout << "printed?" << endl;
}
else
cout << "could not open file" << endl;

for(;;);

return 0;

}


Did you make sure that there is a file "test.txt"?
Best

Kai-Uwe Bux
Sep 30 '05 #4

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

Similar topics

4
3692
by: Joe | last post by:
Hello - I wrote a program that uses ifstream to open an ASCII file and getline() to read in the lines. The problem is when I try to open the same file again later in the code. I used close() to close the file but the next open() fails. If I comment out the getline() then I can open it a second time without a problem. Here are parts of the code: ifstream InputFile; char Line; // MAXcharLine = 512
1
2376
by: Jim Phelps | last post by:
Hello all, I am in a bit of a pickle using the getline function with an ifstream. It does not seem to work as advertised. Here is my scenario. In a nutshell, my code needs to pick up a fixed record length flat file that is generated by an old IBM mainframe. These data fields need to be read in by my program EXACTLY as they are represented in the file. Here is an example text file that I used in my test. ...
6
3665
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt" file_names = "file2.txt" etc ...
6
3401
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber" 10,20,30
6
4290
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate function called readline() because I would also like to do some processing on the line each time (ignoring comments and so on). I have:
4
3743
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream in("in.txt"); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";}
10
18092
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
1
4920
by: Xiaozhou.Yin | last post by:
Hi~ In the program,I first used the ifstream variable fin open the file and,open it again after called the fin.close().But the fin is fieled to open the file in the second time.The book I'm learning hadn't picked out this.Is it a common problem?Or just occurs in Vc++6.0? #include<fstream> #include<iostream> using namespace std; int count(ifstream& fin);
2
15346
by: mpalomas | last post by:
Hi C++ folks, I have trouble to open files whose path contains non-ascii characters with std::ifstream. For instance let's say i just have a file which has Japanese characters either in the file path or the file name : 疑問.dat. The file itself does not contains unicode characters or whatever, it is a binary file, but the file name, or path, contains non-ascii characters, here it is Japanese but it could be anything else, i know...
0
1362
by: James Kanze | last post by:
On 11 avr, 17:44, "mc" <mc_r...@yahoo.comwrote: OK. If the actual format is well documented, that's half the battle won already. Note, however, that reading a float as an int is still very implementation dependent, since the actual internal format of a float varies between machines. The documentation should specify the format of the float in the file, either by reference to some known format (e.g. IEEE), or by explicitely specifying...
0
11025
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10682
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...
1
10758
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10371
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
9513
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 projectplanning, coding, testing, and deploymentwithout 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...
1
7915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7082
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();...
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3187
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.