Posted By

somada141 on 03/12/10


Tagged

file c stream


Versions (?)

Using fstreams to write and read data #2


 / Published in: C++
 

  1. #include <iostream>
  2. #include <fstream> //this includes the necessary functions for file IO
  3. #include <string>
  4. using namespace std;
  5.  
  6. /* Here is another important topic. File input-output.
  7.  
  8.  In this program we are going to see how to open a text file, write something and then
  9.  read this file.
  10.  
  11.  The file IO operations are done via streams, similarly to the way we did it for input
  12.  and output in the console. However I must note that this is the case when writing files
  13.  and data in text format. We are going to see binary files later.
  14.  
  15.  As we said we use streams. Therefore we use the '<<' and '>>' operators. The main logic
  16.  is this:
  17.  
  18.  1)We create a stream suitable for an input or output file
  19.  2)We open a file and link it to the stream we created. This file must be either
  20.  for input or output and it must agree with the stream type
  21.  3)We use the '<<' or '>>' operators to write or read in/from the stream/file
  22.  4)We close the stream/file/
  23.  
  24.  See the code below to get how this is done. What we are doing in this program is that
  25.  we ask the user whether he wants to create a new file with his name and age inside or
  26.  see what has already been stored in the file. According to the user's choice we either
  27.  open a new file to write in(if this file exists it is overwritten) and write his name
  28.  and age or open an existing file (if it exists) and read his name and age.
  29.  */
  30.  
  31. int main()
  32. {
  33. string name;//Variables that hold the users name and age
  34. double age;
  35. int choice;//A control variable for the user's choice
  36.  
  37. //We ask the user what he wants to do
  38. cout<<"What do you want to do?n";
  39. cout<<"(1) Write a file with your name and agen";
  40. cout<<"(2) Read from a file your name and agen";
  41. cin >> choice;
  42.  
  43. if (choice==1){//The user wants a new file
  44.  
  45. cout<<"Please enter your name:n";//we ask the user for this name and age
  46. cin>>name;
  47. cout<<"Please enter your age:n";
  48. cin>>age;
  49.  
  50. //We create a new stream to an output file. Note the type is 'ofstream' meaning
  51. //a stream to a file for output. The name of this variable is the name of the used
  52. //stream and for us it holds the name to the file (but not the filename)
  53. ofstream output_file_stream;
  54.  
  55. /* Here we open a new file named 'file1.txt' using the 'open()' function of the
  56. stream. Note that we enclose this function in an 'if' statement. We do that because
  57. if the 'open()' function fails to open the file then it returns a 'false' otherwise
  58. if all goes well it returns a 'true'. So we use this if to inform the user as to how
  59. the opening of the file went. Alternatively we can use the 'is_open()' function which
  60. returns true if a file is open and false if is not. the syntax is 'file_stream.is_open()'
  61.  
  62. Note the syntax of the open statement:
  63.  
  64. file_stream_name.open(filename,input_or_output_identifier)
  65.  
  66. filename: the fact that we are going to write text in the file doesnot mean that
  67. the extension must be txt. It could be .dat or nothing at all or anything
  68. input_or_output_identifier: this lets the compiler know how we are going to use this
  69. file. We want to write in the file so we use ios::out.
  70. These identifiers are members of the 'ios' namespace and
  71. therefore we user the '::' operator.
  72.  
  73. There are many identifiers but we use here only 'ios::out' which means we open
  74. a file to write in (output file) and 'ios::in' which means we open a file to read
  75. from (input file)
  76. */
  77.  
  78. if (output_file_stream.open("file1.txt"),ios::out) {
  79.  
  80.  
  81. /*Note that we use the stream to the file with the '<<' operator to write
  82.   * anything we want inside just like we used the 'cout' stream for the console
  83.   *
  84.   * IMPORTANT: note the usage of 'n' between the variables. This is neccessary to distinguish
  85.   * the different variables. BEWARE: The usage of 'endl' instead of 'n' does NOT work
  86.   */
  87. output_file_stream<<name<<"n"<<age;
  88. //output_file_stream<<age;
  89. }
  90. else cout<<"The file could not be openedn";
  91.  
  92. //We close the file to complete the IO operation
  93. output_file_stream.close();
  94. }
  95. else if (choice==2){
  96.  
  97. ifstream input_file_stream;
  98. /* Here the only difference from above is that we open a file that should already
  99. exist. If it doen't then the 'open' function returns a false and the program stops
  100. with an error.
  101. If it does then we read the data with the same order as when we wrote it and we place
  102. that data in their variables.*/
  103. if (input_file_stream.open("file1.txt"),ios::in){
  104. input_file_stream>>name;
  105. input_file_stream>>age;
  106.  
  107. //We print the data from the file
  108. cout<<"Your name is '"<<name<<"' and you age is "<<age<<endl;
  109.  
  110. //We close the file
  111. input_file_stream.close();
  112.  
  113.  
  114. }
  115.  
  116. else
  117. cout<<"The file could not be openedn";
  118.  
  119. }
  120. return 0;
  121. }
  122.  

Report this snippet  

You need to login to post a comment.