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

Hello, The below program is supposed to edit a text file that has many lines of

ID: 3620412 • Letter: H

Question

Hello,
The below program is supposed to edit a text file that has many lines of text. For example, one line is "ajax/">Ajax (Ontario)".

Part 1 of the Program:
The program is first supposed to delete all the characters up to the ">" character.

Part 2 of the Program:
Than, it is supposed to delete any line where the community (not province) doesn't include a letter in your name. For this example, the name is 'lucas' (all lowercase letters, I don't need to worry about finding "L" and "l").

Part 1 works, and the code for part 2 worked for an example I made. However, when I pasted the same code into this program, it doesn't seem to delete the lines. I tried using the letter 'x', because that would narrow the results down to just a handful of communities, but that didn't work...

I know this program could probably be combined better, but I just want to know if any of you can see the problem with part 2.

Thanks for your help

********************************************************************************************
The code:
***************************
#include
#include

int main()
{
char input[100];
char buffer[100];
int i,j,k,m;
FILE *cityin; //This is the original input text file that includes the
   garbage letters
FILE *editcity1; //This is the first output file that includes all the cities
   without garbage letters
FILE *editcity2; //This is the second output file that only includes
   communities with the letters from 'lucas'


cityin = fopen("city.txt","r"); //Opens the original input file
editcity1 = fopen("cityout.txt","w"); //Opens the first output file

if(cityin == NULL) //Ensures that the input file was opened
{ printf("Error opening input file! ");
return 0;
}

/*************Part 1********************************/

//This code reads the input file, and deletes all the garbage characters up to the '>' character

while(fgets(input, 100, cityin)!=NULL)
{
for(i=0;input[i]!='>';i++);
{k=0;
for(j=i+1;input[j]!='';j++)
input[k++]=input[j];
input[k]='';
}

fprintf(editcity1, "%s",input); //This prints all the communities without the garbage
letters

}
fclose(cityin);
fclose(editcity1);

/**************************Part 2*******************/

//This code is for deleting any lines that do not include the letters in the name 'lucas'
editcity1=fopen("cityout.txt", "r");
editcity2=fopen("cityout2.txt","w");

if(editcity1 == NULL) //Ensures that the input file was opened
{ printf("Error opening input file 2! ");
getche();
return 0;
getche();
}

//This part of the code reads the lines from the first output file and deletes the lines that do not include the letters from 'lucas'

while(fgets(buffer, 200, editcity1)!=NULL)
{
for(m=0;buffer[m]!='(';m++); //This scans the string from the first output
   file up until the '(' character if(strchr(buffer,'l') != NULL || strchr(buffer,'u') != NULL || strchr(buffer,'c') !=
NULL|| strchr(buffer,'a') != NULL|| strchr(buffer,'s') != NULL)

fprintf(editcity2, "%s",buffer); //Prints the remaining communities to the
   second output file
}
fclose(editcity1); //Closes the first and second output files
fclose(editcity2);

getch();
return 0;
}
   file up until the '(' character

Explanation / Answer

please rate - thanks remove the line I've made into a comment--in red would need it if you were checking 1 character at a time, which your not doing
#include<stdio.h>
#include<conio.h>

int main()
{
char input[100];
char buffer[100];
int i,j,k,m;
FILE *cityin; //This is the original input text file that includes the   
              // garbage letters
FILE *editcity1; //This is the first output file that includes all the cities
                    // without garbage letters
FILE *editcity2; //This is the second output file that only includes
                        //   communities with the letters from 'lucas'


cityin = fopen("city.txt","r"); //Opens the original input file
editcity1 = fopen("cityout.txt","w"); //Opens the first output file

if(cityin == NULL) //Ensures that the input file was opened
{ printf("Error opening input file! ");
return 0;
}

/*************Part 1********************************/

//This code reads the input file, and deletes all the garbage characters up to the '>' character

while(fgets(input, 100, cityin)!=NULL)
{
for(i=0;input[i]!='>';i++);
{k=0;
for(j=i+1;input[j]!='';j++)
input[k++]=input[j];
input[k]='';
}

fprintf(editcity1, "%s",input); //This prints all the communities without the garbage
                                //letters

}
fclose(cityin);
fclose(editcity1);

/**************************Part 2*******************/

//This code is for deleting any lines that do not include the letters in the name 'lucas'
editcity1=fopen("cityout.txt", "r");
editcity2=fopen("cityout2.txt","w");

if(editcity1 == NULL) //Ensures that the input file was opened
{ printf("Error opening input file 2! ");
getche();
return 0;
getche();
}

//This part of the code reads the lines from the first output file and deletes the lines that do not include the letters from 'lucas'

while(fgets(buffer, 200, editcity1)!=NULL)
{
//for(m=0;buffer[m]!='';m++); //This scans the string from the first output
                                           //   file up until the '' character

if(strchr(buffer,'l') != NULL || strchr(buffer,'u') != NULL || strchr(buffer,'c') !=
NULL|| strchr(buffer,'a') != NULL|| strchr(buffer,'s') != NULL)

fprintf(editcity2, "%s",buffer); //Prints the remaining communities to the
                                          //     second output file
}
fclose(editcity1); //Closes the first and second output files
fclose(editcity2);

getch();
return 0;
}

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