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 //...
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...
Create a Word file containing the following: The full name of the “test subject” The test...
Create a Word file containing the following: The full name of the “test subject” The test subject is a person, not yourself, who can say the names of the products. The best test subject is one who is adamant about being an expert who can distinguish brand A from B. You cannot be the test subject because you generate the sequence, and the test subject cannot see it for an unbiased test. Pets and babies are not allowed as they...
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
Consider the following header file for the intArray object and the following main.cpp and answer the...
Consider the following header file for the intArray object and the following main.cpp and answer the following questions: intArray.h main.cpp #include <iostream> using namespace std; class intArray { friend ostream& operator<<(ostream& outStream, intArray& rhs); public:     intArray();     intArray(int _size);     intArray(int _size, int array[]);     intArray(const intArray&);     ~intArray();     void Print();     void PrintSize();     int Size() const;     int operator[](int) const;     intArray operator=(const intArray ); private:     int size;     int *data; }; 7 #include <iostream>...
Given the following frame, answer the questions that follow. No Ethernet header!
Given the following frame, answer the questions that follow. No Ethernet header!
Use Excel to find the Standard Normal Probability for the following questions. Don't forget to sketch...
Use Excel to find the Standard Normal Probability for the following questions. Don't forget to sketch the normal distribution and shade the required area. a) What is the area in the right hand tail of the standard normal curve beyond z = 1.02? (4dp)   b) What is the area under the standard normal curve between z = -1.94 and z = 0.00? (4dp)   c) What is the z-value that gives the left hand tail area equal to 0.1492? (2dp) d)...
Using Matlab functions ‘fdesign’ and ‘design’, create the following filters (do not forget to create an...
Using Matlab functions ‘fdesign’ and ‘design’, create the following filters (do not forget to create an m file to write a script that should be printed in your report). [0.5] a-c each (a) Low pass Hamming and low pass Hann filters, with cut off frequency Fc=300 Hz, sampling frequency Fs=2000 Hz, filter order N=100. Plot both filters on the same graph and use legend in the figure to mark each filter. Comment on the figure. (b) Low pass Hamming filter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT