String instead of char

This is a discussion on String instead of char within the C++ Programming forums, part of the General Programming Boards category; Why cant i use string instead of char for the user input in this function? Code: ifstream* openFile(){ ifstream* file1 ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    String instead of char

    Why cant i use string instead of char for the user input in this function?

    Code:
    ifstream* openFile(){
              
            ifstream* file1 = 0;
            for(;;)
            {
                char fileName[88];
                cout << "Enter the name of your file: " << endl;
                cin >> fileName;
                file1 = new ifstream(fileName);
                if (file1 -> good())
                {
                    break;
                }
                cerr << "\nThere is no file, named --> " << fileName << " <-- in your folder.\n\n" << endl;
                delete file1;
            }
            return file1;
    }
    Thanks in advance!

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You can. You need to use the c_str() function to return a char*, however.

    Code:
    std::string fileName;
    ...
    file = new ifstream(fileName.c_str());
    Your solution is not quite clean though. You're never even deallocating memory.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You can't (not directly anyway... yet?) because there is no overloaded version of the constructor which takes a string parameter and there is no pre-defined conversion which would automatically converts one into the type of argument the constructor does take (const char* not char*). Therefore, the above mentioned c_str() member function needs to be used in such cases to convert the object into the proper type.
    "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

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thanks a lot guys for the explanation!

    Quote Originally Posted by Desolation View Post
    You're never even deallocating memory.
    Well i thought i did with "delete file1;" didn't i ?
    Last edited by Ducky; 02-08-2008 at 06:34 AM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can't (not directly anyway... yet?)
    Yet. As per N1981, already integrated into the working paper, this will be possible in C++09.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by Ducky View Post
    Thanks a lot guys for the explanation!



    Well i thought i did with "delete file1;" didn't i ?
    Oh, sorry, I didn't pay attention enough. The pointer you are returning is not valid, then.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Yes you're right, thanks for noticing.

    So i should "delete" it after "return" i guess.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> The pointer you are returning is not valid, then.
    Yes it is, because that code is inside a loop and the loop will only be broken after the allocation and before the deallocation.

    >> So i should "delete" it after "return" i guess.
    No, because nothing is executed after the return. Your code is already correct, it just requires the code that calls the function to delete the pointer when it is done with the file.

    However, I don't see the point of using dynamic memory allocation here. I would do something like this:
    Code:
    void openFile(ifstream& file){
            file.clear();
            for(;;)
            {
                string fileName;
                cout << "Enter the name of your file: " << endl;
                cin >> fileName;
                file.open(fileName.c_str());
                if (file.good())
                {
                    break;
                }
                cerr << "\nThere is no file, named --> " << fileName << " <-- in your folder.\n\n" << endl;
                file.clear();
            }
    }
    That way there needs to be no dynamic memory allocation and no cleanup. Call it like this:
    Code:
    std::ifstream myfile;
    openFile(myfile);

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thanks Daved!

    Though i get a "too few arguments to function" error.
    Last edited by Ducky; 02-08-2008 at 12:46 PM.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    probably you are not including all the headers - I have no problem compiling the following code:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    void openFile(std::ifstream& file)
    {
    	file.clear();
    	for(;;)
    	{
    		std::string fileName;
    		std::cout << "Enter the name of your file: " << std::endl;
    		std::cin >> fileName;
    		file.open(fileName.c_str());
    		if (file.good())
    		{
    			break;
    		}
    		std::cerr << "\nThere is no file, named --> " << fileName << " <-- in your folder.\n\n" << std::endl;
    		file.clear();
    	}
    }
    
    int main()
    {
    	std::ifstream f;
    	openFile(f);
    	return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thanks i forgot to change "openFile(f);" in "int main()".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21