string & in order list & files

Hello all,
please check my code below
the problem is, i don't know how to get seqNum and segData from the array of characters.
I'm sure that the list works fine.

Main Code
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
72
73
74
75
76
77
78
79
#include <cstdlib>
#include <iostream>
#include "iolist.h"
#include <fstream>
#include <math.h>

using namespace std;



int segSplit(char * buf,char *p2)
{
int p1;
sscanf(buf, "%d@%s", &p1, p2);
return p1;
}



int main(int argc, char *argv[])
{
ifstream infile ("test.jpg",ios::in|ios::binary|ios::ate); 
//this opens the file 
int fileSize = (long int) infile.tellg(); //get the file size
infile.seekg (0, ios::beg); 
//point to first byte in input file to be ready for reading

long int i=(long int)ceil((double)fileSize/segSize);
//i is the # of segments in the file
int lastSegSize=fileSize-segSize*(i-1); 
//(i-1) segments of size segSize and last seg of size lastSegSize

iolist txBuffer; //transmitter buffer
seg segment; //new type defined in iolist.h
bool flag(false);

// store (i-1) segments of size segSize
for(int j(1);j<i;j++)
{
segment.seqNum=j-1;
infile.read(segment.data,segSize);
cout<<"segData=\n"<<segment.data<<'\n';
flag=txBuffer.insert(segment);
}
//then store last segment of size lastSegSize
segment.seqNum=i-1;
infile.read(segment.data,lastSegSize);
cout<<"segData=\n"<<segment.data<<'\n';
flag=txBuffer.insert(segment);

//at this point we save all file data segments with their numbers in a list called txBuffer 

ofstream outfile ("received.jpg", ios::out | ios::trunc | ios::binary);

flag=txBuffer.first(segment); //read first element
int k(1);
while(flag)
{
char strSegment[segSize+6];
itoa(segment.seqNum,strSegment,10);//convert its seqNum to string and store it in strSegment
strcat(strSegment,"@");
//concatinate seqNum with "@" this will help Rx to extract seqNum and actual data
strcat (strSegment,segment.data);//strSegment will contain {seqNum@data}
//suppose that all you know is strSegment and we want to reconstruct the file again from txBuffer
char mem[segSize]; //this will hold data 
int num=segSplit(strSegment,mem); //split segment into seqNum and data again
cout<<"mem=\n"<<mem<<'\n';
if(k==i) //if this is the last segment
outfile.write(mem,lastSegSize);
else 
outfile.write(mem,segSize);

flag=txBuffer.next(segment); //read first element
k++;
}

    return EXIT_SUCCESS;
}

Last edited on
I think the problem is in segSplit function?
?
Last edited on
In the (binary) data you read from the file, one would expect to happen upon the value of 0 for individual bytes on occasion. This 0 will be interpreted as the end of a string when you attempt to use functions meant to manipulate c-strings. That probably isn't what you want to happen.
@Cire
Yes finally i figured out what you said, and i've used memmove instead of strcat
memmove enable you to copy bytes as is from input file
strcat will stop at the first null which what i don't want here.
problem solved . :)
Topic archived. No new replies allowed.