extract data from .txt file

Hy,

I have a .txt file with lots of data packages. At the end I want to sort this data by time point and output it to an output.txt file. The problem is that I don't need all data but only data from the headers: "Forces from Reactions at Point Ties:"

How do I include this in my program. The line where the header is being read in is the following: "else if (line.find(':') != string::npos)"
Now, I need to define a bool-variable that checks, if a previously defined header is true or not.

I would appreciate if you could give me a hint how to implement this :)

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <fstream>
using namespace std; 

class myItem {
public:
myItem(int T, int S, int I, const string& L): Time(T), Section(S), Id(I), Line(L) {}
int Time, Section, Id;
string Line;
bool operator < (const myItem& o) const {
if (Section != o.Section) return Section < o.Section;
if (Id != o.Id) return Id < o.Id;
return Time < o.Time;
}
}; 
int main() {
string line;
string header1 = "FORCES FROM REACTIONS AT POINT TIES:"
string header2 = "FORCES FROM REACTIONS AT RESTRAINED FACES :"
vector <string> Sections, TableHeads;
vector <myItem> AllData;
int curTime, curSection, curId;
// INPUT
while (getline(cin,line).good()) {
cerr << line << endl;
char dummy;
if (sscanf(line.c_str()," SOLUTION AT RAMP POINT %*d TIME %d",&curTime) == 1) { 
// A new ramp point and time, restart sections
curSection = -1;
} else if (line.find(':') != string::npos) {
// A new heading
string tablehead;
getline(cin,tablehead);
curSection++;
if (Sections.size() <= curSection) {
Sections.push_back(line);
TableHeads.push_back(tablehead);
}
} else if (line.find_first_not_of("- \r\n\t")==string::npos) {
// ignore line
} else {
// data line
int curId = line[0] == '*'? (~0u)>>1 : atoi(line.c_str());
AllData.push_back(myItem(curTime,curSection,curId,line));
}
}
// SORT
sort(AllData.begin(), AllData.end());
// OUTPUT
std::ofstream output ("//gt//home//h113027//GT26//forces_moments//final1//output.txt"); 

curSection = -1; curId = -1; 
for (vector<myItem>::iterator i=AllData.begin(); i!= AllData.end(); i++) { 
if (i->Section != curSection) { 
curSection = i->Section; 
curId = -1; 
output << Sections[curSection].c_str() << std::endl; 
} 
if (i->Id != curId) { 
curId = i->Id; 
output << "\nTime " << TableHeads[curSection].c_str() << std::endl; 
} 
output << i->Time << std::endl << i->Line.c_str() << std::endl; 
} 
output.close(); 
}
Here's a hint: indent your code.

Anyway; now that's out of the way... what are you trying to do? I don't understand.
Hy chrisname,

Sorry for the late reply ;)

I have attached the input file to this folder:

http://cid-daa1966be9348935.skydrive.live.com/embedicon.aspx/.Public

The problem right now is, that the code is reading and sorting all data of this input file.
But I only need some of them. The data packages in the input file are always sorted by a header ending with ":". That's why in line 35 the code is looking for that. Now I just want to define that only data should be read in if the header corresponds to a previously defined string. For example only "Forces from Reactions at Point Ties:" should be read in and all the other data should be skipped. I think this can be solved by defining a bool-variable in line 35. But I'm really a beginner in C++, maybe there is an easier way to implement this.

Thanks so much for your help ;)
I'm still not entirely sure; I haven't read your file yet, because I'm at school.

What I think you're saying is that you have a file laid out like this:
<string 1>:<string 2>
and you only want to get string 1. The colon represents the end of string one. Right?

In such a case;
find the position of the colon
create an array with n elements, where n is the length of the string up to the colon
Then you'd do something like
1
2
while (n--)
    array[n] = *(char*)full_line.c_str();


Then array would contain string one. So you would reverse it:
1
2
3
    int i = 0, j = (int)std::string(array).length; i++, j--) {
        array[i] = array[j];
    }
(Note: I'm not sure about that code, I was kinda rushed writing it)
and finally convert it into an std::string:
std::string string_1 = std::string(array);
Topic archived. No new replies allowed.