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

. NOTES: On the substring detection problem, we are only processing the file one

ID: 3824709 • Letter: #

Question

. NOTES: On the substring detection problem, we are only processing the file one line at a time. It is necessary to find all instances of the given substring within a single line (and, of course, there may be more than one), but it is not necessary to look for instances of a given substring that may span two lines.

2.    As noted in class and in the slides, the fgets() function will put a newline (' ') in the target string if it finds one in the source (e.g., keyboard or file). If fgets() is used to get the name of the input file, this newline character can cause the fopen() function to fail, as the file names on our hard drives do not contain newlines. Therefore, it is necessary to remove the newline before executing the fopen() function call.  

This can be done with the following code:

char * ptr = strchr (str, ' ');
if (ptr != NULL)
    *ptr = '';
    
where "str" is the character array containing the newline. Here we set the newline to a '', terminating the string at that point

Program in C, compatible with Visual Studio please, The good answer will be rated first.The following is the content of the file decl of indep:

Program 2 -Detecting Substrings Introduction A very common task that is often performed by programs that work with text files is the problem of locating a specific substring within the file. I'm sure we've all done this many times in working with Word, Notepad, or other editors. Since we don't have a GUI or other means of displaying the contents of a file all at once, let's modify the problem slightly. Rather than locating a specific substring within a file and then highlighting the results, as most modern programs do, let's write a C program that locates the occurrences of a specific substring within a file and then display the occurrence number as well as a portion of the text around the found substring. It should also count the number of occurrences and indicate that in a brief report at the end. Although it should work on any input file and any substring, we'll provide a copy of an input file to use for testing (i.e "Decl of Indep txt"). Overview Here's a high level overview of what your program should do: 1) Ask the user for the name of the input file a) If the file is not present or can't be opened, display an error message and ask the user to enter another file name. 2) Ask the user for the substring to search for 3) Calculate and display the found occurrences of the substring in the file. For each found occurrence, your program should display (a) the location number starting at 1 and going up to the total number of locations found, and (b) the portion of the string containing the found substring. This portion should consist of the substring and 8 characters before and 8 characters after the found substring.

Explanation / Answer

1

ans:

//Using read() member function

#include <fstream.h>
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 200

class Employee
{
   protected:
       int eno;
       char ename[20],job[15];
       float sal,comm,np;
   public:
       void dispData();
};

void Employee :: dispData()
{
   cout << endl << "No: " << eno << endl;
   cout << "Name: " << ename << endl;
   cout << "Job: " << job << endl;
   cout << "Salary: " << setprecision(3) << sal << endl;
   cout << "Comission: " << setprecision(3) << comm << endl;
   cout << "Netpay: " << setprecision(3) << np << endl;
}

void main(void)
{
   clrscr();
   char fname[12];
   int j;
   Employee e[MAX];
   ifstream f;
   cout << " Enter the file name: ";
   cin >> fname;

   //Reading the records from the file using read() member function
   f.open(fname);
   cout << " Employee list....! ";
   j=0;
   while(1)
   {
       f.read((char *) &e[j],sizeof(e[j]));
       if(f.eof())
       break;
       e[j].dispData();
       getch();
       j++;
   }
   getch();
   f.close();
}

2.

ans


#include <fstream.h>
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 20

class Employee
{
   protected:
       int eno;
       char ename[20],job[15];
       double sal,comm,np;
   public:
       Employee()
       {
           comm=0;
           np=0;
       }
       void getData();
       void dispData();
       void findComm();
       void findNp();
};

void Employee :: getData()
{
   cout << "Eployee no: "; cin >> eno;
   cout << "Employee name: "; cin >> ename;
   cout << "Employee job: "; cin >> job;
   cout << "Employee sal: "; cin >> sal;
}

void Employee :: dispData()
{
   cout << "No: " << eno << endl;
   cout << "Name: " << ename << endl;
   cout << "Job: " << job << endl;
   cout << "Salary: " << setprecision(3) << sal << endl;
   cout << "Comission: " << setprecision(3) << comm << endl;
   cout << "Netpay: " << setprecision(3) << np << endl;
}

void Employee :: findComm()
{
   if(strcmp(job,"Manager")==0)
   comm=0;
   else
   comm=sal * float(0.12);
}

void Employee :: findNp()
{
   np = sal + comm;
}

void main()
{
   clrscr();
   char fname[12];
   int n,i;
   Employee e[MAX];
   fstream f;
   cout << " Enter the file name: ";
   cin >> fname;
   f.open(fname,ios::in,ios::out);
   cout << " Enter no of records: ";
   cin >> n;

   cout << " Enter the employees information..! ";
   for(i=0; i<n; i++)
   {
       cout << endl << "Record: " << (i+1) << endl;
       e[i].getData();
       e[i].findComm();
       e[i].findNp();
   }
   f.open(fname,ios::out);
   cout << " Writing to the file....! ";
   for(i=0;i<n;i++)
   {
       f.write((char *) &e[i],sizeof(e[i]));
   }
   cout << endl << n << " Records written sucessfully....!";
   f.close();
   clrscr();
   //Reading the records from the file using read() member function
   f.open(fname,ios::in);
   cout << " Employee list....! " << endl;
   for(i=0;i<n;i++)
   {
       f.read((char *) &e[i],sizeof(e[i]));
       if(f.eof())
       break;
       e[i].dispData();
       cout << endl;
       getch();
   }
   f.close();
   getch();
}
#include <fstream.h>
#include <iomanip.h>
#include <conio.h>

class Student
{
   private:
       int rno;
       char sname[20],course[15];
       double cf;
   public:
       void getData(void)
       {
           cout << "Enter roll no: "; cin >> rno;
           cout << "Enter name: "; cin >> sname;
           cout << "Enter course: "; cin >> course;
           cout << "Enter fees: "; cin >> cf;
       }
       void dispData(void)
       {
           cout << setw(10) << rno
           << setw(15) << sname
           << setw(10) << course
           << setprecision(3) << setw(10) << cf << endl;
       }
};

void main(void)
{
   Student s;
   fstream f;
   clrscr();
   f.open("Student.dat",ios :: ate | ios :: in | ios :: out |
           ios :: binary);
   f.seekg(0,ios::beg); //go to start
   cout << " Current student details.....!" << endl;
   while(f.read((char *) &s, sizeof(s)))
   {
       s.dispData();
   }
   f.clear(); //Turn off EOF flag

   //Adding one more record

   cout << " Input data for one more student...! ";
   s.getData();
   f.write((char *) &s,sizeof s);

   //Display the appended file

   f.seekg(0); //go to the start

   cout << " Contents of the student file ";
   while(f.read((char *) &s,sizeof s))
   {
       s.dispData();
   }

   //Find number of records in the file

   int last = f.tellg();
   int n = last / sizeof(s);
   cout << " No of records: " << n << endl;
   cout << " Total bytes in the file: " << last << endl;

   //Edit an existing student record

   cout << " Enter record no to be updated: ";
   int rn;
   cin >> rn;

   int loc = (rn - 1) * sizeof(s);

   if(f.eof()) f.clear();
   f.seekp(loc);
   cout << " Enter new values for the record ";
   s.getData();
   f.write((char *) &s,sizeof s) << flush;

   //Displaying updated file

   f.seekg(0);
   cout << " Contents of updated file ";
   while(f.read((char *) &s, sizeof s))
   {
       s.dispData();
   }
   f.close();
   getch();
}

3.

ans

       /* reading the information from a file */
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
void main(void)
{
   clrscr();
   ifstream in;
   char fname[10];
   char ch;
   cout << " Enter any file name: ";
   cin >> fname;
   in.open(fname);
   if(in.fail())
   {
       cerr << " No such file exists....! ";
       getch();
       exit(1);
   }
   while(!in.eof())
   {
       ch=(char)in.get();
       cout << ch;
       //delay(500);
   }
   in.close();
   getch();
}