473,396 Members | 1,767 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.

ifstream create new file

8
Hi i have a problem of ifstream creating new file when the file i want to open does not exist. According to some googling done, it does not create file. But i do not know why my program does. Is there any setting need to be done to prevent ifstream creating new files? Can help me.

Thanks a lot.
Jan 17 '07 #1
4 11675
Ganon11
3,652 Expert 2GB
ifstream should not create new files, because it will be trying to load data from an existing file. To create a new, blank file would serve no purpose, since there would be no data to input.

You may be confused with ofstream. ofstream does create new files, since it will be saving data. It is perfectly all right to create a new, empty file if you know you are going to be filling the file with values rather than inputting values.
Jan 17 '07 #2
vincec
8
Expand|Select|Wrap|Line Numbers
  1. for (int x=0;x<2;x++)
  2.     {
  3.         for (int y=0;y<2;y++)
  4.         {
  5.         a.Format("%d",x);
  6.         b.Format("%d",y);
  7.         targetfilename = "PolyU_0" + a +"_" + b +".spmCHDHT";
  8.  
  9.         ifstream target(targetfilename);
  10.  
  11.         if(!target)
  12.         {
  13.             MessageBox("Cannot open file");
  14.         }
  15.         else
  16.         {
  17.             MessageBox("File opened");
  18.         }
  19.         }
  20.  
  21.     }
this is the code that i used. i tried using a new workspace to execute the code. it works, it will prompt some of the files "Cannot open file". But when i integrate it to the orginal file, ifstream will automatically create file. I think there must be some setting need or some #include file needed ?
Jan 18 '07 #3
Ganon11
3,652 Expert 2GB
Well, you would have to include <fstream>, but that's necessary for even using ifstream, so you probably already have it.

Try outputting targetfilename once you have set it and see what that is. I'm not sure what the .Format statements are doing, but it may be changing those values such that they no longer match your filename.
Jan 18 '07 #4
vincec
8
Hi thanks for helping, i have mange to solve the problem using fopen(); instead of ifstream. Using fopen, the program wouldn't create new file if the file i want to open for reading is not available. Although i still do not know why ifstream will create new file, but lucky that fopen did the job.

The .Format(); is for converting int to CString.
Jan 19 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mathieu Malaterre | last post by:
Hi, I am tryig to convert part of a c++ IO library. Right now I am removing all 'FILE*' and replace them with ifstream. But I have a problem, after seeking through a stream I have to call the...
1
by: egsys | last post by:
I am simply trying to create a file in access 2003. Receive an error message that: "Workgroup administrator couldn't create the workgroup information file. Make sure that you have specified a...
4
by: Bnob | last post by:
It is possible to create a file in local user-machine? With this code in code-behind: fs = New System.IO.FileStream(lFileCSV, System.IO.FileMode.Create, System.IO.FileAccess.Write) The file...
1
by: Victor | last post by:
Hi, We have following setup here Web Server (Win2000) and FTP Server (Win 2000) We have ASP.NET application which runs on Web Server and writes file(s) onto FTP Server. We created asp.net...
3
by: christiang | last post by:
Hi guys, I'd like to use Alexia thumbnail service. It's an interesting service, you pass an url to its cgi and you get a thumbnail of that url. Of course they don't have the screenshots of all the...
1
by: j7o0s5 | last post by:
How to create file help chm?
9
by: vermarajeev | last post by:
Hi, Is there a way to convert ifstream to FILE*; Something like this ifstream ifs( " test.txt "); if( !ifs.is_opsn() ) return 0; FILE* f = ifs; //how can I convert ifs to FILE*
0
Kosal
by: Kosal | last post by:
Hi I have problem with vb.net I want to create file setup or exe file for run on Client Computer that no need to install vb.net 2005 but I never do so I don't know what can i do? Please help...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
0
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,...

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.