B
Brandon McCombs
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:ut | 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:ut | 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
trying here.
I have a C++ book by Deitel and Deitel that says I can use fstream
File("data.dat", ios::in | ios:ut | 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:ut | 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