Guest User

Untitled

a guest
Dec 18th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,
  2. int MAX_TOKENS_PER_LINE,char DELIMITER);
  3.  
  4. int main(int argc, const char * argv[]) {
  5.  
  6. std::string Input_File("Input.txt");
  7. const int MAX_CHARS_PER_LINE = 1200;
  8. const int MAX_TOKENS_PER_LINE = 40;
  9. const char* const DELIMITER = " ";
  10.  
  11. std::ifstream inp(Input_File, std::ios::in | std::ios::binary);
  12. if(!inp) {
  13. std::cout << "Cannot Open " << Input_File << std::endl;
  14. return 1; // Terminate program
  15. }
  16. char *token;
  17. // read each line of the file
  18. while (!inp.eof())
  19. {
  20. Line_Parse(token,MAX_CHARS_PER_LINE,MAX_TOKENS_PER_LINE,
  21. *DELIMITER);
  22. }
  23. inp.close();
  24. return 0;
  25. }
  26.  
  27. void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,
  28. int MAX_TOKENS_PER_LINE,char DELIMITER)
  29. {
  30. // read an entire line into memory
  31. char buf[MAX_CHARS_PER_LINE];
  32. inp.getline(buf, MAX_CHARS_PER_LINE);
  33.  
  34. // parse the line into blank-delimited tokens
  35. int n = 0; // a for-loop index
  36.  
  37. // array to store memory addresses of the tokens in buf
  38. *&token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
  39.  
  40. // parse the line
  41. token[0] = *strtok(buf, &DELIMITER); // first token
  42. if (token[0]) // zero if line is blank
  43. {
  44. for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
  45. {
  46. token[n] = *strtok(0, &DELIMITER); // subsequent tokens
  47. if (!token[n]) break; // no more tokens
  48. }
  49. }
  50. }
  51.  
  52. void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,
  53. int MAX_TOKENS_PER_LINE,char DELIMITER, std::ifstream inp)
  54.  
  55. Line_Parse(token, MAX_CHARS_PER_LINE, MAX_TOKENS_PER_LINE, *DELIMITER, inp);
  56.  
  57. void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,int MAX_TOKENS_PER_LINE,
  58. char DELIMITER, std::ifstream & inp);
  59.  
  60. Line_Parse(token, MAX_CHARS_PER_LINE, MAX_TOKENS_PER_LINE, *DELIMITER, Input_File);
Add Comment
Please, Sign In to add comment