Read from text file, capture text, write to another text file in specific syntax

I have a text file A with the following syntax:
1
2
3
Attribute_Name, 'Path', 'Tutorial';
Attribute_Name2, 'Path2', 'Tutorial';
....


What I need to do is to read from that file, capture those 3 values: Attribute Name, Path and Project Name (tutorial in that case) and write it to output text file, B with the following syntax:
 
DELETE ATTRIBUTE "Atribute_Name" IN FOLDER "Path" FROM PROJECT "Tutorial";

and repeat for as many iterations as there are lines in the input file.

Can anyone help me with that?
I like to use the string class find and substr methods to split strings.
You could read until the semicolon and then split on commas.
I will leave it as an exercise for you to modify the output from the split string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

std::vector<std::string> split(std::string str, char seperator)
{
   std::vector<std::string> result;
   
   std::string::size_type token_offset = 0;
   std::string::size_type seperator_offset = 0;
   while (seperator_offset != std::string::npos)
   {
      seperator_offset = str.find(seperator, seperator_offset);
      std::string::size_type token_length;
      if(seperator_offset == std::string::npos)
      {
         token_length = seperator_offset;
      }
      else
      {
         token_length = seperator_offset - token_offset;
         seperator_offset++;
      }
      std::string token = str.substr(token_offset, token_length);
      if (!token.empty())
      {
         result.push_back(token);
      }
      token_offset = seperator_offset;
   }
   
   return result;
}

int main(int argc, char **argv)
{
   std::fstream fin("input.dat");
   
   while(!fin.eof())
   {
      std::string line;
      getline(fin, line, ';');
      fin.ignore(80, '\n'); //to discard end of line
      
      std::vector<std::string> strs = split(line, ',');
      for(int i = 0; i < strs.size(); ++i)
      {
         std::cout << strs[i] << std::endl;
      }
   }
   
   fin.close();
   
   return 0;
}
Last edited on
Topic archived. No new replies allowed.