Thread: Searching through files

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Searching through files

    Ok, my assignment is to create a menu driven program that will allow the user to input the name of the file they want to use and then either tell how many words are in it, what the longest word is and the average word length, or find how many times a word occurs.

    I _think_ (fingers crossed) I can make it do all of those things, if someone can explain how to get it to open a file when the name is typed in by the user, and how to make it go through that file.

    Much Thanks,
    Atropos

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by Atropos
    I _think_ (fingers crossed) I can make it do all of those things, if someone can explain how to get it to open a file when the name is typed in by the user, and how to make it go through that file.

    Much Thanks,
    Atropos
    Are you saying that you can do all those things IF someone does them for you
    Anyway, the rule is, show what you got and what the problem is. It's a class assignment after all so I suppose everything you need is the textbook or class notes.
    Peace and good luck!
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    Opps. Sorry, improper phrasing. I need to know how to phrase the open file statement (with an imputed file name) and then if I need to change the beginning of the loop that reads through the file.

    I can make it get the filename and once I have the comp happily opening the file I have the knowledge to tell it what to search for.

    I have a book, but also a problem. The book uses a version of C++ that comes with apstream.h (I believe that is the name), but our school computers do not have that version, so I do not know how to phrase the open statement (since the command for it is executable when you include apstream).


    This is as close as I can remember to what I have (due to copywrite laws I am not allowed a copy of C++ for home use).

    #include<iostream.h>
    #include<iomanip.h>
    #include<string.h>
    #include<fstream.h>


    main()

    {

    char filename[30];
    int word;
    int count;

    ifstream teachfile; ///Or would it be ofstream?


    cout<<”What file would you like to go through?”<<endl;
    cin.getline(filename,30);

    teachfile.open(???????) //What? do I need a drive name, " ", etc

    teachfile >> word ///Here’s the section where it all goes bad…..
    count=0;
    while (! teachfile.fail()) /// ?????
    {
    count=count+1;
    teachfile>>word;
    }
    }


    ((That would be just the section that would count the words.))
    Last edited by Atropos; 03-30-2004 at 08:38 PM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Maybe this will get you started:

    Code:
    #include<iostream.h>
    #include<fstream.h>
    
    
    int main()
    
    {
    
      char filename[30];
      int word;
      int count;
    
    
    
      cout<<"What file would you like to go through?"<<endl;
    
      cin.getline(filename,30);
    
      ifstream teachfile(filename);
    
      if (teachfile.fail()) {
        cout << endl << "Can't open file <" << filename << "> for reading." << endl;
        return 1;
      }
    
      cout << endl << "Opened input file <" << filename << ">" << endl;
    
      // now do your stuff!
      
      teachfile.close();
    
      return 0;
    }
    Good luck.

    Dave
    Last edited by Dave Evans; 03-30-2004 at 09:17 PM.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by Atropos
    I have a book, but also a problem. The book uses a version of C++ that comes with apstream.h (I believe that is the name), but our school computers do not have that version, so I do not know how to phrase the open statement (since the command for it is executable when you include apstream).


    This is as close as I can remember to what I have (due to copywrite laws I am not allowed a copy of C++ for home use).
    You could always download Bloodshed's Dev-C++. Its a free IDE with a compiler and it is pretty much up to the standards (AFAIK)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    Thank you for the help.

    Thanks also for the suggestion, and I'll probably dig that one up somewhere, but because all programs I write must be runable on the school version I need to make it usable on Borland 4.5. Hopefully I can get one of the techs with the password to make me a copy as I don't have 40 euros... or any euros...

    Much Thanks
    Atropos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  2. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Searching txt files (another file i/o qn)
    By Catif in forum C++ Programming
    Replies: 9
    Last Post: 05-13-2002, 04:14 PM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM