Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete (with all necessary declaration) function called loadMyIncome t

ID: 3808210 • Letter: W

Question

Write a complete (with all necessary declaration) function called loadMyIncome that: 1. requires an argument as a char pointer to the file name. 2. The function must open the file named in the argument for reading, 3. Must insure that the file was opened successfully. 4. Must read the file line by line using "fscanf ' storing the read data in char array called myLine assuming that no line is longer that 80 characters (some may be exactly 80 characters long) 5. Must continue reading until it encounters the end of file. 6. Write a main that called this function and reads the file generated in part 1 above and print every part read after each scan on a separate line. 7. Explain why the outputs are different.

Explanation / Answer

A function is used to execute the procedures or task which satisfies the requirement of the given scenario.

1. I have created a function int loadMyIncome(FILE* fp) ,

2. To open a file , we need to use the "fopen" file stucture as follow:- FILE *fp = fopen(argv[0], "r");

3. To check the condition and make sure to pass the successful message on completion,

4. I have used the "fscanf" to read the data from stream and will always store the data into the parameters using " r = fscanf(fp, "%f ", myLine);",

5. To handle the function till the End of the file, Remember to check the data whether it reaches the end of the file using the following " while ((x = fgetc(fp)) != EOF) ",

6. I have created a main function by calling the loadMyIncome function into it,as int main(int argc, char *argv[]),

7. I have attached the function below along with the comments:-

Code:-

// It refers to header file which contains all our function declartion
#include <stdio.h>
#include <stdlib.h>
using namespace std;

// The function prototype/ declartion syntax:

int loadMyIncome(FILE* fp) {
int x;
   // The fgetc is mainly used to fetch the next character from the array.
  
while ((x = fgetc(fp)) != EOF) {
  
   // The putchar is helpful in writing a specific character.
  
putchar(x);
}
   // It closes the file.

fclose(fp);
}


//It is a main method

int main(int argc, char *argv[]) {
  
//It will open a new file

   FILE *fp = fopen(argv[0], "r");
   // Checking the condition
   if (fp == NULL)
{
printf (" Error occured when opening a file '");
exit(EXIT_FAILURE);
}
   char ch, *myLine[80];

   if (argc == 3)
{
r = fscanf(fp, "%f ", myLine);
       while(ch != ' ')
{
fscanf(fp, "%s %d ", myLine[i]);
i++;
ch = fgetc(fp);
}
   else {
loadMyIncome(fp);
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote