Game Programming



Visit Base Classics - Gaming Chairs

 
game programming
Game Programming :: Programming :: General Programming :: Code Examples :: [C++] Text Files - Intro to fstream - View Topic
Topic Rating: *****
Printable View
Andy
Administrator
*****

Posts: 40
Status: Offline
Gender: Male
Joined:  
pm

Reputation: 0%

[C++] Text Files - Intro to fstream (11th Jan 08 at 2:06am UTC)
Level: Beginner (some experience)

Many programs need to work with other files, such as word processors. This program introduces the fstream library and shows you how to open a text file (extension *.txt), and count the number of words in the file. Notice how the results of the run are printed to the console window. However, you could also code the program to put the results in a new text file.

Note: I have added additional information about the function definitions. Technically, you should have a function comment header for each function in your program.


Code:
 
  1. //********************************************************************
  2. //
  3. // Word Count Program
  4. //
  5. // A program to count the number of words in a text file
  6. //
  7. //********************************************************************
  8.  
  9. #include <cstdlib>
  10. #include <fstream>
  11. #include <iostream>
  12. #include <string>
  13.  
  14. #define cls system ("cls")
  15. #define pause system ("pause")
  16.  
  17. using namespace std;
  18.  
  19. //
  20. // function prototypes
  21. //
  22. string getFileName ();
  23.  
  24. int wordCount (ifstream &txt);
  25.  
  26. // start of main
  27. int main ()
  28. {
  29. int totalWords;
  30.  
  31. cout << "Word Count Program launched..." << "\n\n";
  32.  
  33. pause;
  34.  
  35. cls;
  36.  
  37. ifstream txtFile; // handle for the text file
  38.  
  39. string fileName = getFileName ();
  40.  
  41. txtFile.open (fileName.c_str ());
  42.  
  43. if (!txtFile.is_open ()) // tests if file opening failed
  44. {
  45. cerr << "Could not open file: " << fileName << "\n\n";
  46.  
  47. exit (1);// exits program
  48. }
  49.  
  50. cls;
  51.  
  52. totalWords = wordCount ( txtFile );
  53.  
  54. //
  55. // print fileName and totalWords of text file to screen
  56. //
  57. cout << "FQN of File:" << '\n' << fileName << "\n\n";
  58.  
  59. cout << "Word Total of Text File = " << totalWords << "\n\n";
  60.  
  61. txtFile.close(); // closes the text file
  62.  
  63. return 0;
  64. }// end of main
  65.  
  66.  
  67. //********************************************************************
  68. //
  69. // Function Name: getFileName
  70. //
  71. // Purpose: To get the FQN (Fully Qualified Name) of a text file
  72. // from the user
  73. //
  74. // Input Parameters: none
  75. //
  76. // Output Parameters: none
  77. //
  78. // Return Value: fN is assigned as the value for fileName in main
  79. //
  80. //********************************************************************
  81.  
  82. string getFileName ()
  83. {
  84. string fN;
  85.  
  86. cout << "Enter the fully qualified name of the text file." <<
  87. " (i.e. must include entire path" << "\n\n" <<
  88. "of file): ";
  89.  
  90. cin >> ws;
  91.  
  92. getline (cin, fN);
  93.  
  94. cout << "\n\n";
  95.  
  96. return (fN);
  97. }// getFileName
  98.  
  99.  
  100. //********************************************************************
  101. //
  102. // Function Name: wordCount
  103. //
  104. // Purpose: To count the number of words in a text file
  105. // (note: "word" refers to characters separated by
  106. // whitespace)
  107. //
  108. // Input Parameters: txt- a file handle pointing (pass by reference)
  109. // to the (input) text file
  110. //
  111. // Output Parameters: none
  112. //
  113. // Return Value: wC is assigned as the value for totalWords in main
  114. //
  115. //********************************************************************
  116.  
  117. int wordCount (ifstream &txt)
  118. {
  119. int wC = 0;
  120.  
  121. char ch,
  122. word = 'O';
  123.  
  124. while ( txt.get(ch) )
  125. {
  126. if (ch == ' ' || ch == '\n' || ch == '\t')
  127. {
  128. word = 'O';
  129. }
  130. else if (word == 'O')
  131. {
  132. wC++;
  133.  
  134. word = 'I';
  135. }
  136. }// end of loop
  137.  
  138. return (wC);
  139. }// wordCount
  140.  
  141.  
 
 Printable View

All times are GMT+0 :: The current time is 7:56am
Page generated in 0.4583 seconds
Purchase Ad-Free Credits
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums