Thread: Searching for files using strings...

  1. #1
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156

    Searching for files using strings...

    Hey again, I have yet again, another problem with my Database.....

    I'm nearly finished my star wars database . Right now i'm trying to write a function that will search for a file according to user input, but the function will not open the file for some reason, I can't figure out how. Here is the code snippets of relevance:

    DatabaseFileMgr.cpp:
    Code:
    ....
    bool DatabaseFileMgr::bReadFromFile()
    {
    	DatabaseResourceMgr TempResourceManager;
    	char ch; /*Character for reading contents of file*/
    	std::ifstream iFile(m_cFilename); /*Create an ifstream object for opening a file for output*/
    	if(!iFile.is_open()) /*Checks if the file can be opened*/
    	{
    		std::cout << "Error! Unable to open " << m_cFilename << "..." << std::endl << std::endl; /*If it can't, output this*/
    		TempResourceManager.vDisplayExitMessage();
    		return false;
    	}
    	else /*Otherwise...*/
    	{
    		while(iFile.get(ch)) /*Read the characters from the file*/
    		{
    			std::cout << ch; /*Output the file contents to the console*/
    		}
    		std::cout << std::endl; /*Output a new line to the console after file contents is read*/
    		return true;
    	}
    	iFile.close(); /*Close the file after it's purpose has bin fullfilled*/
    }
    
    void DatabaseFileMgr::vSearchForFile()
    {
    	std::string sUserInput;
    	std::cin >> sUserInput;
    	if(sUserInput == "StarDestroyer")
    	{
    		vSetFileName("Imperial-I Star Destroyer");
    		bReadFromFile();
    		vClearFileNameBuffer();
    	}
    }
    ...
    There are the two main functions that I'm using. Can anyone help?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Right now i'm trying to write a function that will search for a file according to user input, but the function will not open the file for some reason, I can't figure out how

    I'm assuming then you're getting the ""Error! Unable to open" message? If so, you either have the filename wrong, not enough permission, or you're in the wrong directory, probably.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Oh I fixed it, stupid mistake, I didn't specify the file type when I set the file name :S
    BUT when I replace:
    Code:
    if(sUserInput == "StarDestroyer")
    ...with:
    Code:
    if(sUSerInput == "Star Destroyer")
    It doesn't register the white space, how can I fix this?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use std::getline instead of just straight std::cin >>. The later will stop reading input at the first whitespace it encounters so if you type "Star Destroyer" then it will only read and store "Star" into the variable sUserInput. You'll need to make sure you've cleared the input buffer/stream prior to calling getline or it may pick up a stray newline character from a previous cin >> operation and think that you've input a blank line.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by hk_mp5kpdw View Post
    Use std::getline instead of just straight std::cin >>. The later will stop reading input at the first whitespace it encounters so if you type "Star Destroyer" then it will only read and store "Star" into the variable sUserInput. You'll need to make sure you've cleared the input buffer/stream prior to calling getline or it may pick up a stray newline character from a previous cin >> operation and think that you've input a blank line.
    Hey there, I'm only familiar with std::cin.getline(), which I tried but it didn't work. Could you please tell me what the parameters are for std::getline()? Sorry, noob question I know

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Hey there, I'm only familiar with std::cin.getline(), which I tried but it didn't work. Could you please tell me what the parameters are for std::getline()? Sorry, noob question I know

    There's no excuse for not looking these things up yourself (enter 'getline' into the search bar on the site). I'd recommend downloading a good reference (or getting a book), for good measure.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Sorry, I was just about to reply to say that I had found out, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  3. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM
  4. Ofstream, Ifstream, Searching files for strings?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 04-04-2005, 03:45 PM
  5. Searching and Comparing Strings Using Parsing
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2002, 10:18 AM