473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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::out | 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!

Aug 7 '06 #1
5 3951
cy***********@gmail.com wrote:
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::out | 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.
Aug 7 '06 #2
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 Magnus wrote:
cy***********@gmail.com wrote:
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::out | 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.
Aug 7 '06 #3
On 7 Aug 2006 05:49:27 -0700, cy***********@gmail.com wrote:
>Rolf Magnus wrote:
>cy***********@gmail.com wrote:
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.
[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

Aug 9 '06 #4
this works. Many thanks!!!
rossum wrote:
On 7 Aug 2006 05:49:27 -0700, cy***********@gmail.com wrote:
Rolf Magnus wrote:
cy***********@gmail.com wrote:

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.
[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
Aug 14 '06 #5
cy***********@gmail.com wrote:
this works. Many thanks!!!
Please read the information below.

Brian

--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Aug 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Albert Tu | last post by:
Hi, I am learning and pretty new to Python and I hope your guys can give me a quick start. I have an about 1G-byte binary file from a flat panel x-ray detector; I know at the beggining there...
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been able to read data from binary files to filestrean, the data in the files are structured, what I'm trying to do is to loop through the binary files and add data to my...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
4
by: Erpman | last post by:
I am trying to access the data with in a wav file. I am testing with very small files in order to keep the code simple to start with. Basically, im writing the entire wav file to a byte using a...
10
by: T Cordon | last post by:
I am using a StreamReader to read text from an HTML file and display it as part of a page in a Label Control. Buy it is not displaying characters as: ñ, ó, ú, etc. Please Help. Thanks
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
9
by: Bill Woessner | last post by:
Suppose I have a structure, foo, which is a POD. I would like to read and write it to disk as follows: std::ofstream outs; foo bar; outs.write(reinterpret_cast<char*>(&bar), sizeof(foo));...
9
by: dgleeson3 | last post by:
Hello All I have a txt file of strings of different lengths. I dont know how many strings are in the file. I have no problem reading the file and sending to the console (as below). To...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.