dangrmous

Hi,

I'm looking to create a new text file. I am in second term C++ so we're not using the MFC or anything. We have covered opening, reading, closing ifstream and ostream objects from fstream.h , but nothing about how to create a file if one's not there. Is this possible

Also - anyone know how to insert an eof into a file through a text editor or something in Windows I am given to understand the eof flag is represented by a ctrl-Z but I can't insert this using textedit. In the mean time I'm using a pipe symbol '|' as my eof flag which is much less elegant.

Thank you o wise sages of the code.




Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

Simple Samples

What happens if you use ofstream to open a file for output if the file does not exist Did you try it What is the code you used to do it What error did you get Or are you assuming it won't work

What do you need an EOF character for It has no value that I am aware of.






Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

dangrmous

If I create the ifstream object to open the file and the file does not exist, the ifstream object returns a negative result.

The code I am using is:

ifstream fileInput;
ofstream fileOutput;

char dataFileName[] = "c:\\database.txt";

fileInput.open(dataFileName);
if(!fileInput)
{
cerr << "Failed to open the database " << dataFileName << " for input!" << endl;
exit(1);
}

I need an EOF to determine when I'm done reading a file.





Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

Simple Samples

dangrmous wrote:
If I create the ifstream object to open the file and the file does not exist, the ifstream object returns a negative result.
Yes, of course; that is what we should expect to happen. Your question, however, asks how to create a new text file, not how to read a text file.


dangrmous wrote:
The code I am using is:I need an EOF to determine when I'm done reading a file.
No, you don't need an EOF to determine the end of file. The EOF character has no significance to the ifstream class in particular, and I am not aware of any C/C++ class or function or any Windows function that the EOF character has any significance for.






Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

dangrmous

You asked me in your last comment the following, and I quote directly:

"What happens if you use ofstream to open a file for output if the file does not exist Did you try it What is the code you used to do it What error did you get Or are you assuming it won't work "

I answered your question as best I could.

Secondly, there is member function inherited for ios called "eof". You can learn about it here:

http://www.cplusplus.com/reference/iostream/ios/eof.html

Thank you very much for all of your helpful information!





Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

Simple Samples

 dangrmous wrote:
You asked me in your last comment the following, and I quote directly:

" What happens if you use ofstream to open a file for output if the file does not exist Did you try it What is the code you used to do it What error did you get Or are you assuming it won't work "

I answered your question as best I could.
Read your reply (the part I quoted) again. Your reply said ifstream but I said ofstream. I was confused about what you were asking and I tried to clarify what you meant.

 dangrmous wrote:
Secondly, there is member function inherited for ios called "eof". You can learn about it here:

http://www.cplusplus.com/reference/iostream/ios/eof.html

Thank you very much for all of your helpful information!
Yes, eof is the solution. It is easy to find. I choose to not provide answers that can be easily found. I knew you could find eof if you look at the documentation and such. If people could get answers that can easily be found from forums such as this one then there would be too many such questions.

Note that if the question had been how to determine EOF then you would probably have gotten the answer you really wanted. You did not ask that, however; instead you asked about the EOF character.

 

 






Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

dangrmous

Yes, eof is the solution. It is easy to find. I choose to not provide answers that can be easily found. I knew you could find eof if you look at the documentation and such. If people could get answers that can easily be found from forums such as this one then there would be too many such questions.

Note that if the question had been how to determine EOF then you would probably have gotten the answer you really wanted. You did not ask that, however; instead you asked about the EOF character.

Please read my inital question again I was asking how to INSERT and eof character into a document, not what and eof character was.

Please do not respond to my questions in this forum in the future.

Thank you.





Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

Simple Samples

dangrmous wrote:
Please read my inital question again I was asking how to INSERT and eof character into a document, not what and eof character was.
I was trying to make it clear that an EOF character is most likely useless, and if it is useless, then that is infinitely relevant to how to write it. If there is reason it is useful, then the solution of how to write it is likely irregular, since the requirement is irregular.




Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

einaros

dangrmous wrote:

I'm looking to create a new text file. I am in second term C++ so we're not using the MFC or anything. We have covered opening, reading, closing ifstream and ostream objects from fstream.h , but nothing about how to create a file if one's not there. Is this possible

Yeah, ifstream's open lets you specify how to open the file, and what happens if it already exists or doesn't exist at all.

ifstream f; f.open("filename", openmode);

Where openmode can be (and this is just about a direct paste from the MSDN page, so in the future, consider poking around there such as Sam suggests ):

ios_base::app, to seek to the end of a stream before each insertion.
ios_base::ate, to seek to the end of a stream when its controlling object is first created.
ios_base::binary, to read a file as a binary stream, rather than as a text stream.
ios_base::in, to permit extraction from a stream.
ios_base::out, to permit insertion to a stream.
ios_base::trunc, to delete contents of an existing file when its controlling object is created.

To create a new file you can use ifstream::open without any openmode, which will default to ios_base::out. If the file already exists, it will be truncated.

dangrmous wrote:

Also - anyone know how to insert an eof into a file through a text editor or something in Windows I am given to understand the eof flag is represented by a ctrl-Z but I can't insert this using textedit. In the mean time I'm using a pipe symbol '|' as my eof flag which is much less elegant.

An EOF isn't generally something you can insert (anymore). It's not an entity that's present in your file at all, but rather a state indicated to your application when reading hits the end of the file's size. If you wish to create a file with a certain length, without inputing any particular data, you can open a new file for writing, and seek ahead using theFileStreem.seekp(numberOfBytesToSkipAhead);, output a random character (or e.g. a null), and finally close it.

In TextPad / UltraEdit and similar applications you can e.g. switch to hex mode, and input a bunch of null's, then save it. The EOF is "placed" (not entirely true, since it's not present as such) at the end of the file.






Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

dangrmous

Thank you very much for the informative, coherent reply! This information is extremely useful!

I don't understand why people keep assuming I didn't look anywhere else before I posted here. I did in fact go to the Help menu of Visual Studio 2005, which directed me to the MSDN online site. Many searches turned up reams of unrelated junk to sort through, which I did, to no avail. I also tried to get as much information as possible off of http://www.cplusplus.com (my usual first choice).

Anyway, if I have any future questions, I will try harder to find the information elswhere, and should I need to post here I will provide a list of sources I have already checked.

Best regards,

Geoff





Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

Simple Samples

dangrmous wrote:
I don't understand why people keep assuming I didn't look anywhere else before I posted here.
You assumed you needed an EOF character when you apparently do not. You got ifstream and ofstream confused; I said ofstream and you said ifstream. These two things made it difficult for me to help you. By the time Einar got involved, we had gotten the assumptions and such straightened out.

If you had acknowledged what I said, then we could have proceeded from there in a manner similar to what Einar did.






Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

Simple Samples

einaros wrote:
Of course I could have been wrong in my assumption, but I figured with the current state of the thread I may as well present it.
Yes, things had become clear when you answered.

Also, I prefer not to assume; I try to get clarification first. By the time you answered, your assumptions were reasonably certain to be accurate.






Re: Visual C++ General How do a create a new text file, and how do I insert an eof into it?

karist

insert EOF:
need to casting: (StreamWriter SW)
SW.Write((char) x01a);