Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. 2015,Star Wars,DVD,120
  2. 1987,Star wars,DVD,110
  3. 2010,Inception,DVD,100
  4.  
  5. store.add(year,name,media,length)
  6.  
  7. public static void readFromFile(String filename,Store store){
  8. try {
  9. Scanner ps = new Scanner(new BufferedInputStream(new FileInputStream(filename)));
  10. while(ps.hasNextLine()){
  11. String line = ps.nextLine();
  12. String field[] = line.split(",");
  13. store.add(Integer.parseInt(field[0]),field[1],field[2],Integer.parseInt([3]));
  14. }
  15.  
  16. ps.close();
  17. } catch(Exception e){
  18. System.out.println(e.getMessage());
  19. }
  20. }
  21.  
  22. void readMovieDataFromFile(vector<movie> &movies) {
  23.  
  24. fstream inputfile;
  25. std::string line;
  26. inputfile.open("C:\movie.dat", ios::out | ios::in );
  27.  
  28. std::string token;
  29.  
  30. while (std::getline(inputfile, line)) {
  31. std::istringstream iss(line);
  32. while (std::getline(iss, token, ','))
  33. {
  34. movies[i].setYear(atoi(token.c_str()));
  35.  
  36. std::getline(iss, token, ',');
  37. movies[i].setName(token);
  38.  
  39. std::getline(iss, token, ',');
  40. movies[i].setMedia(token);
  41.  
  42. std::getline(iss, token, ',');
  43. movies[i].setLength(atoi(token.c_str()));
  44. }
  45. i++;
  46. }
  47. inputfile.close();
  48. }
  49.  
  50. struct movie {
  51. int year;
  52. std::string name;
  53. std::string media;
  54. int length;
  55. };
  56.  
  57. std::istream &operator>>(std::istream &is, movie &m) {
  58. std::string line;
  59. std::getline(is, line);
  60. std::istringstream buffer(line);
  61.  
  62. char ignore;
  63. buffer >> m.year >> ignore;
  64. std::getline(buffer, m.name, ',');
  65. std::getline(buffer, m.media, ',');
  66. buffer >> m.length;
  67. return is;
  68. }
  69.  
  70. std::vector<movie> movies {
  71. std::istream_iterator<movie>(infile),
  72. std::istream_iterator<movie>() };
  73.  
  74. void readMovieDataFromFile() {
  75.  
  76. fstream inputfile;
  77. std::string line;
  78. inputfile.open("filmy.dat", ios::in);
  79.  
  80.  
  81.  
  82. while (std::getline(inputfile, line)) {
  83. std::istringstream iss(line);
  84. std::string token;
  85. while (std::getline(iss, token, ','))
  86. {
  87.  
  88. int a = atoi(token.c_str());
  89.  
  90. std::getline(iss, token, ',');
  91. char *y = new char[token.length() + 1];
  92. std::strcpy(y, token.c_str());
  93.  
  94.  
  95. std::getline(iss, token, ',');
  96. char *c = new char[token.length() + 1];
  97. std::strcpy(c, token.c_str());
  98.  
  99.  
  100. std::getline(iss, token, ',');
  101. int b = atoi(token.c_str());
  102. store.add(a, y, c, b);
  103.  
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement