.txt Files

Hi. Here is a scenario. I failed a quiz on this so I need help.
You have a .txt file
You have your source code
You need to take information from your source code and put it on the .txt file
You need the .txt file to display said information!

'Kay now where I'm going wrong is this whole fstream madness. What operators do I use to transport a bundle of code onto a .txt file (if that is even possible)? What operator is used to display the .txt file after I have done that? As far as I can tell you have some sort of data type associated with the fstream (library?). And you create an instance of that data type and do...

Edit: What does fstream even denote... or mean...?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
//Some integer called "random." & I'm not sure what fstream denotes
int random;
fstream some_file;

fstream some_file.open("text.txt"); //Does the file text.txt need to already have been opened? If not will this open it?

return 0;
}


Thanks in advance to anyone available to assist!
closed account (3qX21hU5)
What operators do I use to transport a bundle of code onto a .txt file (if that is even possible)?


fstream is just like any other stream. You use it the same way you use cout and cin statements.

Lets say you want to write 10 lines that say "Hello!" to the console. How would you do that? Most likely you would do something like this.

1
2
for (int i = 0; i != 10; ++i)
    std::cout << "Hello!" << std::endl;


Easy right?

Now how would we do the same thing but instead write 10 lines of "Hello!" to a text file? Well it is basically the same exact thing.

1
2
3
4
std::ofstream output("test.txt");  // Open the text file

for (int i = 0; i != 10; ++i)
    output << "Hello!" << std::endl;  // We use output just like it was a cout statement 


Pretty simple :). We basically use a ofstream like we would use cout but instead of displaying to the console as we do with std::cout we are putting the information into whatever file we opened with our ofstream object.

Now there is some other things you should know about fstream and I would recommend you head on over to the tutorial on this site that talks about it. It should give you a good grasp of using ofstream and ifstream http://www.cplusplus.com/doc/tutorial/files/


What operator is used to display the .txt file after I have done that?


Now when you say display do you mean open that said .txt file in like notepad? Or display it's contents in the console? I am thinking your professor means the latter.

To display things from a file on the console you would use the ifstream objects. They work just like ofstream does but instead of writing stuff to a file they read stuff from a file.

For example.

1
2
3
4
5
std::ifstream fromFile("testing.txt");   // Stream which will read from a file
std::string word;                                 // Variable that will hold each word from the file.

while (fromFile >> word)
    std::cout << word << std::endl;     // Display every word from the file. 


Again I would check out the tutorial on this site to a more in depth answer on how to do this and how it works.


fstream some_file.open("text.txt"); //Does the file text.txt need to already have been opened? If not will this open it?

No that file text.txt doesn't already need to be opened. And yes that code will open that file but not in the way I believe you are thinking. It won't open the file in notepad or anything like that so that you can see it. What it is doing is giving your program access to what that file contains and also giving it access to write things to that file.
Last edited on
closed account (jwkNwA7f)
It looks like Zereo has explained it well, but I am still going to post a link to the tutorial on how to use fstream on this site:
http://www.cplusplus.com/doc/tutorial/files/

EDIT: Just noticed Zereo posted the link too.
Last edited on
I am so sorry for my lack of response. I just got out of a lab course.

Thank you both for your responses! Ok, so I have hit another brick wall with this std operator you have using. I haven't yet used it in either my 190 course or the 226 course I am taking. I don't think I've seen it in any of my professor's source code either.

At any rate, I think I've interpreted the assignment incorrectly. So if what you're saying is true, then after the 10 lines of "Hello!" are passed to the .txt file does it then close it and save it without ever opening it? Or is nothing ever saved? But I guess I wouldn't want the information to be saved, because when I compile (for test runs) it I'd have to clear it again to turn in to the professor.

I was under the impression that we were supposed to open the notepad program via source code written on MVS.
Last edited on
closed account (3qX21hU5)
Thank you both for your responses! Ok, so I have hit another brick wall with this std operator you have using. I haven't yet used it in either my 190 course or the 226 course I am taking. I don't think I've seen it in any of my professor's source code either.


That is just something that I do. It has to do with namespaces which you will learn about later in your classes. Though for now just think of it as the same as when you declare using namespace std; at the top of your files. By that I mean you can either include using namespace std; once at the top or use std:: for everything that is in the std:: namespace. But again it doesn't really matter at this point and you will learn about it later on.

At any rate, I think I've interpreted the assignment incorrectly. So if what you're saying is true, then after the 10 lines of "Hello!" are passed to the .txt file does it then close it and save it without ever opening it? Or is nothing ever saved?


Yes it closes the file and saves the file when the program exits without ever opening the .txt in notepad or another program like it. Though if you ever need to close the file before your program exits you can call .close on the stream that opened the file. For example using my example above we could close it like so.

1
2
3
4
5
6
7
std::ofstream output("test.txt");  // Open the text file

for (int i = 0; i != 10; ++i)
    output << "Hello!" << std::endl;  // We use output just like it was a cout statement

// This will close the file and save it.
output.close();


Here is all the methods for ofstream and what they do http://www.cplusplus.com/reference/fstream/ofstream/

But I guess I wouldn't want the information to be saved, because when I compile (for test runs) it I'd have to clear it again to turn in to the professor.


As default whatever you run the program this is what it will do. It will open the file and delete everything that is already in it before writing to it again. So if you just ran the program and it put in a bunch of info into the .txt file. And then you ran your program again right after, it would delete the information that is already in the .txt file that was in there from the last time you ran the program and then it would precede to write the information again.

So basically every time you run the program it overwrites whatever is already in the text file.

I was under the impression that we were supposed to open the notepad program via source code written on MVS.


You might be correct but that is a bit more advanced then what you are on right now so I don't think that is what they are asking for. Though I would check with the professor just to be sure.
Last edited on
Topic archived. No new replies allowed.