N
name
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;
*/
int i, j, k;
j = k = 0;
for(i = 0; i < 1000; ++i)
{
while ((c=getc(ifp)) != EOF)
buf1 = c;
printf("%c", buf1);
}
while ((buf2[j] = buf1))
{
if(i == MAXLENGTH -1)
{
if(buf1 != ' ')
{
for(k = i; k > 0 && buf1[k] != ' '; --k);
if(k == 0)
buf2[j] = '\n';
else
buf2[k] = '\n';
}
else
buf2[k] = '\n';
}
++i;
++j;
}
for ( i = 0; buf2 != EOF; ++i)
{
printf("%c", buf2);
}
}
int main(int argc, char *argv[])
{
FILE *fp1;
FILE *fp2;
char *prog = argv[0];
if (argc != 3)
{
printf("Usage: %s: file1 file2\n", prog);
return EXIT_FAILURE;
}
else if ((fp1 = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "%s: can't open %s\n", prog, argv[1]);
return EXIT_FAILURE;
}
else if ((fp2 = fopen(argv[2], "w")) == NULL)
{
fprintf(stderr, "%s: can't open %s\n", prog, argv[2]);
return EXIT_FAILURE;
}
else
{
wordwrap(fp1, fp2);
fclose(fp1);
fclose(fp2);
}
if (ferror(fp2))
{
fprintf(stderr, "%s: error writing %s\n", prog, argv[3]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
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.
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. 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.
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);
}
So I thought I could build on that for 'wordwrap'. Wrong (?)
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.
So I thought maybe I was wrong and the data coming in were in fact the disk
addresses of the read file (fp1). I can't understand how that works because
those addresses aren't available to memory and so cannot be inspected or
manipulated. Nevertheless, I tried initializing pointers, and botched the
job (obviously). Gcc had this to say:
[wtallman@ansible ~]$ gcc -g -Wall -o f2f f2fwdwrp.c
f2fwdwrp.c: In function wordwrap':
f2fwdwrp.c:16: warning: assignment makes pointer from integer without a cast
f2fwdwrp.c:17: warning: assignment from incompatible pointer type
I have no idea whether or not I'm on the right track here, but what I get
from the printf statement is:
[wtallman@ansible ~]$ f2f testfile testfile11
ã .....and a long string of like characters not reproducable here (at
least not in Jed..). Which is why I wondered if I was looking at disk
addresses, and so needed to put up some pointers to assign to.
Obviously, I am lost here. And after reading K&R2, D&D, H&S, all in the
sections that appeared to be relevant, I'm still lost.
Could someone point me in the direction I need to go to figure this out.
Thanks for reading
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;
*/
int i, j, k;
j = k = 0;
for(i = 0; i < 1000; ++i)
{
while ((c=getc(ifp)) != EOF)
buf1 = c;
printf("%c", buf1);
}
while ((buf2[j] = buf1))
{
if(i == MAXLENGTH -1)
{
if(buf1 != ' ')
{
for(k = i; k > 0 && buf1[k] != ' '; --k);
if(k == 0)
buf2[j] = '\n';
else
buf2[k] = '\n';
}
else
buf2[k] = '\n';
}
++i;
++j;
}
for ( i = 0; buf2 != EOF; ++i)
{
printf("%c", buf2);
}
}
int main(int argc, char *argv[])
{
FILE *fp1;
FILE *fp2;
char *prog = argv[0];
if (argc != 3)
{
printf("Usage: %s: file1 file2\n", prog);
return EXIT_FAILURE;
}
else if ((fp1 = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "%s: can't open %s\n", prog, argv[1]);
return EXIT_FAILURE;
}
else if ((fp2 = fopen(argv[2], "w")) == NULL)
{
fprintf(stderr, "%s: can't open %s\n", prog, argv[2]);
return EXIT_FAILURE;
}
else
{
wordwrap(fp1, fp2);
fclose(fp1);
fclose(fp2);
}
if (ferror(fp2))
{
fprintf(stderr, "%s: error writing %s\n", prog, argv[3]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
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.
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. 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.
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);
}
So I thought I could build on that for 'wordwrap'. Wrong (?)
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.
So I thought maybe I was wrong and the data coming in were in fact the disk
addresses of the read file (fp1). I can't understand how that works because
those addresses aren't available to memory and so cannot be inspected or
manipulated. Nevertheless, I tried initializing pointers, and botched the
job (obviously). Gcc had this to say:
[wtallman@ansible ~]$ gcc -g -Wall -o f2f f2fwdwrp.c
f2fwdwrp.c: In function wordwrap':
f2fwdwrp.c:16: warning: assignment makes pointer from integer without a cast
f2fwdwrp.c:17: warning: assignment from incompatible pointer type
I have no idea whether or not I'm on the right track here, but what I get
from the printf statement is:
[wtallman@ansible ~]$ f2f testfile testfile11
ã .....and a long string of like characters not reproducable here (at
least not in Jed..). Which is why I wondered if I was looking at disk
addresses, and so needed to put up some pointers to assign to.
Obviously, I am lost here. And after reading K&R2, D&D, H&S, all in the
sections that appeared to be relevant, I'm still lost.
Could someone point me in the direction I need to go to figure this out.
Thanks for reading