reading from multiple files 1kb at a time to a byte array

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::eek: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!
 
R

Rolf Magnus

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.

You can just open more files, like you did with the first one.
Thanks in advance.

typedef struct

You can leave out the typedef. It would only be needed in C.
{
int part;
char DataChunk[1024];
} DataPacket;

void ProcessFile(char* abc[1024], ifstream& ifs, ofstream& ofs);

The first parameter is a pointer to pointer to char. The 1024 is ignored.
Did you want that?
ifstream and ofstream are not defined. You need to add proper #includes and
some using declarations.
DataPacket dp1;

int _tmain(int argc, _TCHAR* argv[])

The above is non-standard.
{
ifstream source;
ofstream target;

source.open("picture.jpg", ios::in | ios::binary);

target.open("output.jpg", ios::eek: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;
}

You shouldn't use int for the position in a file. What is that function
supposed to be good for anyway?
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;
}

Hmm. You don't access abc here at all. Actually, it seems the whole function
is never called either.
 
C

cybersangeeth

Thanks for the reply. I forgot to get rid of the function
(ProcessFile()). was intending to use that external function. Would you
be able to give some pseudo code to read 1KB from multiple files. I
actually have a dynamic list of files in a folder which has to be read
1K at a time and written to a socket.
Thanks!
Rolf said:
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.

You can just open more files, like you did with the first one.
Thanks in advance.

typedef struct

You can leave out the typedef. It would only be needed in C.
{
int part;
char DataChunk[1024];
} DataPacket;

void ProcessFile(char* abc[1024], ifstream& ifs, ofstream& ofs);

The first parameter is a pointer to pointer to char. The 1024 is ignored.
Did you want that?
ifstream and ofstream are not defined. You need to add proper #includes and
some using declarations.
DataPacket dp1;

int _tmain(int argc, _TCHAR* argv[])

The above is non-standard.
{
ifstream source;
ofstream target;

source.open("picture.jpg", ios::in | ios::binary);

target.open("output.jpg", ios::eek: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;
}

You shouldn't use int for the position in a file. What is that function
supposed to be good for anyway?
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;
}

Hmm. You don't access abc here at all. Actually, it seems the whole function
is never called either.
 
R

rossum

[snip code]

Top posting fixed.
Thanks for the reply. I forgot to get rid of the function
(ProcessFile()). was intending to use that external function. Would you
be able to give some pseudo code to read 1KB from multiple files. I
actually have a dynamic list of files in a folder which has to be read
1K at a time and written to a socket.
Thanks!

[Pseudocode]
foreach (entry in dynamic_list)
open corresponding file
add opened file to list_of_open_files
end foreach
open output_socket
repeat
foreach (file in list_of_open_files)
read up to 1KB from file
write up to 1KB to output_socket
if (file.EoF)
close file
remove file from list_of_open_files
end if
end foreach
until list_of_open_files is empty
close output_socket

HTH

rossum
 
C

cybersangeeth

this works. Many thanks!!!
rossum said:
[snip code]

Top posting fixed.
Thanks for the reply. I forgot to get rid of the function
(ProcessFile()). was intending to use that external function. Would you
be able to give some pseudo code to read 1KB from multiple files. I
actually have a dynamic list of files in a folder which has to be read
1K at a time and written to a socket.
Thanks!

[Pseudocode]
foreach (entry in dynamic_list)
open corresponding file
add opened file to list_of_open_files
end foreach
open output_socket
repeat
foreach (file in list_of_open_files)
read up to 1KB from file
write up to 1KB to output_socket
if (file.EoF)
close file
remove file from list_of_open_files
end if
end foreach
until list_of_open_files is empty
close output_socket

HTH

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top