Question

In: Computer Science

Create a header file, iofunctions.h, containing the following function prototypes. (don't forget to follow the coding...

Create a header file, iofunctions.h, containing the following function prototypes. (don't forget to follow the coding style)

int readfile( struct pokemon pokearray[ ], int* num, char filename[ ] );
int writefile( struct pokemon pokearray[ ], int num, char filename[ ] );

Then, create a source file, iofunctions.c, defining the functions above.

Specifications

  • The readfile function will read the data from a text file and store it in the array called by pokearray.

    • It must not load the pokemons more than the array can hold. If the array size is 10, the function must not load more than 10 pokemons.

    • It must tell the function which called readfile how many pokemons it has stored into the array.

  • The writefile function will write the data stored in the array into a text file.

    If the text file already exits with some data, writeifile must OVERWRITE it with the newly given data. For example, if the file has one pikachu and the array has a charizard and a bulbasaur, the file will only have a charizard and a bulbasaur after writefile function is called.

  • The return values must be used to tell whether the functions successfully read/wrote data.

  • The size of the array and the number of pokemons in the text file are assumed to be arbitrary (You cannot assume the array size is always 5).

  • The file name of the text file is also arbitrary.

  • The text file must be just a sequence of items, i.e., it must not tell how many pokemons it contains at the beginning.

    For example, the contents in a file should look like this:

    16
    Pikachu
    9
    Metapod
    70
    Mewtwo
  • They must not crash (segmentation fault) even if the file does not exist or cannot be open. (first run, some problem in the file system, etc)

Reminder: readfile and writefile functions must not have the terminal I/O's (printf, scanf, etc).

Solutions

Expert Solution

The code consists of two files iofunctions.h and iofunctions.c

iofunctions.h==>this header file contains structure pokemon and prototypes of readfile and writefile functions.....

readfile function has three agrument==>

(i) pokearray[ ] ==>this parameter is used to store the data read from the file

(ii) num ==> which is a reference to the variable in main function and hence store the number of pokemons stored in the array..

(iii) filename[] ==> which stores the filename from which data is to be read

writefile function has three agrument==>

(i) pokearray[ ] ==>this parameter has data stored which is used to write  data to file

(ii) num ==> which is a variable that tells the number of pokemons in the array

(iii) filename[] ==> which stores the filename on which data is to written

From main function the input of maxsize and input of file name to be read and wriiten is taken ..

The code snippet and screen shot has been attached

iofunctions.h

#include<stdio.h>
//Declaring the structure pokemon
struct pokemon{
  char name[50];//name of pokemon
  int data;//data associated with it

};

int readfile(struct pokemon pokearray[],int *num,char filename[]);

int writefile(struct pokemon pokearray[],int num,char filename[]);

iofunctions.c

#include <stdio.h>
#include <stdlib.h>
#include "iofunctions.h"
#include<string.h>
int max_size;
int readfile(struct pokemon pokearray[],int *num,char filename[])
{
  FILE *fptr;  //file pointer
  //initialize the file pointer.. if it returns NULL then error must be there in reading file
  if ((fptr = fopen(filename, "r")) == NULL)
     {
       return 0; //unsuccessful reading of file
        // function returns 0 if pointer returns NULL.
     }
  int count =0;//denotes number of pokemons
  int data; //number attached to each pokemon
  char name[50]; //name of pokemon

  //check if count is less than max size ..check whether file has reached end of line or not
  //if not EOF then data get the data stored with pokemon
  while(count<max_size&&fscanf(fptr,"%d",&data)!=EOF)
  {
      count++; //increase the count of pokemon
      fscanf(fptr,"%s",name); //read the name from file
     pokearray[count-1].data=data; //store the data in array
     strcpy(pokearray[count-1].name,name);//store the name in array

  }
 *num=count;//store the count in num
 return 1;//means reading the file is successful


}
int writefile(struct pokemon pokearray[],int num,char filename[])
{
    FILE *fptr; //delcare the file pointer
  //initialize the file pointer.. if it returns NULL then error must be there in writing file

   if ((fptr = fopen(filename, "w")) == NULL) {
       return 0;
        // function returns 0 if file pointer returns NULL.
     }
   for(int i=0;i<num;i++)
   {
       fprintf(fptr,"%d\n",pokearray[i].data); //write data to file
       fprintf(fptr,"%s\n",pokearray[i].name);//write name to file

   }
     return 1;//means writing the file is successful


}


int main()
{
printf("Enter the max size of array \n");
scanf("%d",&max_size);//take input of max size of array
struct pokemon *pokearray =(struct pokemon*)(malloc)(sizeof(struct pokemon)*max_size);//allocate space
int num=0;//intialize num variable
printf("Enter the file to be read\n");
char filename[50];
scanf("%s",filename);//input the file name to be read

 if(readfile(pokearray,&num,filename)==0) //call the readfile function
 {
     printf("Error in reading the file\n"); //error

 }
 else
 {
     printf("File read successfully\n %d pokemons has been stored in the array\n",num);
     printf("Enter the name of the file on which data is to be written\n");
     scanf("%s",filename);//input the file name of output file

     if(writefile(pokearray,num,filename)==0)
     {
         printf("Error in writing the file\n");//error


     }
     else
     {
         printf("Data has been written successfully");

     }


 }







}

Screenshots


Related Solutions

Create a Namespaces.h header file containing a namespace declaration yourname. The declaration should include: a method...
Create a Namespaces.h header file containing a namespace declaration yourname. The declaration should include: a method with the signature void message (string, ostream &) that prints a string to the output stream. a method with the signature void message (double, ostream &) that prints a double to the output stream. Create a testNamespaces.cpp that uses the yourname namespace, and invokes the message() string method with a message of your choice and cout as input parameters, and invokes the message double...
C++: Write the following function prototypes that would be declared for the Cplx class (you don't...
C++: Write the following function prototypes that would be declared for the Cplx class (you don't need to write the function definitions or write the rest of the class). Cplx class is a complex class with lots of data. Remember to show the return type, and const where needed. For all your parameters, call them lval or rval to show that you understand where they would be in the operation. // << function // + as a non-member function //...
Given the header file (grid.h) and the source file,(grid.cpp) create the file trap.cpp. Here are the...
Given the header file (grid.h) and the source file,(grid.cpp) create the file trap.cpp. Here are the requirements for trap.cpp: Write a main program in a file called trap.cpp that solves the following scenario, using your Grid class: Giant Mole People have risen from their underground lairs and are taking over the world. You have been taken prisoner and placed in an underground jail cell. Since the Mole People are blind and don't know how to build doors, your cell has...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName,...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName, lastName, startYear. recordID is an integer. In testRecord, first create a record (record1) using “normal” pointers. Set the values to 1001, Fred, Flintstone, 1995 In testRecord, create a new record (record2) using a unique smart pointer. Set the values to 1002, Barney, Rubble, 2000 Create a function (or functions) that will print the records to any output stream. Since the print function does not...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this...
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this header file, declare an Employee class with the following features: Private members a std::string for the employee’s first name a std::string for the employee’s last name an unsigned int for the employee’s identification number a std::string for the city in which the employee works Public members A constructor that takes no arguments A constructor that takes two arguments, representing: the employee’s first name the...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include <iostream> using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();...
Please use python3 Create the function team_average which has the following header def team_average(filename): This function...
Please use python3 Create the function team_average which has the following header def team_average(filename): This function will return the average number of games won by the Red Sox from a text file with entries like this 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @ Astros Win 2-1 2011-07-04 Red Sox vs Blue Jays Loss 7-9 This function should create a file object for the file whose name is given by the parameter filename. If the file cannot...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
Create an array-based implementation of a binary tree. DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
Create an array-based implementation of a binary tree. DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT