Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (cwtan):

#include void main() { FILE * show; show=fopen("callersinfo.dat","r"); int n; int age; char name; char telno; fscanf(show,"%s",name); fscanf(show,"%s",telno); fscanf(show,"%d",age); printf("Name: %s\n",name); printf("Tel.No.:%s\n",telno); printf("Age: %d\n",age); } Compile Success Run fail lol.... Help me pls.... TT

OpenStudy (lgbasallote):

uhmm put a getch(); ? i heard that helps the run "stay longer"

OpenStudy (lgbasallote):

#include <stdio.h> void main() { FILE * show; show=fopen("callersinfo.dat","r"); int n; int age; char name; char telno; fscanf(show,"%s",name); fscanf(show,"%s",telno); fscanf(show,"%d",age); printf("Name: %s\n",name); printf("Tel.No.:%s\n",telno); printf("Age: %d\n",age); getch(); }

OpenStudy (anonymous):

The problem is caused by not handling strings properly. When you declare name as a char, the name will be only a single character long. Instead, you should define name as a sufficiently large (it's up to you to decide what sufficiently large is) array. Also, when using a function like scanf, you'll need to pass the parameter with a '&', so e.g. `scanf( "%s", &name );' The reason for this is that you don't just want to pass the value of name to the function, but you want the function to be able to change the value. (You should learn more about this later on. It's due to pointers and memory.)

OpenStudy (rsmith6559):

I thought that scanf() handles lines, not (arguably) tokens. So assuming that the character arrays were declared properly, wouldn't it be: fscanf( show, "%s %s %d\n", &name, &telno, &age ); ?

OpenStudy (cwtan):

nvm i fixed this prob the last thing for my assignment how to allow the spacing in scanf? i tried to use gets but it isn't suit my coding, bcoz the question ask me to put [20] on the name.... (i mean this :char name[20]) if i use gets i got the error:cannot convert parameter 1 from 'char (*)[20]' to 'char *' sooo anyone has a great idea to solve it?

OpenStudy (anonymous):

Usually, I get that message as a warning. I think you can safely ignore this message. To get rid of the error/warning message, there are two things you could do: 1) you could type-cast the array to a char pointer (using "(char*) &name" instead of "&name") 2) you could pass the pointer to the first element (using "&name[0]" instead of "&name")

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!