How to get a sequential data from a string
Aug 11, 2016 at 1:21am UTC
I have nearly everything in working order here. I just need the last variable in this. I have it commented (unlike anything else in the program ;P). Being as I am a beginner it should be pretty easy to relate to the method of the code. If you're a beginner you might pick something up about frameworks (storing data in asynchronous positions). The code itself here is a function to get the last 2 segments of code from a compressed file the code makes. It grabs the portionfour at the bottom and gets the `PN` from the code before. That was after 2 previous functions looked through the file.
The only thing I'm lacking is how to turn the last of the file, which is sequential digits into a unsigned int. I took a turn at it, and I need to know how to exercise it. Hence:
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
void Node::extr_third(ifstream& in, unsigned int counter) {
char d;
int c = 10;
filebuf* pbuf = in.rdbuf();
in.seekg(0,in.end);
long length = in.tellg();
in.seekg(0,in.beg);
// get file size using buffer's members
size_t size = pbuf->pubseekoff (0,in.end,in.in);
pbuf->pubseekpos (0,in.in);
// allocate memory to contain file data
char * mybuffer=new char [size];
// get file data
pbuf->sgetn (mybuffer,size);
int v, i;
long b = 0;
char redline[] = { "thisisthestartofportionfour" };
int frsize = sizeof (redline)/sizeof (char );
char redcheck[frsize] = {};
while (counter != length) {
if (counter/length >= c/33) {
c += 10;
cout << "X" << flush;
}
for (int i = 0; i < frsize; ++i)
redcheck[i] = mybuffer[counter+i];
if (string(redcheck) == string(redline) && ! STOP) {
STOP = 1;
}
else if (! STOP)
PN.push_back(static_cast <unsigned int >(mybuffer[counter++]));
else if (STOP) {
if (isdigit(mybuffer[counter])) {
cd[v] = mybuffer[counter++];
v++;
}
else
counter++;
}
}
totalCnt = 0;
for (int x = 0;x < v;x++) {
int h = cd[x];
totalCnt = totalCnt + (h * pow(10,v-x));
}
cout << endl << totalCnt << flush;
}
Last edited on Aug 11, 2016 at 3:29pm UTC
Aug 11, 2016 at 1:40am UTC
The problem is I get this:
C:\cygwin64\bin>press -d
Press, Copyright Aunk 2016
Input File: press.cgr
Output File: press.txt
Data sorting.. [XXXXXXXXX 1 [main] press 9076 cygwin_exception::open_stackdumpfile: Dumping stack trace to press.exe.stackdump
Aug 11, 2016 at 1:43am UTC
1 2 3
in.seekg(in.beg,in.end);
//...
in.seekg(in.end,in.beg);
If this works correctly, it's only by coincidence. It should be
1 2 3
in.seekg(0, in.end);
//...
in.seekg(0, in.beg);
Aug 11, 2016 at 12:59pm UTC
Okay, well that's changed now, helios. What about my question though?
Aug 11, 2016 at 1:21pm UTC
Sorry, I don't understand the question, or what you're trying to do.
Aug 11, 2016 at 1:28pm UTC
Okay, so the last output from the compressing execution lays out a bunch of chars.
the last thing I output is with
1 2 3 4 5
int totalCnt;
...
totalCnt++;
...
out << totalCnt;
That outputs as a real number. I want to collect each digit in that number. Next, I want to turn it into an `int`. So I use
1 2 3 4 5 6
//This code is where I am getting the digits from
while (counter != length-1) {
cd[v++] = mybuffer[counter++];
}
totalCnt = int (cd);
cout << totalCnt;
And technically, this should work, but it doesn't. Where in my loops am I screwing up?
Aug 11, 2016 at 1:55pm UTC
helios:
Should I put more code up?
Aug 11, 2016 at 2:06pm UTC
Here's my last output from the function that does the compression:
thisisthestartofportionfour 179922
So. What can I do with it?
Aug 11, 2016 at 4:45pm UTC
This is what solved it. 0 in ASCII is 48. So I take 48 away and I get the numeral. You can do endless numbers with this technique.
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
while (counter != length) {
if (counter/length >= c/33) {
c += 10;
cout << "X" << flush;
}
for (int i = 0; i < frsize; ++i)
redcheck[i] = mybuffer[counter+i];
if (string(redcheck) == string(redline) && ! STOP) {
STOP = 1;
}
else if (! STOP)
PN.push_back(static_cast <unsigned int >(mybuffer[counter++]));
else if (STOP) {
if (isdigit(mybuffer[counter])) {
cd[v] = mybuffer[counter++];
v++;
}
else
counter++;
}
}
totalCnt = 0;
for (int x = 0;x < v;++x) {
int h = cd[x] - 48;
totalCnt = totalCnt + (h * pow(10,v-x-1));
}
Topic archived. No new replies allowed.