Advertisement
bhok

5.1 (TRUE) Sequential File

Jul 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7.     // How to create a notepad file
  8.  
  9.     // This is a tricky part because everything that you
  10.     // make will be found locally in your programming folder.
  11.     // If you are using Visual Studio, this is the folder
  12.     // in where your projects are found.
  13.     // So, how do we find this?
  14.  
  15.     // Go to the folder where Visual Studio is installed or stores
  16.     // your programs
  17.     // Generally, it will look like this
  18.     // C:\Users\bhok\Documents\Visual Studio 2017
  19.  
  20.     // My files are actually found in the Documents folder
  21.     // You may have to fish for your location
  22.     // Afterwards, you should see a "Projects" folder and the names
  23.     // of your previous program found
  24.     // If this project is called objects, then open the folder and look for the cpp
  25.     // A screenshot is provided to help find the folder
  26.     // The project's folder is where your text will be found. Keep in open.
  27.  
  28.     // How to create a text file
  29.  
  30.     // This whole line will create the text file
  31.     std::fstream hotdog("hotdog2.txt", std::ios::out);
  32.  
  33.     //std::fstream is the namespace that we will be using
  34.     //hotdog is the object is created
  35.     // "hotdog2.txt" is the name and type of file created
  36.     // std::ios::out is the standard input output stream that outputs the stream
  37.  
  38.     // We can also declare an object for fstream as seen here
  39.     // No parameters are needed when declaring
  40.     std::fstream hotdog1;
  41.  
  42.     // The first parameter is required for fstream to run
  43.     // If there is no second parameters, std::ios::out is setted by default
  44.     std::fstream hotdog2("sweetTea.txt");
  45.  
  46.     // Now how would we write text into the text file created?
  47.  
  48.     std::fstream raspberry("pie.txt", std::ios::app);
  49.  
  50.     // This will create the string
  51.     // If you would like, treat std::string as like a data variable in your mind
  52.     // Like int, char, etc
  53.     // outSide cake would the name of object
  54.     std::string outsideCake = "monkeys are inside";
  55.  
  56.     // Now when we send outsideCake into the raspberry stream and ta-da
  57.     // It should work
  58.     raspberry << outsideCake;
  59.  
  60.     // Try opening up the pie text and seeing what's inside!
  61.  
  62.     // So now, we can write text into the text file.
  63.     // Shouldn't we learn how to read the text onto our screen now?
  64.  
  65.     // Do you see the differences here?
  66.     std::fstream blueberry("pie.txt", std::ios::in);
  67.  
  68.     // What is happenining here? Run this before the next section
  69.     std::string firstWord;
  70.     blueberry >> firstWord;
  71.     std::cout << firstWord << std::endl;
  72.  
  73.     // Type this this now and run it
  74.     std::string secondWord;
  75.     blueberry >> firstWord >> secondWord;
  76.     std::cout << firstWord << std::endl << secondWord;
  77.  
  78.     // Did you see what happened here?
  79.  
  80.     // Now what if we wanted to read everything on the screen?
  81.    
  82.     std::cout << "read everything line\n";
  83.     while (!blueberry.eof())
  84.     {
  85.         blueberry >> firstWord;
  86.         std::cout << firstWord;
  87.     }
  88.  
  89.     // Practice Questions
  90.  
  91.     // Create your own text file, however edit the file on your desktop
  92.     // and have it read onto the screen! Use multiple words and see if
  93.     // you can get it to work!
  94.  
  95.  
  96.     // Do you notice what happened here?
  97.     // Hm, I wonder what would happen if you were to add more and change
  98.     // the words and variables?
  99.  
  100.  
  101.     system("pause");
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement