C
cybersangeeth
Hi,
I need to read 1KB each time from multiple files in a folder and pass
it to a byte array in a struct to be sent through a socket. I'm a C++
newbie. I managed to read 1KB each time from one file and store it on
to another file using the following code. Could anyone please let me
know how i could go about reading from multiple files. Thanks in
advance.
typedef struct
{
int part;
char DataChunk[1024];
} DataPacket;
void ProcessFile(char* abc[1024], ifstream& ifs, ofstream& ofs);
DataPacket dp1;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream source;
ofstream target;
source.open("picture.jpg", ios::in | ios::binary);
target.open("output.jpg", ios:ut | ios::binary);
int No_of_chunks = CalculateParts(source);
dp1.part = 1;
while (dp1.part != (No_of_chunks))
{
source.read(dp1.DataChunk, 1024);
target.write(dp1.DataChunk, 1024);
cout<< "Part Number: " << dp1.part << "\n";
cout<< "New position: " << seekfn(source) << "\n";
dp1.part = dp1.part + 1;
}
target.close();
return 0;
}
/* Function that calculates the number of parts in a data entity (file
stream) */
int CalculateParts(ifstream& filestream)
{
start = filestream.tellg();
filestream.seekg(0, std::ios::end);
end = filestream.tellg();
filestream.seekg(0, std::ios::beg);
size = end - start;
int a = size/1024;
int b = size % 1024;
if (b != 0)
{
a = a+1;
}
return a;
}
/* Function that gives the current position to be read in a file stream
*/
int seekfn(ifstream& inputstream)
{
int position = inputstream.tellg();
return position;
}
void ProcessFile(char* abc[1024], ifstream& ifs, ofstream& ofs)
{
//dp1.part = 1;
int No_of_chunks = CalculateParts(ifs);
while (dp1.part != (No_of_chunks))
{
ifs.read(dp1.DataChunk, 1024);
ofs.write(dp1.DataChunk, 1024);
cout<< "Part Number: " << dp1.part << "\n";
cout<< "New position: " << seekfn(ifs) << "\n";
dp1.part = dp1.part + 1;
}
Sam!
I need to read 1KB each time from multiple files in a folder and pass
it to a byte array in a struct to be sent through a socket. I'm a C++
newbie. I managed to read 1KB each time from one file and store it on
to another file using the following code. Could anyone please let me
know how i could go about reading from multiple files. Thanks in
advance.
typedef struct
{
int part;
char DataChunk[1024];
} DataPacket;
void ProcessFile(char* abc[1024], ifstream& ifs, ofstream& ofs);
DataPacket dp1;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream source;
ofstream target;
source.open("picture.jpg", ios::in | ios::binary);
target.open("output.jpg", ios:ut | ios::binary);
int No_of_chunks = CalculateParts(source);
dp1.part = 1;
while (dp1.part != (No_of_chunks))
{
source.read(dp1.DataChunk, 1024);
target.write(dp1.DataChunk, 1024);
cout<< "Part Number: " << dp1.part << "\n";
cout<< "New position: " << seekfn(source) << "\n";
dp1.part = dp1.part + 1;
}
target.close();
return 0;
}
/* Function that calculates the number of parts in a data entity (file
stream) */
int CalculateParts(ifstream& filestream)
{
start = filestream.tellg();
filestream.seekg(0, std::ios::end);
end = filestream.tellg();
filestream.seekg(0, std::ios::beg);
size = end - start;
int a = size/1024;
int b = size % 1024;
if (b != 0)
{
a = a+1;
}
return a;
}
/* Function that gives the current position to be read in a file stream
*/
int seekfn(ifstream& inputstream)
{
int position = inputstream.tellg();
return position;
}
void ProcessFile(char* abc[1024], ifstream& ifs, ofstream& ofs)
{
//dp1.part = 1;
int No_of_chunks = CalculateParts(ifs);
while (dp1.part != (No_of_chunks))
{
ifs.read(dp1.DataChunk, 1024);
ofs.write(dp1.DataChunk, 1024);
cout<< "Part Number: " << dp1.part << "\n";
cout<< "New position: " << seekfn(ifs) << "\n";
dp1.part = dp1.part + 1;
}
Sam!