Files and strings

Sigchi4life
Category:
Programming
Price: $10 USD

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





Tutor Answer

(Top Tutor) Daniel C.
(997)
School: UC Berkeley
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1829 tutors are online

Related Programming questions

11/09/2013
11/09/2013
11/09/2013
11/09/2013
11/09/2013
11/09/2013
11/09/2013
11/09/2013
11/09/2013

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