473,856 Members | 1,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file creation problem in Windows using fstream

This may be the wrong group but I didn't see anything for VC++ so I'm
trying here.

I have a C++ book by Deitel and Deitel that says I can use fstream
File("data.dat" , ios::in | ios::out | ios::binary) to declare a file
object with read/write modes turned on for working with binary data.
I've tried this and my file is not created. The only time it is created
is when I specify ifstream or ofstream but not fstream. I've tried
removing the binary mode and it doens't make a difference. The
following is sample code I created to just test to see whether g++ in
Linux and VC++ 6.0 in Windows XP could handle the code:

#include <all appropate files>

using std:: all appropriate statements;
void main ()
{

char fd[255];
int entries;
char *entry = "f";
cout << "Enter the filename for performing operations: " << endl;
cin >> fd;
fstream dataFile(fd, ios::out | ios::in | ios::binary );

cout << "How many entries would you like for the file to hold? " <<
endl;
cin >> entries;

for (int i = 0; i < entries; i++)
dataFile.write( reinterpret_cas t<const char *>(&entry), sizeof(char));
entry="g";
cout << entry << endl;
dataFile.seekg( 0);
dataFile.read(r einterpret_cast <char *>(&entry), sizeof(char));
cout << entry << endl;
}

The file is initalized with "f" and then the "entry" variable is set to
"g". If the file is created properly and can be read then "entry" is
assigned "f" from the file. g++ compiles this progrma fine and upon
execution the file is created and initalized, so a "g" and then a "f" is
printed out. In Windows XP Visual C++ 6 will compile it but my file is
not created. I have a project due next Friday where I'm trying to do
this and I odn't know why it's not working. THe professsor uses VC++6
and he doesn't have problems and I'm doing the same thing the book is
doing from what I can tell. Does anyone know what VC++ is wanting that
I'm not doing?

As a workaround I tried to open a file in read mode using ifstream
declaration and then close the file after reading is done and reopen the
file with write mode using ofstream. That works as far as the file
being created however the contents of the file from a write operation
are discarded once I reopen the file and I think that's stupid behavior.
What happens if a user wants to write to multiple files during a single
execution of a program? If they write to one file then another, and come
back to the original the contents are discarded and they lost all their
work. But anyway, using the ios::append mode just puts my data at the
end of the file and since this has to be random access that is not going
to cut it. Using seekg() and seekp() to override default writing at the
eend of the file was not sucessful. I'm out of options unlesss someone
can give some pointers. (no pun intended)

thanks for any help
Brandon

Jul 19 '05 #1
8 9862
"Brandon McCombs" <bm******@ma.rr .com> wrote in message
news:3F******** *******@ma.rr.c om...
I have a C++ book by Deitel and Deitel that says I can use fstream
File("data.dat" , ios::in | ios::out | ios::binary) to declare a file
object with read/write modes turned on for working with binary data.
I've tried this and my file is not created. The only time it is created
is when I specify ifstream or ofstream but not fstream. I've tried
removing the binary mode and it doens't make a difference. The
following is sample code I created to just test to see whether g++ in
Linux and VC++ 6.0 in Windows XP could handle the code:

#include <all appropate files>

using std:: all appropriate statements;
void main ()
{

char fd[255];
int entries;
char *entry = "f";
cout << "Enter the filename for performing operations: " << endl;
cin >> fd;
fstream dataFile(fd, ios::out | ios::in | ios::binary );

cout << "How many entries would you like for the file to hold? " <<
endl;
cin >> entries;

for (int i = 0; i < entries; i++)
dataFile.write( reinterpret_cas t<const char *>(&entry), sizeof(char));
entry="g";
cout << entry << endl;
dataFile.seekg( 0);
dataFile.read(r einterpret_cast <char *>(&entry), sizeof(char));
cout << entry << endl;
}

The file is initalized with "f" and then the "entry" variable is set to
"g". If the file is created properly and can be read then "entry" is
assigned "f" from the file. g++ compiles this progrma fine and upon
execution the file is created and initalized, so a "g" and then a "f" is
printed out. In Windows XP Visual C++ 6 will compile it but my file is
not created. I have a project due next Friday where I'm trying to do
this and I odn't know why it's not working. THe professsor uses VC++6
and he doesn't have problems and I'm doing the same thing the book is
doing from what I can tell. Does anyone know what VC++ is wanting that
I'm not doing?

As a workaround I tried to open a file in read mode using ifstream
declaration and then close the file after reading is done and reopen the
file with write mode using ofstream. That works as far as the file
being created however the contents of the file from a write operation
are discarded once I reopen the file and I think that's stupid behavior.
What happens if a user wants to write to multiple files during a single
execution of a program? If they write to one file then another, and come
back to the original the contents are discarded and they lost all their
work. But anyway, using the ios::append mode just puts my data at the
end of the file and since this has to be random access that is not going
to cut it. Using seekg() and seekp() to override default writing at the
eend of the file was not sucessful. I'm out of options unlesss someone
can give some pointers. (no pun intended)


From our online documentation:

basic_filebuf *open(const char *filename,
ios_base::openm ode mode);

The member function endeavors to open the file with filename filename,
by calling fopen(filename, strmode). Here strmode is determined from
mode & ~(ate & | binary):

-- ios_base::in becomes "r" (open existing file for reading).

-- ios_base::out or ios_base::out | ios_base::trunc becomes "w"
(truncate existing file or create for writing).

-- ios_base::out | ios_base::app becomes "a"
(open existing file for appending all writes).

-- ios_base::in | ios_base::out becomes "r+"
(open existing file for reading and writing).

-- ios_base::in | ios_base::out | ios_base::trunc becomes "w+"
(truncate existing file or create for reading and writing).

ios_base::in | ios_base::out | ios_base::app becomes "a+"
(open existing file for reading and for appending all writes).

If mode & ios_base::binar y is nonzero, the function appends b to strmode
to open a binary stream instead of a text stream. It then stores the
value returned by fopen in the file pointer fp. If mode & ios_base::ate
is nonzero and the file pointer is not a null pointer, the function calls
fseek(fp, 0, SEEK_END) to position the stream at end-of-file. If that
positioning operation fails, the function calls close(fp) and stores a
null pointer in the file pointer.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 19 '05 #2

So how does all this help me? I know all the modes. The only one I havent
tried is the truncate mode and that gets rid of the file's contents as well so
what are you suggesting, if anything?


From our online documentation:

basic_filebuf *open(const char *filename,
ios_base::openm ode mode);

The member function endeavors to open the file with filename filename,
by calling fopen(filename, strmode). Here strmode is determined from
mode & ~(ate & | binary):

-- ios_base::in becomes "r" (open existing file for reading).

-- ios_base::out or ios_base::out | ios_base::trunc becomes "w"
(truncate existing file or create for writing).

-- ios_base::out | ios_base::app becomes "a"
(open existing file for appending all writes).

-- ios_base::in | ios_base::out becomes "r+"
(open existing file for reading and writing).

-- ios_base::in | ios_base::out | ios_base::trunc becomes "w+"
(truncate existing file or create for reading and writing).

ios_base::in | ios_base::out | ios_base::app becomes "a+"
(open existing file for reading and for appending all writes).

If mode & ios_base::binar y is nonzero, the function appends b to strmode
to open a binary stream instead of a text stream. It then stores the
value returned by fopen in the file pointer fp. If mode & ios_base::ate
is nonzero and the file pointer is not a null pointer, the function calls
fseek(fp, 0, SEEK_END) to position the stream at end-of-file. If that
positioning operation fails, the function calls close(fp) and stores a
null pointer in the file pointer.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Jul 19 '05 #3
"Brandon McCombs" <bm******@ma.rr .com> wrote in message
news:3F******** *******@ma.rr.c om...

So how does all this help me? I know all the modes. The only one I havent
tried is the truncate mode and that gets rid of the file's contents as well so what are you suggesting, if anything?
I'm suggesting you RTFM. You wrote:
I have a C++ book by Deitel and Deitel that says I can use fstream
File("data.dat" , ios::in | ios::out | ios::binary) to declare a file
object with read/write modes turned on for working with binary data.
I've tried this and my file is not created.


And I wrote:

-- ios_base::in | ios_base::out becomes "r+"
(open existing file for reading and writing).

-- ios_base::in | ios_base::out | ios_base::trunc becomes "w+"
(truncate existing file or create for reading and writing).

Deitel and Deitel have many errors in their writings, and g++ does
not score high on conformance. None of that excuses an inability
to read documentation. If you want to *create* a file, then choose
the mode that says a file gets created. Duh.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 19 '05 #4


"P.J. Plauger" wrote:
"Brandon McCombs" <bm******@ma.rr .com> wrote in message
news:3F******** *******@ma.rr.c om...

So how does all this help me? I know all the modes. The only one I havent
tried is the truncate mode and that gets rid of the file's contents as

well so
what are you suggesting, if anything?


I'm suggesting you RTFM. You wrote:
I have a C++ book by Deitel and Deitel that says I can use fstream
File("data.dat" , ios::in | ios::out | ios::binary) to declare a file
object with read/write modes turned on for working with binary data.
I've tried this and my file is not created.


And I wrote:

-- ios_base::in | ios_base::out becomes "r+"
(open existing file for reading and writing).

-- ios_base::in | ios_base::out | ios_base::trunc becomes "w+"
(truncate existing file or create for reading and writing).

Deitel and Deitel have many errors in their writings, and g++ does
not score high on conformance. None of that excuses an inability
to read documentation. If you want to *create* a file, then choose
the mode that says a file gets created. Duh.


i AM using a mode that creates a file. By default ios::out creates a file if it
doesn't already exist and when using ofstream ("data.dat", ios::out |
ios::binary) the file DOES in fact get created and i can even leave out the
ios::out part since thats the default behavior for ofstream but supposedly i can
use fstream and it creates or opens a file with both read/write access but that
doesn't happen in windows. As long as I use fstream no file is created. I'd
like to use fstream so i don't have to constantly be opening a file with read
access, closing it, then having to reopen it to write to it within the same
function and even that is a problem b/c when I open a file with write access
using ios::out the behavior of that is to delete the existing contents whcih is
not what I want done. Using ios::ate and ios::app did not work as intended
based upon the defintions for them in the Deitel book. Using ios::ate still
erased the existing contents and using ios::app just forced the content to go at
the end of the file which is stupid since i used seekp() to define where I
wanted the data to be wrtten.

from what i can tell there is no middle ground for opening a file w/o its
contents being discarded and to write to that file wherever i want. I either
lose the data or I keep the data but am forced to write at the end, which is not
useful for random read/write access.
Jul 19 '05 #5
"Brandon McCombs" <bm******@ma.rr .com> wrote in message
news:3F******** *******@ma.rr.c om...
I have a C++ book by Deitel and Deitel that says I can use fstream
File("data.dat" , ios::in | ios::out | ios::binary) to declare a file
object with read/write modes turned on for working with binary data.
I've tried this and my file is not created.
And I wrote:

-- ios_base::in | ios_base::out becomes "r+"
(open existing file for reading and writing).

-- ios_base::in | ios_base::out | ios_base::trunc becomes "w+"
(truncate existing file or create for reading and writing).

Deitel and Deitel have many errors in their writings, and g++ does
not score high on conformance. None of that excuses an inability
to read documentation. If you want to *create* a file, then choose
the mode that says a file gets created. Duh.


i AM using a mode that creates a file. By default ios::out creates a file

if it doesn't already exist and when using ofstream ("data.dat", ios::out |
ios::binary) the file DOES in fact get created and i can even leave out the ios::out part since thats the default behavior for ofstream but supposedly i can use fstream and it creates or opens a file with both read/write access but that doesn't happen in windows.
If you'll stop hyperventilatin g and reread what I quoted before, you'll
see that there is a way to do what you want. If you want to create a
file and then be able to read and write it, the C++ Standard requires
that you specify in|out|trunc. You got away without the trunc on a
system that doesn't conform well to the C++ Standard. But now you know
the portable way to do what you want.
from what i can tell there is no middle ground for opening a file w/o its
contents being discarded and to write to that file wherever i want. I either lose the data or I keep the data but am forced to write at the end, which is not useful for random read/write access.


And I wrote:
-- ios_base::in | ios_base::out becomes "r+"
(open existing file for reading and writing).
which sounds like exactly what you're asking (this time). Before, you
were asking how to *create* a file for reading and writing, and I wrote:
-- ios_base::in | ios_base::out | ios_base::trunc becomes "w+"
(truncate existing file or create for reading and writing).


RTFM. (The F isn't always silent.)

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 19 '05 #6
>

If you'll stop hyperventilatin g and reread what I quoted before, you'll
see that there is a way to do what you want. If you want to create a
file and then be able to read and write it, the C++ Standard requires
that you specify in|out|trunc. You got away without the trunc on a
system that doesn't conform well to the C++ Standard. But now you know
the portable way to do what you want.

I tried the ios::trunc and it did indeed create the file so I thank you for
that. Based on what the Deitel book says it makes no mention of the file being
created if it doesn't already exist when using trunc. I have to wonder why the
standards people required that to be there but stupider things have been created
I guess in the world of CS. I tried using ios::in | ios::out to open a file for
reading and writing and it just deletes the contents of my file which I do not
want. The data I'm writing is placed in the right location since I'm using
seekp() but I only get to keep the last set of data I write and only until I
open the file again. Using ate and app did not help. Am I missing something
else?
from what i can tell there is no middle ground for opening a file w/o its
contents being discarded and to write to that file wherever i want. I either
lose the data or I keep the data but am forced to write at the end, which

is not
useful for random read/write access.


And I wrote:
-- ios_base::in | ios_base::out becomes "r+"
(open existing file for reading and writing).
which sounds like exactly what you're asking (this time). Before, you


Actually I mentioned this problem in the last paragraph of the original post. I
may be able to open the file for reading but that is pointless when the data is
erased upon the opening action and thus there is nothing left to read.


were asking how to *create* a file for reading and writing, and I wrote:
-- ios_base::in | ios_base::out | ios_base::trunc becomes "w+"
(truncate existing file or create for reading and writing).


RTFM. (The F isn't always silent.)

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Jul 19 '05 #7
"Brandon McCombs" <bm******@ma.rr .com> wrote in message
news:3F******** *******@ma.rr.c om...
If you'll stop hyperventilatin g and reread what I quoted before, you'll
see that there is a way to do what you want. If you want to create a
file and then be able to read and write it, the C++ Standard requires
that you specify in|out|trunc. You got away without the trunc on a
system that doesn't conform well to the C++ Standard. But now you know
the portable way to do what you want.

I tried the ios::trunc and it did indeed create the file so I thank you

for that. Based on what the Deitel book says it makes no mention of the file being created if it doesn't already exist when using trunc.
As I said before Deitel^2 is not the most precise book around.
I have to wonder why the
standards people required that to be there but stupider things have been created I guess in the world of CS.
Look, you want to open a file for read and write, sometimes starting
afresh and sometimes retaining the existing file. What's so stupid
about using trunc to distinguish the two cases? Do you have a better
suggestion?
I tried using ios::in | ios::out to open a file for reading and writing and it just deletes the contents of my file which I do not want.
Well I just tried it and it doesn't. Perhaps you should repeat your
experiment, a bit more carefully.
The data I'm writing is placed in the right location since I'm using
seekp() but I only get to keep the last set of data I write and only until I open the file again. Using ate and app did not help. Am I missing something else?


Evidently. My test code does exactly what the documentation I quoted
says.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 19 '05 #8
"P.J. Plauger" <pj*@dinkumware .com> wrote:
"Brandon McCombs" <bm******@ma.rr .com> wrote:
[OP question about Deitel&Deitel example]


If you'll stop hyperventilatin g and reread what I quoted before, you'll
see that there is a way to do what you want. If you want to create a
file and then be able to read and write it, the C++ Standard requires
that you specify in|out|trunc. You got away without the trunc on a
system that doesn't conform well to the C++ Standard. But now you know
the portable way to do what you want.


I've been searching for an answer to a related question recently. I
generally go around using in|out mode for a matchup to the C fopen in
"r+" mode. I have a batch of files I want opened for both input and
output, and I want a failure on a non-existant file; I don't want an
empty file to be created.

It is definately non-fashionable for in|out mode to create an empty
file instead of failing, if the file doesn't exist. All modern
compilers I use make in|out mode equivalent to the "r+" mode on fopen.
But I was unable to find any text in the Standard saying it is
actually non-standard for an implementation to create the empty file.

This has been bugging me, because I have an ancient target platform
that creates empty files on in|out. There is a non-standard flag I can
send to stop it. If creating the empty file on in|out is non-standard,
I am willing to use conditional compilation to fix this problem on
this one platform. But if it is only non-fashionable to create the
file, I am probably stuck with the bloated hack of doing two opens for
every in|out file I want, the first one being a separate test for
existance with 'in' mode.

--
Dave O'Hearn
Jul 22 '05 #9

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

Similar topics

3
2717
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead. > > Can somebody please tell whats wrong?? > > This is the code : : #include<fstream.h> #include<conio.h>
11
4054
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType = "text/xml" Response.AddHeader("Content-Disposition", "filename=test.XML")
2
4646
by: gauravkhanna | last post by:
Hi All I need some help for the below problem: Scenario We need to send large binary files (audio file of about 10 MB or so) from the client machine (.Net Windows based application, located outside the home network) to the Web Server and then retrieve the file back from the web server to the client.
1
3374
by: Bonzol | last post by:
vb.net 1.1 Windows application Hey,, In a bit of a hurry,, does anyone know a way to turn a file in to a string,, such as a jpeg or pdf,, then turn that string back in to the original file? Cheers
3
6119
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never learned file input/output very well (I self taught all the programming I know...and my c++ book was not good) and so I'm not sure what's wrong with this. The problem is the function void encodemsg(fstream *img, fstream *msg, fstream *out, char key) ...
6
3765
by: cooldisk | last post by:
Is it possible at all to read a binary file larger than 2GB on a 32- bit system? I tried the following: #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv) { fstream fsBin; fsBin.open(argv, fstream::in | fstream::binary);
6
2261
by: subaruwrx88011 | last post by:
Hey, I am trying to create log files in a different directory than where my executable is at. I am pretty stumped. This is what I have in psuedo code. std::string const M_LOG_PATH = "\\data\\logs\\"; std::string m_log_filename_with_path; std::string filename = "log.txt"; m_log_filename_with_path = M_LOG_PATH + filename;
10
22792
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app) outFile.write("teststring", 10); outFile.close(); If I leave out the ios::ate and ios::app modes my string is written to the start of the file as I'd expect but I want to write the data to
2
1732
by: Rene | last post by:
Hello to all! I am a newbee to C++, I did a beginners' course, and now I am stuck with a strange problem. I have written the function that is pasted downwards. The peculiar thing is that when I sent the function the value "0", the .lst file is created properly (the vector I am using/saving is made up of structs of one AnsiString (using BCB) called "naam" and one int called "nummer", I guess You'll get what they mean in English ;-))....
0
9762
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11057
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
10696
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
10384
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...
1
7932
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
7094
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
5958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4575
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
2
4174
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.