E
Emerson
Hi all:
my problem is in the comments of the code .
would u please give me some clue what 's the problem?
what u in advance.
/*******************************/
#include "stdio.h"
#include "malloc.h"
#include "assert.h"
int main(void)
{
FILE *p_file;
static char *filename="test";
char *s=NULL;
int c;
int num=0;
if ((p_file=fopen(filename,"w+"))==NULL) { /* does "w+" means your
can read & write the file? */
printf("cant't open file %s\n",filename);
}
else{
while ((c=getchar())!=EOF) {
if (fputc(c,p_file)!=EOF)
++num; /*# of character written*/
}
fflush(p_file);
}
if ((s=(char*)calloc(num,sizeof(*s)))==NULL) {
printf("can't calloc enough memory\n");
}
else{
assert(p_file!=NULL); /*the file stores actually what i
input,ant the p_file is NOT NULL */
if((s=fgets(s,5,p_file))==NULL) /*i just get 5-1 character,and make
sure the p_file has more than 5 character*/
printf("error in fgets\n"); /*problem is HERE,this message is
printed,WHY?*/
else
printf("fgets get followings: %s \n", s);
}
fclose(p_file);
return 0;
}
my problem is in the comments of the code .
would u please give me some clue what 's the problem?
what u in advance.
/*******************************/
#include "stdio.h"
#include "malloc.h"
#include "assert.h"
int main(void)
{
FILE *p_file;
static char *filename="test";
char *s=NULL;
int c;
int num=0;
if ((p_file=fopen(filename,"w+"))==NULL) { /* does "w+" means your
can read & write the file? */
printf("cant't open file %s\n",filename);
}
else{
while ((c=getchar())!=EOF) {
if (fputc(c,p_file)!=EOF)
++num; /*# of character written*/
}
fflush(p_file);
}
if ((s=(char*)calloc(num,sizeof(*s)))==NULL) {
printf("can't calloc enough memory\n");
}
else{
assert(p_file!=NULL); /*the file stores actually what i
input,ant the p_file is NOT NULL */
if((s=fgets(s,5,p_file))==NULL) /*i just get 5-1 character,and make
sure the p_file has more than 5 character*/
printf("error in fgets\n"); /*problem is HERE,this message is
printed,WHY?*/
else
printf("fgets get followings: %s \n", s);
}
fclose(p_file);
return 0;
}