Reading file input into an array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 1
Reputation: ericelysia is an unknown quantity at this point 
Solved Threads: 0
ericelysia ericelysia is offline Offline
Newbie Poster

Reading file input into an array

 
0
  #1
Mar 25th, 2007
I am trying to read file input into an array so I can tally up the number of times each letter appears (including the apostrophe). I am not sure what I am doing wrong, but my file data is not getting assigned to the array outside the function.

  1. #include<iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. //using::left;
  6. //using::right;
  7. using std::istream;
  8. using std::ostream;
  9. #include<string>
  10. using std::getline;
  11. #include<iomanip>
  12. using std::setw;
  13. //#include <cstdlib>
  14. //#include <ctime>
  15. #include<cstring>// prototype for strtok, strlen, strcmp, strncmp, strcpy, strncpy, strcat, strncat
  16. using std::string;
  17. #include<cctype>
  18. #include<fstream>
  19. using std::fstream;
  20. using std::ifstream;
  21. using std::ofstream;
  22. // prototypes
  23. //int readData( char *, char * );
  24. int readData( char *, char *, int [ ]);
  25. void tokenizeData( char *, char * );
  26. constchar apostrophe = '\'';
  27. char *tokenPtr;
  28. /*
  29. // initialize alaphabet array
  30. const char *alphabet[ 27 ] = { '\'', 'A', 'B', 'C', 'D',
  31. 'E', 'F', 'G', 'H', 'I',
  32. 'J','K', 'L', 'M', 'N',
  33. 'O','P', 'Q', 'R', 'S', 'T',
  34. 'U', 'V', 'W', 'X', 'Y', 'Z', '\0' };
  35. */
  36. //------------------------------------------------------------------------------
  37. int main()
  38. {
  39. /*
  40. // initialize alaphabet array
  41. char alphabet[ 28 ] = { '\'', 'A', 'B', 'C', 'D',
  42. 'E', 'F', 'G', 'H', 'I',
  43. 'J','K', 'L', 'M', 'N',
  44. 'O','P', 'Q', 'R', 'S', 'T',
  45. 'U', 'V', 'W', 'X', 'Y', 'Z', '\0' };
  46. */
  47. int letterTotals[ 28 ] = { 0 };
  48.  
  49. int numberOfWords;
  50. //int numberOfTokenizedWords;
  51. //ifstream myInFile( "data.txt" );
  52. char *myInFile = ( "data.txt" );
  53. char *wordArray = { 0 };
  54. //char *tokenizedWordArray = { 0 };
  55. //numberOfWords = readData( myInFile, wordArray );
  56. numberOfWords = readData( myInFile, wordArray, letterTotals );
  57. //cout << "\nnumberOfWords = " << numberOfWords;
  58. for( int index = 0; index < numberOfWords; index++ )
  59. {
  60. cout << "----------------------------" << wordArray[ index ] << "\n";
  61. //cout << wordArray[ index ] << "\n";
  62. }
  63.  
  64. /*
  65. numberOfTokenizedWords = tokenizeData( myInFile, tokenizedWordArray );
  66. //cout << "\nnumberOfTokenizedWords = " << numberOfTokenizedWords;
  67. for( int index = 0; index < numberOfTokenizedWords; index++ )
  68. {
  69. cout << "----------------------------" << tokenizedWordArray[ index ] << "\n";
  70. //cout << tokenizedWordArray[ index ] << "\n";
  71. }
  72. */
  73. return 0;
  74. }
  75. //------------------------------------------------------------------------------
  76. //int readData( char *dataFileName, char *wWordArray /*int letterTotals[ ] */)
  77. int readData( char *dataFileName, char *wWordArray, int letterTotals[ ] )
  78. {
  79. int wordCount = 1;
  80. char currentLetter;
  81. ifstream wMyInFile( dataFileName );
  82. int letterIndex = 0;
  83.  
  84. /*
  85. if( wMyInFile.eof() ) // if no words exist in file
  86. {
  87. return 0;
  88. }
  89. */
  90.  
  91. wWordArray = new char;
  92. int counter = 0;
  93. while( !wMyInFile.eof() )
  94. {
  95. currentLetter = wMyInFile.peek(); // look at first letter
  96.  
  97. tolower( currentLetter );
  98. while( isalpha( currentLetter ) || ( currentLetter == apostrophe ) )
  99. {
  100. //letterTotals[ wordCount ] += wMyInFile.get();
  101. //letterTotals[ counter ];
  102. cout << "\n\ncurrent letter = " << currentLetter << "\n\n";
  103. wWordArray[ wordCount ] += wMyInFile.get( ); // store letter in array
  104. currentLetter = wMyInFile.peek(); // look at the next char
  105. switch( currentLetter )
  106. {
  107. case '\'': letterTotals[ 0 ]++;
  108. break;
  109. case 'a':
  110. case 'A': letterTotals[ 1 ]++;
  111. break;
  112. case 'b':
  113. case 'B': letterTotals[ 2 ]++;
  114. break;
  115. case 'c':
  116. case 'C': letterTotals[ 3 ]++;
  117. break;
  118. case 'd':
  119. case 'D': letterTotals[ 4 ]++;
  120. break;
  121. case 'e':
  122. case 'E': letterTotals[ 5 ]++;
  123. break;
  124. case 'f':
  125. case 'F': letterTotals[ 6 ]++;
  126. break;
  127. case 'g':
  128. case 'G': letterTotals[ 7 ]++;
  129. break;
  130. case 'h':
  131. case 'H': letterTotals[ 8 ]++;
  132. break;
  133. case 'i':
  134. case 'I': letterTotals[ 9 ]++;
  135. break;
  136. case 'j':
  137. case 'J': letterTotals[ 10 ]++;
  138. break;
  139.  
  140. case 'k':
  141. case 'K': letterTotals[ 11 ]++;
  142. break;
  143. case 'l':
  144. case 'L': letterTotals[ 12 ]++;
  145. break;
  146. case 'm':
  147. case 'M': letterTotals[ 13 ]++;
  148. break;
  149. case 'n':
  150. case 'N': letterTotals[ 14 ]++;
  151. break;
  152. case 'o':
  153. case 'O': letterTotals[ 15 ]++;
  154. break;
  155. case 'p':
  156. case 'P': letterTotals[ 16 ]++;
  157. break;
  158. case 'q':
  159. case 'Q': letterTotals[ 17 ]++;
  160. break;
  161. case 'r':
  162. case 'R': letterTotals[ 18 ]++;
  163. break;
  164. case 's':
  165. case 'S': letterTotals[ 19 ]++;
  166. break;
  167. case 't':
  168. case 'T': letterTotals[ 20 ]++;
  169. break;
  170. case 'u':
  171. case 'U': letterTotals[ 21 ]++;
  172. break;
  173. case 'v':
  174. case 'V': letterTotals[ 22 ]++;
  175. break;
  176. case 'w':
  177. case 'W': letterTotals[ 23 ]++;
  178. break;
  179. case 'x':
  180. case 'X': letterTotals[ 24 ]++;
  181. break;
  182. case 'y':
  183. case 'Y': letterTotals[ 25 ]++;
  184. break;
  185. case 'z':
  186. case 'Z': letterTotals[ 26 ]++;
  187. break;
  188. default: break;
  189. }
  190. char letterValue = 44;
  191. cout << "letter: " << letterValue << "\n\n";
  192. int index = 0;
  193. cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n";
  194. letterValue = 65;
  195. for( index = 1; index < 27; index++ )
  196. {
  197. cout << "letter: " << letterValue << "\n";
  198. cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n\n";
  199. letterValue++;
  200. }
  201. }
  202.  
  203. counter++;
  204. wWordArray[ wordCount ] = '\0';
  205. //letterIndex = 0;
  206. cout << "\n----- wordCount = " << wordCount << " -----\n\n";
  207.  
  208. while( !isalpha( currentLetter ) && ( currentLetter != apostrophe ) )
  209. {
  210. wMyInFile.ignore( 1 );
  211. currentLetter = wMyInFile.peek(); // look at the next char
  212. }
  213.  
  214. wordCount++;
  215. wWordArray = new char;
  216. }
  217. return wordCount;
  218. }
  219. //------------------------------------------------------------------------------
  220. void tokenizeData( char *dataFileName, char *wTokenizedWordArray )
  221. {
  222. int tokenizedWordCount = 1;
  223. cout << "\nThe string to be tokenized is:\n " << wTokenizedWordArray
  224. << "\n\nThe tokens are: \n\n";
  225. // begin tokenization of sentence
  226. tokenPtr = strtok( wTokenizedWordArray, " " );
  227. // continue tokenizing sentence until tokenPtr becomes NULL
  228. while( tokenPtr != NULL )
  229. {
  230. cout << tokenPtr << '\n';
  231. tokenPtr = strtok( NULL, " " ); // get next token
  232. tokenizedWordCount++;
  233. }
  234. cout << "\nAfter strtok, sentence = " << wTokenizedWordArray << endl;
  235. //cout << "\nnumberOfTokenizedWords = " << numberOfTokenizedWords;
  236. for( int index = 0; index < tokenizedWordCount; index++ )
  237. {
  238. cout << "*************" << wTokenizedWordArray[ index ] << "\n";
  239. //cout << tokenizedWordArray[ index ] << "\n";
  240. }
  241. //return tokenizedWordCount;
  242. }
Last edited by WaltP; Mar 25th, 2007 at 7:51 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Reading file input into an array

 
0
  #2
Mar 25th, 2007
Sheesh! Why do you make it so complicated?

Take a look at the ASCII table. Each letter has in fact a numeric value. So use this to your advantage. Create an int array of 128 elements, as there are 128 characters in ASCII. Now look how easy it is:
  1. lookupTable['a']++;

That simply increments the value of 'a', which happens to be the value 97. So if you want to make it simple, just loop through the string, and plug each letter into the array and increment that element. Then all you have to do is print out the elements in the array, and you're done.

Also see this for why eof is bad.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Reading file input into an array

 
0
  #3
Mar 25th, 2007
it is much easier if u use the c++ std library.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iterator>
  4. #include <map>
  5. #include <algorithm>
  6. #include <cctype>
  7. using namespace std;
  8.  
  9. struct print_it
  10. {
  11. inline void operator() ( const pair<char,int>& p ) const
  12. { cout << p.first << " occurs " << p.second << " times\n" ; }
  13. };
  14.  
  15. int main()
  16. {
  17. const char* const file_name = "data.txt" ;
  18. ifstream file(file_name) ;
  19. if( !file ) return 1 ;
  20. const char apostrophe = '\'' ;
  21. map<char,int> char_cnt ;
  22. char c ;
  23. while( (file>>c) && ( isalpha(c) || (c==apostrophe) ) ) ++char_cnt[c] ;
  24. for_each( char_cnt.begin(), char_cnt.end(), print_it() ) ;
  25. return 0 ;
  26. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC