Advertisement
Guest User

Untitled

a guest
Oct 26th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. ifstream ifs ( path );
  2. string * in_file;
  3. int count = 0;
  4. while ( !ifs.eof() )
  5. {
  6. ++count;
  7. if ( count == 1 )
  8. {
  9. in_file = new string[1];
  10. }
  11. else
  12. {
  13. // Dynamically allocate another space in the stack
  14. string *old_in_file = in_file;
  15. in_file = new string[count];
  16. // Copy over values
  17. for ( int i = 0 ; i < ( count - 1 ) ; i++ )
  18. {
  19. in_file[i] = old_in_file[i];
  20. }
  21. delete[] old_in_file;
  22. }
  23. // After doing some debugging I know this is the problem what am I
  24. // doing wrong with it?
  25. getline(ifs,in_file[count - 1]);
  26. }
  27.  
  28. Hello
  29. Bye
  30. See you later
  31.  
  32. in_file [0] = Hello
  33. in_file [1] = Bye
  34. in_file [2] = See you later
  35.  
  36. std::string str;
  37.  
  38. std::vector <std::string> vec;
  39.  
  40. while ( std::getline(ifs,str) )
  41. {
  42. vec.push_back(str) ;
  43. }
  44.  
  45. for(size_t i=0;i<vec.size();i++)
  46. in_file[i] = vec[i];
  47.  
  48. while ( !ifs.eof() )
  49.  
  50. while ( ifs.good() )
  51.  
  52. std::string line;
  53. while ( std::getline(ifs, line) ) {
  54. if (line.empty()) // be careful: an empty line might be read
  55. continue;
  56. ...
  57. }
  58.  
  59. std::string word;
  60. while ( ifs >> word ) {
  61. ...
  62. }
  63.  
  64. std::ifstream ifs(path);
  65. std::vector<std::string> lines;
  66.  
  67. std::string line;
  68. while ( std::getline(ifs, line) )
  69. {
  70. // skip empty lines:
  71. if (line.empty())
  72. continue;
  73.  
  74. lines.push_back(line);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement