Reading/Writing other files from inside a C program

E

explorer

I am learning C, so I decided to try out r/w-ing to other files. Can
anyone please tell me why I get the following error when running the
program?


Transcript:
Name?
Nikhil R. Mulani
Bus error


Code:

#include <stdio.h>

main()
{
char name[60];
FILE *Fp;

Fp = fopen("/deeter.dat", "w");

printf("Name?\n");
scanf("%s",name);

fprintf(Fp, "Name: %s\n", name);

int fclose( FILE *Fp );
}
 
S

Sebastian Hungerecker

I am learning C, so I decided to try out r/w-ing to other files. Can
anyone please tell me why I get the following error when running the
program?
[...]
scanf("%s",name);
[...]

You miss an "&" before "name".
 
I

infobahn

Sebastian said:
I am learning C, so I decided to try out r/w-ing to other files. Can
anyone please tell me why I get the following error when running the
program?
[...]
scanf("%s",name);
[...]

You miss an "&" before "name".

I haven't seen the original article, but if name is an array of char,
then prepending & is incorrect. %s matches char *, not char (*)[].

Also, the OP should be aware that scanf("%s", name) opens the program
up to the possibility of a buffer overrun attack.
 
J

Jens.Toerring

I am learning C, so I decided to try out r/w-ing to other files. Can
anyone please tell me why I get the following error when running the
program?
Transcript:
Name?
Nikhil R. Mulani
Bus error

#include <stdio.h>
main()
{
char name[60];
FILE *Fp;
Fp = fopen("/deeter.dat", "w");

fprintf(Fp, "Name: %s\n", name);
int fclose( FILE *Fp );
}

There are several problems with your program of different importance.

1) main() is supposed to return an int. While you will get away without
specifying a return type on older compilers (i.e. non-C99) you should
avoid that and use either

int main( void )

or

int main( int argc, char *argv[ ] )

2) You never test if the call of fopen() succeeds. But when it fails
'Fp' will be set to NULL and you can't write to the file.

3) Using scanf() to read in strings is tricky. First of all, when
you just have a "%s" conversion specifier you can't avoid that
the user enters more characters than fit into the buffer into
which the result gets written - and if (s)he does you write past
the end of that buffer, which is a very bad mistake. Thus you
should call it at least with

scanf( "%59s", name );

That way the user can't enter more than 59 characters (you need
the 60th for the final '\0' character).

Another problem is that scanf() stops at white space (i.e spaces,
tabs etc.). This will keep you from entering "Nikhil R. Mulani"
since scanf() will stop reading after the "Nikhil". While that
can be circumvented by using

scanf( "%59[^\n]", name );

it probably is simpler to use fgets() to read in the whole line.

4) Since you didn't check that 'Fp' isn't NULL you might get into
problems with the call of fprintf() - this expects a valid FILE
pointer as the first argument.

5) The line

int fclose( FILE *Fp );

should get the compiler upset (and I strongly doubt that you got
the program compiled in the for you posted it). I guess you want

fclose( Fp );

here - but don't call fclose() when 'Fp' is NULL.

6) Since main() is supposed to return an int your program is missing
a return statement at the end.

Regards, Jens
 
J

Jens.Toerring

Sebastian Hungerecker said:
I am learning C, so I decided to try out r/w-ing to other files. Can
anyone please tell me why I get the following error when running the
program?
[...]
scanf("%s",name);
[...]
You miss an "&" before "name".

No, he definitely doesn't - 'name' is an array of chars and in value
context (like here) it is automatically converted to a pointer to the
first element of the array.
Regards, Jens
 
M

Martin Ambuhl

Sebastian said:
I am learning C, so I decided to try out r/w-ing to other files. Can
anyone please tell me why I get the following error when running the
program?
[...]
scanf("%s",name);
[...]


You miss an "&" before "name".

You are wrong.
 
M

Martin Ambuhl

I am learning C, so I decided to try out r/w-ing to other files. Can
anyone please tell me why I get the following error when running the
program?


Transcript:
Name?
Nikhil R. Mulani
Bus error

It's hard to say. It's probably not because of the failure to explicitly
state the return type of main as int (required for C99), or
because of the failure to explicitly return a value from main (required
for predictable behavior before C99), or because of the failure to check
that the fopen succeeded, or because of using scanf("%s",... for input
data that *might* contain whitespace. My best guess is ...
Code:

#include <stdio.h>

main()
{
char name[60];
FILE *Fp;
Fp = fopen("/deeter.dat", "w");
printf("Name?\n");
scanf("%s",name);
fprintf(Fp, "Name: %s\n", name);
int fclose( FILE *Fp );

.... this line, which contains a bogus 'int' and 'FILE *', and so looks
like a declaration in the middle of your code. This should, on a
pre-C99 compiler, have resulted in a failure to compile.

Here is an example of how you might start to make your code look more
like a real C program. Improvements to my example code are, of course,
possible.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
char name[60];
FILE *Fp;
if (!(Fp = fopen("./deeter.dat", "w"))) {
fprintf(stderr,
"Could not open \"./deeter.dat\" for output.\n");
exit(EXIT_FAILURE);
}
printf("What is your name? ");
fflush(stdout);
if (fgets(name, sizeof name, stdin)) {
char *nl;
if ((nl = strchr(name, '\n')))
*nl = 0;
fprintf(Fp, "Name: %s\n", name);
}
else {
fprintf(stderr, "Arcane input error.\n");
fclose(Fp);
remove("./deeter.dat");
exit(EXIT_FAILURE);
}
fclose(Fp);
return 0;
}
 
J

Jack Klein

I am learning C, so I decided to try out r/w-ing to other files. Can
anyone please tell me why I get the following error when running the
program?
Transcript:
Name?
Nikhil R. Mulani
Bus error

#include <stdio.h>
main()
{
char name[60];
FILE *Fp;
Fp = fopen("/deeter.dat", "w");

fprintf(Fp, "Name: %s\n", name);
int fclose( FILE *Fp );
}

There are several problems with your program of different importance.

[snip]
5) The line

int fclose( FILE *Fp );

should get the compiler upset (and I strongly doubt that you got
the program compiled in the for you posted it). I guess you want

You went a little overboard here. The line represents a perfectly
correct prototype for the function fclose(), and a prototype is
perfectly legal at this point in the source code. So it most
certainly should compile, although it doesn't do what the OP expects.
fclose( Fp );

here - but don't call fclose() when 'Fp' is NULL.

The rest of your advice is excellent, of course.
 
K

Keith Thompson

Jack Klein said:
On 26 Feb 2005 17:59:11 GMT, (e-mail address removed)-berlin.de wrote
in comp.lang.c: [snip]
5) The line

int fclose( FILE *Fp );

should get the compiler upset (and I strongly doubt that you got
the program compiled in the for you posted it). I guess you want

You went a little overboard here. The line represents a perfectly
correct prototype for the function fclose(), and a prototype is
perfectly legal at this point in the source code. So it most
certainly should compile, although it doesn't do what the OP expects.
fclose( Fp );

here - but don't call fclose() when 'Fp' is NULL.

The rest of your advice is excellent, of course.

A prototype is legal at that point only if you have a C99 compiler (or
a C90 compiler that allows declarations to follow statements as an
extension) <OT>or a C++ compiler</OT>.
 
K

Keith Thompson

I am learning C, so I decided to try out r/w-ing to other files. Can
anyone please tell me why I get the following error when running the
program?


Transcript:
Name?
Nikhil R. Mulani
Bus error


Code:

#include <stdio.h>

main()
{
char name[60];
FILE *Fp;

Fp = fopen("/deeter.dat", "w");

printf("Name?\n");
scanf("%s",name);

fprintf(Fp, "Name: %s\n", name);

int fclose( FILE *Fp );
}

My best guess is that the fopen() call failed because you don't have
permission to create the named file. You then call fprintf() with Fp
as an argument, which invokes undefined behavior. Dropping the
leading '/' might address the problem. In any case, you need to check
whether the fopen() succeeded, and *don't* attempt to write to the
file if it failed.

Something else you should address is the fact that scanf("%s", name)
only grabs the first whitespace-delimited word of input; in this case,
just "Nikhil", not "Nikhil R. Mulani". You probably want fgets().
 
J

Jens.Toerring

You went a little overboard here. The line represents a perfectly
correct prototype for the function fclose(), and a prototype is
perfectly legal at this point in the source code. So it most
certainly should compile, although it doesn't do what the OP expects.

Yes, thank you. I didn't realize that it's allowed now in C99 and
only tested in strict ANSI-89 mode.

Regards, Jens
 
J

Jack Klein

Jack Klein said:
On 26 Feb 2005 17:59:11 GMT, (e-mail address removed)-berlin.de wrote
in comp.lang.c: [snip]
5) The line

int fclose( FILE *Fp );

should get the compiler upset (and I strongly doubt that you got
the program compiled in the for you posted it). I guess you want

You went a little overboard here. The line represents a perfectly
correct prototype for the function fclose(), and a prototype is
perfectly legal at this point in the source code. So it most
certainly should compile, although it doesn't do what the OP expects.
fclose( Fp );

here - but don't call fclose() when 'Fp' is NULL.

The rest of your advice is excellent, of course.

A prototype is legal at that point only if you have a C99 compiler (or
a C90 compiler that allows declarations to follow statements as an
extension) <OT>or a C++ compiler</OT>.

You are correct, sir, my bad. Jens forgot about C99, and I forgot
about C90!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top