Question

//Done in C please!

//Any help appreciated!

Write two programs to write and read from file the age and first and last names of people. The programs should work as follows:

1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the program should function as follows (items in italics to be entered by user):

This program reads and saves ages and names in a binary file.

Enter person (age, first last):40, John Smith

Enter person (age, first last):32, Bill Jones

Enter person (age, first last):0

2. The second program reads in from the text file above (with the fscanf function) and prints out the ages and names on the screen. The program should take the name of the file as a command line argument. The program should function as follows (items in italics to be entered by user):

This program prints ages and names from a text file.

Name: 40, John Smith

Name: 32, Bill Jones

Algorithm design process

1. First program:

a. Declare needed variables.

b. Use command line arguments to open the file specified by the user for writing.

c. Enter the prompt and read the information from the user.

d. In a loop (until the user enters 0)

    i. Use scanf to read age, first name, last name

      ii. Use fprintf to write age, first name, last name to the file

e. Close the file.

2. Second program:

a. Declare needed variables (to store age, name string).

b. Use command line arguments to open the file specified by the user for reading.

c. In a loop until reaching the end of the file (EOF)

               i. Use fscanf to read age, first name, last name

               ii. If fscanf returns an integer not equal to 3, break the loop

              iii. Print the information on the screen.

d. Close the file.

Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0 e This program reads and saves ages and names in a binary file Enter person (age, first last) :40, John Smith Enter person (age, First last) :32, Bill Jones Enter person (age, first last):0 nre, Eirat last):3 The second program reads in from the text file above (with the fscanf function) and prints out the ages and names on the screen. The program should take the name of the file as a command line argument. 2. This program prints ages and names from a text file Name 40, John Smith Name: 32, Bill Jones Algorithm design process 1. First program: a. Declare needed variables. b. Use command line arguments to open the file specified by the user for writing. c. Enter the prompt and read the information from the user d. In a loop (until the user enters 0) i. Use scanf to read age, first name, last name ii. Use fprintf to write age, first name, last name to the file Close the file. e. 2. Second program: Declare needed variables (to store age, name string). Use command line arguments to open the file specified by the user for reading. In a loop until reaching the end of the file (EOF) i. Use fscanf to read age, first name, last name ii. If fscanf returns an integer not equal to 3, break the loop ii. Print the information on the screen. a. b. c. d. Close the file

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

/*
C program that promts user to enter age,
fist name, last name and then write the data
to binary file, person_data.txt*/

//include header files
#include<stdio.h>
#include<string>
#include<conio.h>
//Person structure
struct Person
{
   int age;
   char fname[20];
   char lname[20];

};
int main()
{


   int index=0;
   char comma;
   int age;
   char firstName[20];
   char lastName[20];
   struct Person person;

   printf("This program reads and saves ages and names in a binary file.\n");
   //Prompt user to enter age ,first and last names and write to file
   //in binary mode
   FILE* outFile=fopen("person_data.dat", "wb");
   printf("Enter person (age, first last):");
   scanf("%d",&age);
  
   while(age!=0)
   {
       scanf("%c%s%s",&comma,&firstName,&lastName);
       person.age=age;
       strcpy(person.fname,firstName);
       strcpy(person.lname,lastName);
       //write to file
       fwrite (&person, sizeof(struct Person), 1, outFile);
       printf("Enter person (age, first last):");
       scanf("%d",&age);
   }

   //close the file object
   fclose(outFile);

  
   getch();
   return 0;
  
}

Sample Ouptut:

his program reads and saves ages and names in a binary file Enter person (age, first last):40, John Smith Enter person (age,

-------------------------------------------------------------------------------------------------------------------------------------------

2)

/**
C program that read an input file person_data.txt file
and then print the Person age, name on console output.
*/
//include header files
#include<stdio.h>
#include<string>
#include<conio.h>
//Person structure
struct Person
{
   int age;
   char fname[20];
   char lname[20];

};
int main()
{
   char comma;
   int age;
   char firstName[20];
   char lastName[20];
   struct Person person;
   FILE *infile;   

   // Open person_data.dat for reading
   infile = fopen ("person_data.dat", "r");
   if (infile == NULL)
   {
       fprintf(stderr, "\nError opening file\n");
       exit (1);
   }

   printf("This program prints age and names from a text file.\n");
   // read file contents till end of file
   while(fread(&person, sizeof(struct Person), 1, infile))
       printf ("Name = %d, %s %s\n", person.age,person.fname, person.lname);

   getch();
   return 0;
}

Sample Output:

is program prints age and names from a text file. Name 40, John Smith Name = 32, Bill Jones Th

Note :Do not open person_data.dat since it is in binary format.Not user readble format.

Add a comment
Know the answer?
Add Answer to:
//Done in C please! //Any help appreciated! Write two programs to write and read from file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() -...

    Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() - Prints out the contents of the file "myfile.txt", that was read into a list by readlines(). Note that this should not look like a list, so you will need to loop through the list created by readlines and print the text. - Use the try/except method to create the file if it does not exist - If the file does not exist, prompt the...

  • Write a program in Java according to the following specifications: The program reads a text file...

    Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts the...

  • Create a C++ function that reads a line of text from a file (of any name),...

    Create a C++ function that reads a line of text from a file (of any name), saves the line, and prints it out using cout. Use the string stream class <sstreams>

  • Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify...

    Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify the Person class in Lab #4. It has four private data members firstNam (char[20]), day, month, year (all int); and two public member functions: setPerson(char *, int, int, int) and printInfo(). The function setPerson sets the first name and birthday in the order of day, month and year. The function printInfo prints first name and birthday. But it should print the date in the...

  • Following should be done in C++: Create a program that reads a text file and prints...

    Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Write a program that allows the user to navigate the lines of text in a file....

    Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...

  • Write a C++ program that produces the following output: /* OUTPUT Enter your age: 21 Enter...

    Write a C++ program that produces the following output: /* OUTPUT Enter your age: 21 Enter the last name: Lee Hello Tom Lee. You are 21 years old. Press any key */   Declare an array named: firstName The array is a c_string, i.e., it is a null-terminated character array. The size of the array is 10. Assign a first name to it when it is declared.   Declare an array named: lastName The array is a c_string, The size of the...

  • Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following...

    Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following requirement. You and your partner will be writing a simple Java program that implements some basic file I/O operations. You can use this code as the basis for some of your next project. FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each...

  • Help with my code: The code is suppose to read a text file and when u...

    Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT