I can't believe I am having so many problems with file access
-
I must be some kind of melon head. I'm including and don't understand why I can construct an fstream like so fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); and get things to work but not fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::noreplace); because it says error C2039: 'noreplace' : is not a member of 'basic_ios >' I've only looked in the help and checked out "fstream", read the notes about its ctor and seen these flags, I could understand if the 1st one didn't work, I would assume I'm doing something ridiculous, but can't work out how it can recognise some of the mode flags, but not another If any kind soul can just get me going (I know it tends to be David Crow who comes to the rescue!) I would really appreciate it. I just want to: - check if a file (specified by a CString object) exists. - if it does, I want to nip to the end of the file and start appending data rows - if it doesn't, I just need to create it, bung in a few lines of header and then put the data rows in it. I'm embarrassed and ashamed that it's taking me so long to get this to work as I'm sure in days gone by I must have done this a hundred times! aaarrrggghhhhh!
-
I must be some kind of melon head. I'm including and don't understand why I can construct an fstream like so fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); and get things to work but not fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::noreplace); because it says error C2039: 'noreplace' : is not a member of 'basic_ios >' I've only looked in the help and checked out "fstream", read the notes about its ctor and seen these flags, I could understand if the 1st one didn't work, I would assume I'm doing something ridiculous, but can't work out how it can recognise some of the mode flags, but not another If any kind soul can just get me going (I know it tends to be David Crow who comes to the rescue!) I would really appreciate it. I just want to: - check if a file (specified by a CString object) exists. - if it does, I want to nip to the end of the file and start appending data rows - if it doesn't, I just need to create it, bung in a few lines of header and then put the data rows in it. I'm embarrassed and ashamed that it's taking me so long to get this to work as I'm sure in days gone by I must have done this a hundred times! aaarrrggghhhhh!
just so you don't think I am not writing anything, here is what I've got so far, please do not laugh void CTestFolderBrowseDlg::OnSaveentry() { // TODO: Add your control notification handler code here int sel = m_Table.GetSelectionMark(); if(sel < 0) return; // ERROR no selection // Build Target File Name if(!pathLoaded) return; // ERROR no path info CString energyText; energyText = m_Table.GetItemText(sel, 0); CString fileName(path); fileName += '\\'; if(energyText.GetLength()<2) fileName += "0"; energyText += "MeV.dat"; fileName += energyText; // Check if File Exists fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); // note, the noreplace flag doesnt work if(FileDataStream.is_open()) { // file exists (I think without noreplace, it's always going to end up in here) // Read Data // Append Data FileDataStream<
-
I must be some kind of melon head. I'm including and don't understand why I can construct an fstream like so fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); and get things to work but not fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::noreplace); because it says error C2039: 'noreplace' : is not a member of 'basic_ios >' I've only looked in the help and checked out "fstream", read the notes about its ctor and seen these flags, I could understand if the 1st one didn't work, I would assume I'm doing something ridiculous, but can't work out how it can recognise some of the mode flags, but not another If any kind soul can just get me going (I know it tends to be David Crow who comes to the rescue!) I would really appreciate it. I just want to: - check if a file (specified by a CString object) exists. - if it does, I want to nip to the end of the file and start appending data rows - if it doesn't, I just need to create it, bung in a few lines of header and then put the data rows in it. I'm embarrassed and ashamed that it's taking me so long to get this to work as I'm sure in days gone by I must have done this a hundred times! aaarrrggghhhhh!
just curious : why are you creating a fstream if you only need to write to the file ? doesn't a ofstream suffice ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
I must be some kind of melon head. I'm including and don't understand why I can construct an fstream like so fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::ate); and get things to work but not fstream FileDataStream; FileDataStream.open(fileName, ios::out | ios::noreplace); because it says error C2039: 'noreplace' : is not a member of 'basic_ios >' I've only looked in the help and checked out "fstream", read the notes about its ctor and seen these flags, I could understand if the 1st one didn't work, I would assume I'm doing something ridiculous, but can't work out how it can recognise some of the mode flags, but not another If any kind soul can just get me going (I know it tends to be David Crow who comes to the rescue!) I would really appreciate it. I just want to: - check if a file (specified by a CString object) exists. - if it does, I want to nip to the end of the file and start appending data rows - if it doesn't, I just need to create it, bung in a few lines of header and then put the data rows in it. I'm embarrassed and ashamed that it's taking me so long to get this to work as I'm sure in days gone by I must have done this a hundred times! aaarrrggghhhhh!
ldsdbomber wrote:
I've only looked in the help and checked out "fstream"...
But you did not look in
ios.h
, which is included byfstream.h
. With the newer header files, thenoreplace
andnocreate
flags were removed.ldsdbomber wrote:
- check if a file (specified by a CString object) exists.
How about
_access(str, 0)
? Or just try to open the file for reading. If that fails, the file likely does not exist.ldsdbomber wrote:
- if it does, I want to nip to the end of the file and start appending data rows
Have you tried
ios::app
?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
ldsdbomber wrote:
I've only looked in the help and checked out "fstream"...
But you did not look in
ios.h
, which is included byfstream.h
. With the newer header files, thenoreplace
andnocreate
flags were removed.ldsdbomber wrote:
- check if a file (specified by a CString object) exists.
How about
_access(str, 0)
? Or just try to open the file for reading. If that fails, the file likely does not exist.ldsdbomber wrote:
- if it does, I want to nip to the end of the file and start appending data rows
Have you tried
ios::app
?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
thanks David. I have not used _access before, I am mainly relying on the help files within VC from pressing F1 with the keyword highlighted or searching in the index there. It doesn't always seem very well organised and I am often confused about when I am looking at pure C, C++, MFC, STL etc etc. But _access seems to be exactly what I am looking for. If you can recommend a good book that covers all this newer up to date stuff, that would be great. For the most part, I'm looking at MFC dialog apps
-
thanks David. I have not used _access before, I am mainly relying on the help files within VC from pressing F1 with the keyword highlighted or searching in the index there. It doesn't always seem very well organised and I am often confused about when I am looking at pure C, C++, MFC, STL etc etc. But _access seems to be exactly what I am looking for. If you can recommend a good book that covers all this newer up to date stuff, that would be great. For the most part, I'm looking at MFC dialog apps
ldsdbomber wrote:
For the most part, I'm looking at MFC dialog apps
For MFC, you can use
CFile::GetStatus()
. There's alsoGetFileAttributes()
.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne