Re: newbie question

From: Default User (first.last_at_boeing.com.invalid)
Date: 08/14/04


Date: Sat, 14 Aug 2004 16:58:12 GMT

name wrote:
>
> Studied C a while back, and thought I could write this code, but it turns
> out I've either forgotten or never understood something important here.
>
> This is supposed to copy one file to another, and word wrap the lines. I
> know there's probably a lot of error checking, etc., that should go in the
> function 'wordwrap', but I'd like to get it working first.
>
> The code:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #define MAXLENGTH 75
>
> void wordwrap(FILE *ifp, FILE *ofp)
> {
> int c;
>
> char buf1[1000];
> char buf2[1000];
> /*
> int *pbuf1;
> int *pbuf2;
>
> pbuf1 = buf1[0];
> pbuf2 = &buf2;
> */

Neither of the two commented out assignments would be correct. In first,
you are assigning one uninitialized character from buf1 to an int
pointer. That's not what you want.

The second assigns a pointer to an array 1000 of char to an int pointer.
That's a pointer type mismatch. Not what you want.

If pbuf1 and pbuf2 were char * types, then you could assign to them like
so:

char *pbuf1;
char *pbuf2;

pbuf1 = buf1;
pbuf2 = buf2;

> Note that I've got pointers to the arrays included but commented out,
> because I can't seem to understand a) why they are needed, and b) how to do
> that assignment.

See above for the how. The why is usually to use increment operations,
which you can't do with an array.

buf1++; /* NO!! */
pbuf1++ /* Sure! */

> What I thought I understood was that what was passed to the function
> wordwrap were dereferenced pointers, so that what was going in and out were
> the characters in the files, and not the addresses thereof.

Errr, what? What you pass are the file pointers, which are pointers to
an implementation-defined struct of type FILE. When used with the stdio
file functions, you can read or write data from/to the associated files
(assuming these pointers represent correctly opened files). There's no
dereferencing going in.

> And so I
> thought that I should be able to read them directly, assign them to an
> array, manipulate them whilst passing them to another array, and read the
> second array out to the second file.

Sounds ok.
 
> I used the getc and putc routines used in the K&R2 example in chapter 1. I
> did so becaue the filecopy routine drops right in place of wordwrap and the
> second file is identical to the first. Which is why I figured I could at
> least use them to read into the first array and out of the seond.
>
> That function is:
>
> void filecopy(FILE *ifp, FILE *ofp)
> {
> int c;
>
> while ((c=getc(ifp)) != EOF)
> putc(c, ofp);
> }

Looks ok, a basic filecopy. Probably not the most efficient, but should
work.

> So I thought I could build on that for 'wordwrap'. Wrong (?)

Hard to say.
 
> The code compiled (without the array pointers that are commented out). But
> note the printf right after the assignment of input to the first array.
> What comes out of that are graphical characters I don't understand. Without
> that printf statement, the code compiles, but does nothing. The second file
> is empty.

You mean this part here, I think:

        for(i = 0; i < 1000; ++i)
        {
                while ((c=getc(ifp)) != EOF)
                        buf1[i] = c;
                printf("%c", buf1[i]);
        }

You have loop problems. Look at your while loop. When does i get
incremented? By the outer loop. Where is the data read? Inner loop. What
happens is that the whole file get stuffed into buf1[0] (first outer
loop pass). After that, each successive outer loop pass increments i,
but no data is read in because ifp is already at EOF.

Get rid of the inner loop, combining the two:

        for(i = 0; i < 1000 && (c=getc(ifp)) != EOF; ++i)
        {
                buf1[i] = c;
                printf("%c", buf1[i]);
        }

Brian Rodenborn



Relevant Pages

  • Re: loop performance question
    ... | such that every possible pair in the array is tested. ... | whenever I access any data in the second loop. ... The pointers to separately allocated objects break the ... Then comes the question of how to avoid cache misses when accessing obj2. ...
    (comp.lang.cpp)
  • newbie question
    ... function 'wordwrap', but I'd like to get it working first. ... Note that I've got pointers to the arrays included but commented out, ... array, manipulate them whilst passing them to another array, and read the ... second file is identical to the first. ...
    (comp.lang.c)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • Re: Why does this work? (overloads)
    ... one type to an array originally declared with another type. ... pointers to a common unconstrained array type, ... x: A renames c(0 .. ... for k in v'range loop ...
    (comp.lang.ada)
  • Re: best method to find the freequent numbers
    ... My array size is huge. ... This could save memory, but wouldn't ... less than *max - *min, you can stop counting and break out of the loop, ... A little simpler might be (no need to keep pointers to maximum and ...
    (comp.lang.c)