SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # a.txt
  2. name|age|character
  3. song name|view|author
  4. song name|view|author
  5. song name|view|author
  6. song name|view|author
  7.  
  8. name1|age1|character1
  9. song name|view|author
  10. song name|view|author
  11. song name|view|author
  12. song name|view|author
  13.  
  14. name2|age2|character2
  15. song name|view|author
  16. song name|view|author
  17. song name|view|author
  18.  
  19. # a.cpp
  20. #include <stdio.h>
  21. #include <iostream>
  22. #include <iostream>
  23. #include <fstream>
  24. #include <string.h>
  25.  
  26. using namespace std;
  27.  
  28. char ** split(char * textParam, char character)
  29. {
  30.     int countCharactersInText = 0;
  31.     for (int i = 0; i < strlen(textParam); i++)
  32.     {
  33.         if (textParam[i] == character)
  34.         {
  35.             countCharactersInText++;
  36.         }
  37.     }
  38.  
  39.     char ** result = new char*[countCharactersInText + 1];
  40.     char * text = new char[strlen(textParam) + 1];
  41.     text = strcpy(text, textParam);
  42.     text[strlen(textParam)] = character;
  43.  
  44.     int begin = 0;
  45.     int index = 0;
  46.     for (int i = 0; i < strlen(text); i++)
  47.     {
  48.         if (text[i] == character)
  49.         {
  50.             result[index] = new char[i - begin + 1];
  51.             for (int j = 0; j < i - begin; j++)
  52.             {
  53.                 result[index][j] = text[begin + j];
  54.             }
  55.  
  56.             begin = i + 1;
  57.             index++;
  58.         }
  59.     }
  60.  
  61.     return result;
  62. }
  63.  
  64. int main()
  65. {
  66.  
  67.     fstream file;
  68.     file.open("./a.txt", ios::in);
  69.     if (file.is_open())
  70.     {
  71.         char line[100];
  72.         bool isArtistLine = true;
  73.         while (!file.eof())
  74.         {
  75.             file.getline(line, sizeof(file));
  76.             if (strcmp(line, "") == 0)
  77.             {
  78.                 isArtistLine = true;
  79.                 file.getline(line, sizeof(file));
  80.                 std::cout << "-------------------------" << std::endl;
  81.             }
  82.  
  83.             if (isArtistLine)
  84.             {
  85.                 // read artist
  86.                 char ** properties = split(line, '|');
  87.                 cout << "artist : " << endl;
  88.                 cout << "    " << properties[0] << endl;
  89.                 cout << "    " << properties[1] << endl;
  90.                 cout << "    " << properties[2] << endl;
  91.                 cout << "songs : " << endl;
  92.                 isArtistLine = false;
  93.             }
  94.             else
  95.             {
  96.                 // read song
  97.                 char ** properties = split(line, '|');
  98.                 cout << "    " << properties[0] << endl;
  99.                 cout << "    " << properties[1] << endl;
  100.                 cout << "    " << properties[2] << endl;
  101.             }
  102.         }
  103.  
  104.         std::cout << "------END-----";
  105.  
  106.         file.close();
  107.     }
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top