This is my code right now, int main(int argc, const char * argv[]) { cout << \"*
ID: 3651657 • Letter: T
Question
This is my code right now,int main(int argc, const char * argv[])
{
cout << "** Name Popularity Program **" << endl << endl;
cout << "Please enter a first name to search for: ";
string FN;
cin >> FN;
ifstream file;
file.open("/Users/grahamarthur/Desktop/NamePopularity/firstnames-mac.txt");
if (file.is_open())
{
while (!file.eof())
{
string R;
getline(file,R);
string name;
string FNinR;
FNinR = R.substr(...);
}
}
return 0;
}
the issue i have right now is that i cannot retrieve the first name in each string R to place into new string variable FNinR. I need to search a file for a name, so i go line by line to extract the name and compare to the input. If they are equal they will output a result, the whole string line. But i cannot compare the imput to the name in the string !!
Explanation / Answer
try this........... #include #include #define MAX_LEN_SINGLE_LINE 120 // ? int main() { const char fileOrig[32] = "myOriginalFile.txt"; const char fileRepl[32] = "myReplacedFile.txt"; const char text2find[80] = "lookforme"; const char text2repl[80] = "REPLACE_WITH_THIS"; char buffer[MAX_LEN_SINGLE_LINE+2]; char *buff_ptr, *find_ptr; FILE *fp1, *fp2; size_t find_len = strlen(text2find); fp1 = fopen(fileOrig,"r"); fp2 = fopen(fileRepl,"w"); while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1)) { buff_ptr = buffer; while ((find_ptr = strstr(buff_ptr,text2find))) { while(buff_ptr < find_ptr) fputc((int)*buff_ptr++,fp2); fputs(text2repl,fp2); buff_ptr += find_len; } fputs(buff_ptr,fp2); } fclose(fp2); fclose(fp1); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.