- Home >
- Programming >
- Files and strings
Files and strings
Question Description
Please is another task.
Write a program that asks for the user's first and last name and saves these into a file named by the user.
The program must start by asking for the first name. The last name
is entered next, followed by the desired file name. The first part of
the file name may have a maximum of 8 characters and the second part may
have 3 (for example: personal.usr). The file must reside in the same
directory as the program.
The last name can have a maximum of 20 characters, the first name 15.
Hint:
In the chapter dealing with file processing, files were opened using
a string array. Read the chapter and you should be able to perceive how
to implement the program.
My code
#include <stdio.h>
int main(){
char firstname[10], lastname[5];
char fname[]="filename.txt"; /*There is a problem here*/
char *opening_mode= "w";
printf("The program saves your first and last name into a file.");
printf("\nEnter your first name:");
scanf("%s", &firstname[0]);
printf("Enter your last name:");
scanf("%s",&lastname[0]);
printf("File where you want to save your name:");
scanf("%s",&fname[0]);
FILE *fptr;
if((fptr = fopen("filename.txt",opening_mode)) == NULL) {
printf("Failed to open file (flename.txt).");
exit(1);
}
else{
fprintf(fptr,"%s %s",&firstname[0],&lastname[0]);
printf("\nSuccessfully saved the data!");
fclose(fptr);
}
return 0;
}
Output should be something like this:
# ./a.out
The program saves your first and last name into a file.
Enter your first name:
John
Enter your last name:
Doe
File where you want to save your name:
filename.txt
Successfully saved the data!#
# ./a.out
The program saves your first and last name into a file.
Enter your first name:
David
Enter your last name:
Smith
File where you want to save your name:
file.dat
Successfully saved the data!#
My code output
# ./a.out
The program saves your first and last name into a file.
Enter your first name:
John
Enter your last name:
Doe
File where you want to save your name:
filename.txt
Successfully saved the data!#
so is if file name say 'file.dat', is entered, my code doesn't work
This question has not been answered.
Create a free account to get help with this and any other question!
Similar Content
The Sun Is Also a Star
by Nicola Yoon
Dracula
by Bram Stoker
Killers of the Flower Moon
by David Grann
Dr Jekyll And Mr Hyde
by Robert Louis Stevenson
The Call of the Wild
by Jack London
The King Must Die
by Mary Renault
Normal People
by Sally Rooney
The Joy Luck Club
by Amy Tan
Alice in Wonderland
by Lewis Carroll
Studypool values your privacy. Only questions posted as Public are visible on our website.
Brown University
1271 Tutors
California Institute of Technology
2131 Tutors
Carnegie Mellon University
982 Tutors
Columbia University
1256 Tutors
Dartmouth University
2113 Tutors
Emory University
2279 Tutors
Harvard University
599 Tutors
Massachusetts Institute of Technology
2319 Tutors
New York University
1645 Tutors
Notre Dam University
1911 Tutors
Oklahoma University
2122 Tutors
Pennsylvania State University
932 Tutors
Princeton University
1211 Tutors
Stanford University
983 Tutors
University of California
1282 Tutors
Oxford University
123 Tutors
Yale University
2325 Tutors