Re: newbie question
From: Default User (first.last_at_boeing.com.invalid)
Date: 08/14/04
- Next message: Chris Torek: "Re: <OT> About pointer"
- Previous message: Flash Gordon: "Re: Request for comments - kgets()"
- In reply to: name: "newbie question"
- Next in thread: name: "Re: newbie question"
- Reply: name: "Re: newbie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Chris Torek: "Re: <OT> About pointer"
- Previous message: Flash Gordon: "Re: Request for comments - kgets()"
- In reply to: name: "newbie question"
- Next in thread: name: "Re: newbie question"
- Reply: name: "Re: newbie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|