473,486 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Create 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_cast<const char *>(&entry), sizeof(char));
entry="g";
cout << entry << endl;
dataFile.seekg(0);
dataFile.read(reinterpret_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 9826
"Brandon McCombs" <bm******@ma.rr.com> wrote in message
news:3F***************@ma.rr.com...
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_cast<const char *>(&entry), sizeof(char));
entry="g";
cout << entry << endl;
dataFile.seekg(0);
dataFile.read(reinterpret_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::openmode 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::binary 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::openmode 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::binary 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.com...

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.com...

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.com...
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 hyperventilating 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 hyperventilating 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.com...
If you'll stop hyperventilating 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 hyperventilating 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
2685
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....
11
4007
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 =...
2
4605
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...
1
3356
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? ...
3
6082
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...
6
3733
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...
6
2243
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 =...
10
22576
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)...
2
1718
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...
0
7099
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
6964
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...
0
7175
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
5430
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,...
0
4559
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...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
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 ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
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...

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.