B
bent lee
Hi
I need help with something thats bugging me for a while.
The only way I can get the program to print something
is if I do this in the while loop.
i = 0;
while ((c = getc(fp)) != EOF) {
bp[i++] = c
}
bp = '\0'
instead of
while ((c = getc(fp)) != EOF) {
*bp++ = c;
}
*bp = '\0';
why can't I get it to work?
here is the full program.
i'm running gentoo/linux.
#include <stdio.h>
#include <stdlib.h>
size_t bufsize = 512;
int main(int argc, char *argv[])
{
FILE *fp;
char *bp;
int i;
int c;
*argv++;
fp = fopen(*argv, "r");
if ((bp = malloc(bufsize * sizeof(char *))) == NULL) {
printf("cannot alocate memory.\n");
return 1;
}
/*i = 0;*/
while ((c = getc(fp)) != EOF) {
*bp++ = c;
/* bp[i++] = c */
}
*bp = '\0';
/* bp = '\0' */
fclose(fp);
printf("%s", bp);
free(bp);
return 0;
}
I need help with something thats bugging me for a while.
The only way I can get the program to print something
is if I do this in the while loop.
i = 0;
while ((c = getc(fp)) != EOF) {
bp[i++] = c
}
bp = '\0'
instead of
while ((c = getc(fp)) != EOF) {
*bp++ = c;
}
*bp = '\0';
why can't I get it to work?
here is the full program.
i'm running gentoo/linux.
#include <stdio.h>
#include <stdlib.h>
size_t bufsize = 512;
int main(int argc, char *argv[])
{
FILE *fp;
char *bp;
int i;
int c;
*argv++;
fp = fopen(*argv, "r");
if ((bp = malloc(bufsize * sizeof(char *))) == NULL) {
printf("cannot alocate memory.\n");
return 1;
}
/*i = 0;*/
while ((c = getc(fp)) != EOF) {
*bp++ = c;
/* bp[i++] = c */
}
*bp = '\0';
/* bp = '\0' */
fclose(fp);
printf("%s", bp);
free(bp);
return 0;
}