George said:
On Tue, 09 Dec 2008 18:12:31 -0800, Barry Schwarz wrote: ...
#include <stdio.h>
int main(void)
{
char a[40];
char b[8][5];
char *p1;
char (*p2)[5];
int i;
char c;
for (i=60; i = 60 + 40 ; ++ i)
{
a[i-60] = i;
}
I strongly suspect that you mean "i==60+40". The way you wrote it, the
loop condition always has a value of 100, which is non-zero, so the loop
never terminates.
I got that. In the meantime, my desire to use the inferior tool which is
the C Programming Language for array manipulation has waned. Here's how
you do it in fortran:
integer, parameter :: outfile = 51
character(len=100) :: line
integer :: seqnum, eof,i, bits_needed
integer :: bit_count, ibuf, val
integer(1) :: out(100000)
character :: c*1
! main control
open(unit=50,file='george.txt',form='formatted')
! initialize counters
bit_count = 0
ibuf = 0
val = 0
line_loop: &
do
read (50,*,iostat=eof) seqnum, line
if (eof /= 0) exit line_loop
char_loop: &
do i = 1, len_trim (line)
c = line(i:i)
if (c /= '0' .and. c /= '1') cycle char_loop
! ignore non-data chars
! Pack each 0 or 1 into the next byte, order MSB first.
val = val * 2 ! shift accumulator left 1 bit
if (c == '1') val = val + 1 ! insert new bit on right
bit_count = bit_count + 1
! When next byte is complete, add to buffer.
if (bit_count == 8) then
ibuf = ibuf + 1 ! advance buffer pointer
out(ibuf) = val ! convert for single byte output
if (val >= 128) out(ibuf) = val - 256 ! handle negs
bit_count = 0 ! reset accumulator for next byte
val = 0
end if
end do char_loop
end do line_loop
! Handle incomplete final byte. Pad and add to buffer.
if (bit_count > 0) then
bits_needed = 8 - bit_count
val = val * (2 ** bits_needed) ! shift left, zero pad on right
ibuf = ibuf + 1 ! advance buffer pointer
out(ibuf) = val ! convert for single byte output
if (val >= 128) out(ibuf) = val - 256 ! handle negatives
end if
! Write to output file.
open (outfile, file='bin2.dat', access='direct', recl=ibuf, &
action='write', status='replace')
write (outfile, rec=1) out(1:ibuf) ! write complete buffer
close (outfile)
print *, 'Number of bytes written = ', ibuf
! clean up and exit
close(unit=50)
endprogram
! g95 dave3.f03 -o x.exe
BTW, I don't know who dave is.
p1++ increase the position that p1 points at by one 'char', since p1
points at a char. p2++ increases the position that p2 points at by one
array of 5 chars, since p2 points at an array of 5 chars.
Thanks. This arcane, useless, cryptic, irrelevant, stupid, parochial
formulation was brought up by Barry, with whom I've had a falling out.
I'll take this as my nod to leave. Thanks for your comment and Merry
Christmas.
--
George
The United States and our allies are determined: we refuse to live in the
shadow of this ultimate danger.
George W. Bush
Picture of the Day
http://apod.nasa.gov/apod/