Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. file searching problem

file searching problem

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmshelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Binary0110
    wrote on last edited by
    #1

    I'm trying to search an input file for all occurences of a user specified string and return how many times the word was found and read that into an output file. For some reason the the value of the string changes to every word in the text file at runtime. What am I doing wrong? #include #include #include #include using namespace std; int main(void) { string filename; cout << "Please enter the name of the file: " ; cin >> filename; string search; cout << "Please enter the word to search for"; cin >> search; ifstream fin(filename.c_str()); while (fin.fail()) { cout << "invalid file name"<< endl; cout << endl << "Please enter the name of the file: "; cin >> filename; fin.clear(); fin.open( filename.c_str() ); } int count=0; while (fin >> (search)) { count++; } ofstream fout("output.txt"); fout << "The number of times "<< search <<" was found is "<< count << endl; system ("pause"); return 0; } BINARY

    C 1 Reply Last reply
    0
    • B Binary0110

      I'm trying to search an input file for all occurences of a user specified string and return how many times the word was found and read that into an output file. For some reason the the value of the string changes to every word in the text file at runtime. What am I doing wrong? #include #include #include #include using namespace std; int main(void) { string filename; cout << "Please enter the name of the file: " ; cin >> filename; string search; cout << "Please enter the word to search for"; cin >> search; ifstream fin(filename.c_str()); while (fin.fail()) { cout << "invalid file name"<< endl; cout << endl << "Please enter the name of the file: "; cin >> filename; fin.clear(); fin.open( filename.c_str() ); } int count=0; while (fin >> (search)) { count++; } ofstream fout("output.txt"); fout << "The number of times "<< search <<" was found is "<< count << endl; system ("pause"); return 0; } BINARY

      C Offline
      C Offline
      Curtis Schlak
      wrote on last edited by
      #2

      Binary, Your problem exists here:

      while (fin >> (search))
      {
      count++;
      }

      You are reading a "word" from your file and storing it in the string that you declared to hold your search term. Instead, reuse the filename variable or declare a new one.

      while (fin >> (filename))
      {
      // If the "word" in filename equals the
      // word in search, then
      count++;
      }

      "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty -- modified at 16:04 Wednesday 23rd November, 2005

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups