| | |
Reading file input into an array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 1
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#include<iostream> using std::cin; using std::cout; using std::endl; //using::left; //using::right; using std::istream; using std::ostream; #include<string> using std::getline; #include<iomanip> using std::setw; //#include <cstdlib> //#include <ctime> #include<cstring>// prototype for strtok, strlen, strcmp, strncmp, strcpy, strncpy, strcat, strncat using std::string; #include<cctype> #include<fstream> using std::fstream; using std::ifstream; using std::ofstream; // prototypes //int readData( char *, char * ); int readData( char *, char *, int [ ]); void tokenizeData( char *, char * ); constchar apostrophe = '\''; char *tokenPtr; /* // initialize alaphabet array const char *alphabet[ 27 ] = { '\'', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 'O','P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0' }; */ //------------------------------------------------------------------------------ int main() { /* // initialize alaphabet array char alphabet[ 28 ] = { '\'', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 'O','P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0' }; */ int letterTotals[ 28 ] = { 0 }; int numberOfWords; //int numberOfTokenizedWords; //ifstream myInFile( "data.txt" ); char *myInFile = ( "data.txt" ); char *wordArray = { 0 }; //char *tokenizedWordArray = { 0 }; //numberOfWords = readData( myInFile, wordArray ); numberOfWords = readData( myInFile, wordArray, letterTotals ); //cout << "\nnumberOfWords = " << numberOfWords; for( int index = 0; index < numberOfWords; index++ ) { cout << "----------------------------" << wordArray[ index ] << "\n"; //cout << wordArray[ index ] << "\n"; } /* numberOfTokenizedWords = tokenizeData( myInFile, tokenizedWordArray ); //cout << "\nnumberOfTokenizedWords = " << numberOfTokenizedWords; for( int index = 0; index < numberOfTokenizedWords; index++ ) { cout << "----------------------------" << tokenizedWordArray[ index ] << "\n"; //cout << tokenizedWordArray[ index ] << "\n"; } */ return 0; } //------------------------------------------------------------------------------ //int readData( char *dataFileName, char *wWordArray /*int letterTotals[ ] */) int readData( char *dataFileName, char *wWordArray, int letterTotals[ ] ) { int wordCount = 1; char currentLetter; ifstream wMyInFile( dataFileName ); int letterIndex = 0; /* if( wMyInFile.eof() ) // if no words exist in file { return 0; } */ wWordArray = new char; int counter = 0; while( !wMyInFile.eof() ) { currentLetter = wMyInFile.peek(); // look at first letter tolower( currentLetter ); while( isalpha( currentLetter ) || ( currentLetter == apostrophe ) ) { //letterTotals[ wordCount ] += wMyInFile.get(); //letterTotals[ counter ]; cout << "\n\ncurrent letter = " << currentLetter << "\n\n"; wWordArray[ wordCount ] += wMyInFile.get( ); // store letter in array currentLetter = wMyInFile.peek(); // look at the next char switch( currentLetter ) { case '\'': letterTotals[ 0 ]++; break; case 'a': case 'A': letterTotals[ 1 ]++; break; case 'b': case 'B': letterTotals[ 2 ]++; break; case 'c': case 'C': letterTotals[ 3 ]++; break; case 'd': case 'D': letterTotals[ 4 ]++; break; case 'e': case 'E': letterTotals[ 5 ]++; break; case 'f': case 'F': letterTotals[ 6 ]++; break; case 'g': case 'G': letterTotals[ 7 ]++; break; case 'h': case 'H': letterTotals[ 8 ]++; break; case 'i': case 'I': letterTotals[ 9 ]++; break; case 'j': case 'J': letterTotals[ 10 ]++; break; case 'k': case 'K': letterTotals[ 11 ]++; break; case 'l': case 'L': letterTotals[ 12 ]++; break; case 'm': case 'M': letterTotals[ 13 ]++; break; case 'n': case 'N': letterTotals[ 14 ]++; break; case 'o': case 'O': letterTotals[ 15 ]++; break; case 'p': case 'P': letterTotals[ 16 ]++; break; case 'q': case 'Q': letterTotals[ 17 ]++; break; case 'r': case 'R': letterTotals[ 18 ]++; break; case 's': case 'S': letterTotals[ 19 ]++; break; case 't': case 'T': letterTotals[ 20 ]++; break; case 'u': case 'U': letterTotals[ 21 ]++; break; case 'v': case 'V': letterTotals[ 22 ]++; break; case 'w': case 'W': letterTotals[ 23 ]++; break; case 'x': case 'X': letterTotals[ 24 ]++; break; case 'y': case 'Y': letterTotals[ 25 ]++; break; case 'z': case 'Z': letterTotals[ 26 ]++; break; default: break; } char letterValue = 44; cout << "letter: " << letterValue << "\n\n"; int index = 0; cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n"; letterValue = 65; for( index = 1; index < 27; index++ ) { cout << "letter: " << letterValue << "\n"; cout << "letterTotals[ " << index << " ] = " << letterTotals[ index ] << "\n\n"; letterValue++; } } counter++; wWordArray[ wordCount ] = '\0'; //letterIndex = 0; cout << "\n----- wordCount = " << wordCount << " -----\n\n"; while( !isalpha( currentLetter ) && ( currentLetter != apostrophe ) ) { wMyInFile.ignore( 1 ); currentLetter = wMyInFile.peek(); // look at the next char } wordCount++; wWordArray = new char; } return wordCount; } //------------------------------------------------------------------------------ void tokenizeData( char *dataFileName, char *wTokenizedWordArray ) { int tokenizedWordCount = 1; cout << "\nThe string to be tokenized is:\n " << wTokenizedWordArray << "\n\nThe tokens are: \n\n"; // begin tokenization of sentence tokenPtr = strtok( wTokenizedWordArray, " " ); // continue tokenizing sentence until tokenPtr becomes NULL while( tokenPtr != NULL ) { cout << tokenPtr << '\n'; tokenPtr = strtok( NULL, " " ); // get next token tokenizedWordCount++; } cout << "\nAfter strtok, sentence = " << wTokenizedWordArray << endl; //cout << "\nnumberOfTokenizedWords = " << numberOfTokenizedWords; for( int index = 0; index < tokenizedWordCount; index++ ) { cout << "*************" << wTokenizedWordArray[ index ] << "\n"; //cout << tokenizedWordArray[ index ] << "\n"; } //return tokenizedWordCount; }
Last edited by WaltP; Mar 25th, 2007 at 7:51 am.
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:
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.
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:
c Syntax (Toggle Plain Text)
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."
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
it is much easier if u use the c++ std library.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <iterator> #include <map> #include <algorithm> #include <cctype> using namespace std; struct print_it { inline void operator() ( const pair<char,int>& p ) const { cout << p.first << " occurs " << p.second << " times\n" ; } }; int main() { const char* const file_name = "data.txt" ; ifstream file(file_name) ; if( !file ) return 1 ; const char apostrophe = '\'' ; map<char,int> char_cnt ; char c ; while( (file>>c) && ( isalpha(c) || (c==apostrophe) ) ) ++char_cnt[c] ; for_each( char_cnt.begin(), char_cnt.end(), print_it() ) ; return 0 ; }
![]() |
Similar Threads
- Reading in a *.csv file and loading the data into an Array (Java)
- Still need help with opening a file and store the data on it in an array (C++)
- Storing file input to an array? (C)
- Reading in file input up to newline (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Reading from a file to fill an array (Java)
- Error Message Concerning Reading File From A Drive (C++)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: connecting VC++ w/ MSSQL 2005
- Next Thread: Here is the doc file for the assignment
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






