Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- int main()
- {
- // How to create a notepad file
- // This is a tricky part because everything that you
- // make will be found locally in your programming folder.
- // If you are using Visual Studio, this is the folder
- // in where your projects are found.
- // So, how do we find this?
- // Go to the folder where Visual Studio is installed or stores
- // your programs
- // Generally, it will look like this
- // C:\Users\bhok\Documents\Visual Studio 2017
- // My files are actually found in the Documents folder
- // You may have to fish for your location
- // Afterwards, you should see a "Projects" folder and the names
- // of your previous program found
- // If this project is called objects, then open the folder and look for the cpp
- // A screenshot is provided to help find the folder
- // The project's folder is where your text will be found. Keep in open.
- // How to create a text file
- // This whole line will create the text file
- std::fstream hotdog("hotdog2.txt", std::ios::out);
- //std::fstream is the namespace that we will be using
- //hotdog is the object is created
- // "hotdog2.txt" is the name and type of file created
- // std::ios::out is the standard input output stream that outputs the stream
- // We can also declare an object for fstream as seen here
- // No parameters are needed when declaring
- std::fstream hotdog1;
- // The first parameter is required for fstream to run
- // If there is no second parameters, std::ios::out is setted by default
- std::fstream hotdog2("sweetTea.txt");
- // Now how would we write text into the text file created?
- std::fstream raspberry("pie.txt", std::ios::app);
- // This will create the string
- // If you would like, treat std::string as like a data variable in your mind
- // Like int, char, etc
- // outSide cake would the name of object
- std::string outsideCake = "monkeys are inside";
- // Now when we send outsideCake into the raspberry stream and ta-da
- // It should work
- raspberry << outsideCake;
- // Try opening up the pie text and seeing what's inside!
- // So now, we can write text into the text file.
- // Shouldn't we learn how to read the text onto our screen now?
- // Do you see the differences here?
- std::fstream blueberry("pie.txt", std::ios::in);
- // What is happenining here? Run this before the next section
- std::string firstWord;
- blueberry >> firstWord;
- std::cout << firstWord << std::endl;
- // Type this this now and run it
- std::string secondWord;
- blueberry >> firstWord >> secondWord;
- std::cout << firstWord << std::endl << secondWord;
- // Did you see what happened here?
- // Now what if we wanted to read everything on the screen?
- std::cout << "read everything line\n";
- while (!blueberry.eof())
- {
- blueberry >> firstWord;
- std::cout << firstWord;
- }
- // Practice Questions
- // Create your own text file, however edit the file on your desktop
- // and have it read onto the screen! Use multiple words and see if
- // you can get it to work!
- // Do you notice what happened here?
- // Hm, I wonder what would happen if you were to add more and change
- // the words and variables?
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement