Multiple looping

J

john

Hello,

I am trying to send 2^19 data to USB2.0. I am reading the file
"all_sines" and loading it into
an array called "string1". Then, in a for loop I copied it into the USB
data array "Data_outBuffer". The USB can
carry only "32767" bytes at one time. The way I am doing right now
requires 16 for loops to send the
2^19 amount of data. ( 32767 * 16 = 2^19). How can i solve this problem
by using two loops. Is it possible? Please advice!

I am looking for optimize code solution. The code is attached below
Thanks
Regards,
John



#define COUNT_VAR 32767

int main(int argc, char* argv[])

{

FILE *infile;
unsigned char string1[COUNT_VAR+1];
unsigned char DataOutBuffer[COUNT_VAR -1];
int ReturnVal;
int x;


if((infile=fopen("C:\\C\\all_sines.bin","rb")) != NULL )

{
fread ( string1,sizeof( char), COUNT_VAR, infile );
}

else

printf( "Problem opening the file\n" );

for( x=0; x <= COUNT_VAR-1 ; x++ )

{

DataOutBuffer[x]=string1[x];
printf("value =%d %d\n",x,DataOutBuffer[x] );

}

SetSignals (PodNumber, 0xFF /* Don't Care */, COUNT_VAR /*Max.
Length=32767*/, DataOutBuffer); // USB Command

}/*main*/
 
M

Michael Mair

john said:
Hello,

I am trying to send 2^19 data to USB2.0. I am reading the file
"all_sines" and loading it into
an array called "string1". Then, in a for loop I copied it into the USB
data array "Data_outBuffer". The USB can
carry only "32767" bytes at one time. The way I am doing right now
requires 16 for loops to send the
2^19 amount of data. ( 32767 * 16 = 2^19). How can i solve this problem
by using two loops. Is it possible? Please advice!

?
Are you looking for a solution with two nested loops?
Why not use
for (i=0; i<16; i++)
{
retval=fread(....);
....
memcpy(....);
}
Or are you looking for two consecutive loops?
I am looking for optimize code solution. The code is attached below
Thanks
Regards,
John

The code unfortunately does not exactly help as it does not
compile.
#define COUNT_VAR 32767

int main(int argc, char* argv[])

{

FILE *infile;
unsigned char string1[COUNT_VAR+1];
unsigned char DataOutBuffer[COUNT_VAR -1];

Why "-1" instead of "+1". If the latter was meant and is
in your actual code: Please spare us! Do not give pseudocode
but the real thing.
If the former was meant: You are doing something wrong.
Judging from the way you use these buffers, COUNT_VAR elements
suffice.
int ReturnVal;
int x;

Note that int is guaranteed to be able to represent (2^15-1)
but no more. This coincides with your upper limit and can
lead to annoying errors on implementations where int actually is
16 bits wide. Rather use size_t or signed long.
if((infile=fopen("C:\\C\\all_sines.bin","rb")) != NULL )

{
fread ( string1,sizeof( char), COUNT_VAR, infile );
}

else

printf( "Problem opening the file\n" );

What's this?
If there is an error you are just going on?
You cannot open a file or output anything as you have forgotten
to #include <stdio.h>
You do not look for the return value of fread()
Think along the lines
size_t retval;
....
if (!(infile = fopen("C:\\C\\all_sines.bin","rb")))
{
fprintf(stderr, "Problem opening the file\n" );
exit(EXIT_FAILURE); /* exit() and EXIT_FAILURE are from stdlib.h */
}

retval = fread(string1, 1, COUNT_VAR, infile);
If you really want to code in the size, do not use sizeof (char) but
sizeof *string1. retval holds the number of actually read bytes.
You do not need to copy more.
for( x=0; x <= COUNT_VAR-1 ; x++ )

I would rather use "< upper limit"; this has the advantage
that you can see the number of iterations directly (upper limit
minus lower bound).
Note that retval is _much_ more appropriate as upper limit
(see below).
{

DataOutBuffer[x]=string1[x];
printf("value =%d %d\n",x,DataOutBuffer[x] );

}

Now, why do you do that?
You do not know whether fread() succeeded so you might output the
uninitialized array string1.

Apart from that, why do you not just use memcpy() from <string.h>?

Moreover, why do you not directly read into DataOutBuffer?
SetSignals (PodNumber, 0xFF /* Don't Care */, COUNT_VAR /*Max.
Length=32767*/, DataOutBuffer); // USB Command

You did not indicate that you are using C99, so the // comments
come as a surprise. Apart from that, they make no sense on
usenet: If the comment is broken by a line wrapping, some things
become uncommented inadvertently.

you forgot to fclose() the file.
main() returns int, so let's do it:

return 0;
}/*main*/

I would rather use

while (retval = fread(myBuffer,.... ))
{
/* do operation depending on retval and myBuffer */
}


Cheers
Michael
 
B

Barry Schwarz

Hello,

I am trying to send 2^19 data to USB2.0. I am reading the file
"all_sines" and loading it into
an array called "string1". Then, in a for loop I copied it into the USB
data array "Data_outBuffer". The USB can
carry only "32767" bytes at one time. The way I am doing right now
requires 16 for loops to send the
2^19 amount of data. ( 32767 * 16 = 2^19). How can i solve this problem
by using two loops. Is it possible? Please advice!

I am looking for optimize code solution. The code is attached below
Thanks
Regards,
John



#define COUNT_VAR 32767

int main(int argc, char* argv[])

{

FILE *infile;
unsigned char string1[COUNT_VAR+1];
unsigned char DataOutBuffer[COUNT_VAR -1];
int ReturnVal;
int x;


if((infile=fopen("C:\\C\\all_sines.bin","rb")) != NULL )

{
fread ( string1,sizeof( char), COUNT_VAR, infile );
}

else

printf( "Problem opening the file\n" );

for( x=0; x <= COUNT_VAR-1 ; x++ )

{

DataOutBuffer[x]=string1[x];

At the point when x = COUNT_VAR-1, this statement will invoke
undefined behavior since that is an invalid index for DataOutBuffer.
printf("value =%d %d\n",x,DataOutBuffer[x] );

Here too.
}

SetSignals (PodNumber, 0xFF /* Don't Care */, COUNT_VAR /*Max.
Length=32767*/, DataOutBuffer); // USB Command

}/*main*/



<<Remove the del for email>>
 
J

john

Hello,

Thanks for the reply! I want my loops to run ( 16 * 32767 ) times. The
problem is that how can I keep the track of my "string1" array. for
example I tried to the following thing

y=0;
x=0;
while ( y<17)
{
for (; x< COUNT_VAR-1;x++)
{
DataOutBuffer[x]=string1[x];
}
y=y+1;
x=x+1;
Setsignals (PodNumber,0xFF;COUNT_VAR,DataOutBuffer);
}

Now, if 'x' gets to 32767 which is also the "COUNT_VAR" then the "for
loop" will never execute again , it will just run once. My USB function
can only carry "32767" bytes and I need to send 2^19 bytes out.
Please adivce!

Thanks
Regards,
John
 
K

Kenneth Brody

john said:
Hello,

Thanks for the reply! I want my loops to run ( 16 * 32767 ) times. The
problem is that how can I keep the track of my "string1" array. for
example I tried to the following thing

y=0;
x=0;
while ( y<17)

Note that this will loop 17 times, not 16.
{
for (; x< COUNT_VAR-1;x++)

Change these to:

for ( y=0 ; y < 16 ; y++ )
{
for ( x=0 ; x < COUNT_VAR ; x++ )

This is clearer (to me, at least) as to what you're doing, compared
to the outer "while" loop. Plus, it makes sure that x gets reset to
zero each time the outer loop executes.
{
DataOutBuffer[x]=string1[x];
}
y=y+1;
x=x+1;

Eliminate those two increments. They're take care of by the for()
statements.

Setsignals (PodNumber,0xFF;COUNT_VAR,DataOutBuffer);
}

Now, if 'x' gets to 32767 which is also the "COUNT_VAR" then the "for
loop" will never execute again , it will just run once. My USB function
can only carry "32767" bytes and I need to send 2^19 bytes out.
Please adivce!

Also, is it your intention to send those same 32767 bytes 16 times,
or did you mean to include the fread() in the outer loop?

Finally, why are you doing a char-by-char copy of string1 into
DataOutBuffer, when you could simply send string1 to Setsignals?
(Unless the real code is doing some data manipulation which you
aren't showing here, there is no point to copying the data from
one bufer to another.) And, even if you have a need to copy the
data from one buffer to another unchanges, use memmove instead.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
N

Nick Keighley

I am trying to send 2^19 data to USB2.0. I am reading the file
"all_sines" and loading it into
an array called "string1". Then, in a for loop I copied it into the USB
data array "Data_outBuffer". The USB can
carry only "32767" bytes at one time. The way I am doing right now
requires 16 for loops to send the
2^19 amount of data. ( 32767 * 16 = 2^19). How can i solve this problem
by using two loops. Is it possible? Please advice!

I am looking for optimize code solution. The code is attached below

<code snipped>

Hi,

There seems to be some confusion. At several levels. You need to
clearly
specify what you want then implement it. Along the way accept that C
uses zero based arrays and it is easier to go with the flow. I don't
understand why you copy your data (is there some magic about
DataOutBuffer?). You seem confused about C for-loops.

ok, basic requirement:

UNTIL data_exhausted
read_data_from_file
write_data_to_usb

with the constaint that the USB write has a limit on how much data is
written. I wouldn't calculate how many times USB buffer size divides
into file size I'd let the program do that (one day somebody might
change one of those values).

UNTIL data_exhausted
read_buffer_size_of_data_from_file
write_data_to_usb

Making it a bit more real

unsigned char buffer [BUFFER_SIZE];
unsigned long octet_count;

/* using '/' is usually valid and avoids those escapes */
if ((infile = fopen ("C:/C/all_s­ines.bin", "rb")) == NULL)
terminate();

while (!data_exhausted ())
{
octet_count = fread (buffer, 1, BUFFER_SIZE, infile);

/* USB write */
SetSignals (PodNumber, 0xFF, octet_count, buffer);
}

Assume terminate() terminates the program.
Now I don't know what constitutes data exhaustion. Is it when the end
of file is reached or when 2^19 values have been read? I also had a bit

of difficulty understanding exactly when fread() signals EOF... Assume
we want to read the whole file and adding an error check.

/* stuff elided */

while (!feof (infile) && (octet_count = fread (buffer, 1,
BUFFER_SIZE, infile)) != 0)
{
if (ferror(infile))
terminate();

/* USB write */
SetSignals (PodNumber, 0xFF, octet_count, buffer);
}

None of this code has been tested, or even compiled.


--
Nick Keighley

Every sentence I utter must be understood not as an affirmation,
but as a question.
Niels Bohr
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top