Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,
- int MAX_TOKENS_PER_LINE,char DELIMITER);
- int main(int argc, const char * argv[]) {
- std::string Input_File("Input.txt");
- const int MAX_CHARS_PER_LINE = 1200;
- const int MAX_TOKENS_PER_LINE = 40;
- const char* const DELIMITER = " ";
- std::ifstream inp(Input_File, std::ios::in | std::ios::binary);
- if(!inp) {
- std::cout << "Cannot Open " << Input_File << std::endl;
- return 1; // Terminate program
- }
- char *token;
- // read each line of the file
- while (!inp.eof())
- {
- Line_Parse(token,MAX_CHARS_PER_LINE,MAX_TOKENS_PER_LINE,
- *DELIMITER);
- }
- inp.close();
- return 0;
- }
- void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,
- int MAX_TOKENS_PER_LINE,char DELIMITER)
- {
- // read an entire line into memory
- char buf[MAX_CHARS_PER_LINE];
- inp.getline(buf, MAX_CHARS_PER_LINE);
- // parse the line into blank-delimited tokens
- int n = 0; // a for-loop index
- // array to store memory addresses of the tokens in buf
- *&token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
- // parse the line
- token[0] = *strtok(buf, &DELIMITER); // first token
- if (token[0]) // zero if line is blank
- {
- for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
- {
- token[n] = *strtok(0, &DELIMITER); // subsequent tokens
- if (!token[n]) break; // no more tokens
- }
- }
- }
- void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,
- int MAX_TOKENS_PER_LINE,char DELIMITER, std::ifstream inp)
- Line_Parse(token, MAX_CHARS_PER_LINE, MAX_TOKENS_PER_LINE, *DELIMITER, inp);
- void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,int MAX_TOKENS_PER_LINE,
- char DELIMITER, std::ifstream & inp);
- Line_Parse(token, MAX_CHARS_PER_LINE, MAX_TOKENS_PER_LINE, *DELIMITER, Input_File);
Add Comment
Please, Sign In to add comment