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

Call a function to read the file into reader. The name of the file will be passe

ID: 3662316 • Letter: C

Question

Call a function to read the file into reader. The name of the file will be passed as the command line parameter. Note that many of the lines in the file start with a tab character, and use tabs to separate fields. You should replace these tabs with spaces. You should also remove all ' ' from the lines. Ignore all directive lines which start with a period "." and the "main:" line.

Using the following code, I was only successful in removing "main:" from the file and replacing tab characters with spaces. Unfortunately am unable to remove the directives which are lines that start with a period and don't know if I'm also removing ' '. What part in my code is failing to do those objectives. Finally I want to be able to store the information in a char type array.   

void read(Reader* r, char filename[] )//------delete----//
{

ifstream fin;
  fin.open(filename);

while (!fin.eof())
{
  const int SIZE = 100;
  char line[SIZE];
  char* str = nullptr;
  int index = 0;
fin.getline(line, SIZE);
  
  str = strstr(line, "main:");
  if(str)
  continue;

  for (size_t i = 0; i < strlen(line); i++)
  {

     if (line[i] == ' ')
      line[i] = ' ';
      if (line[i] == '.')

      continue;   
    }
  

}

} //read()

Explanation / Answer

Good Wishes,

You should replace these tabs with spaces.

Your code for this is correct

if (line[i] == ' ')
line[i] = ' ';

You should also remove all ' ' from the lines

I am replacing ' ' with emptyness using '' thus removing them. This is to be done as follows

if (line[i] == ' ')
line[i] = '';

Ignore all directive lines which start with a period "."

This is taken care by

if(line[0]=='.')
continue;

Ignore all directive lines which start with the "main:" line.

this is same as what you did, I did not change as you said it is working fine.

str = strstr(line, "main:");
if(str)
continue;

Finally I want to be able to store the information in a char type array

While processing each character in the line 'line' I copied it to 'result' array.The information is stored in char type array 'result'. and finally placing a null character at the end.

for (size_t i = 0; i < strlen(line); i++)
{
if (line[i] == ' ')
line[i] = ' ';
if (line[i] == ' ')
line[i] = '';
result[j]=line[i];
j++;
}
  
}
result[j]='';

Complete Program

void read(Reader* r, char filename[] )//------delete----//
{
ifstream fin;
size_t j=0;
fin.open(filename);
while (!fin.eof())
{
const int SIZE = 100;
char line[SIZE],result[1000];
char* str = nullptr;
char* str1 = nullptr;
int index = 0;
fin.getline(line, SIZE);
  
str = strstr(line, "main:");
if(str)
continue;

if(line[0]=='.')
continue;

for (size_t i = 0; i < strlen(line); i++)
{
if (line[i] == ' ')
line[i] = ' ';
if (line[i] == ' ')
line[i] = '';
result[j]=line[i];
j++;
}
  
}
result[j]='';
} //read()

As you said the program is working well in removing "main:" from the file and replacing tab characters with spaces.

I did not change them(the working part of the program is untouched). I took default sizes for array while they are declared if you are concerned about memory you can use malloc(dynamic memory allocation).

Hope it is clear. Please share your valuable comments.

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