Ifstream filename passing problem

This is a discussion on Ifstream filename passing problem within the C++ Programming forums, part of the General Programming Boards category; In my test program (main) I am asking the user to give me a filename in case 2: Code: #include ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    Cool Ifstream filename passing problem

    In my test program (main) I am asking the user to give me a filename in case 2:

    Code:
    #include <iostream>
    #include <string>
    #include "Tree.h" // Tree class definition
    #include "AddressBook.h"
    
    using namespace std;
    
    
    int main()
    {
       //Tree< string > stringTree;
        AddressBook entry;
        string someName, someAddress, somePhone;
        string val;
    	string nameOfFile;
    	int choice;
    	int num = 0;
    
    	cout << "What would you like to do? "<< endl;
    	cout << "1=insert" << endl;
    	cout << "2=Read in a file: " << endl;
    	cout << "3=print the nodes" << endl; 
    	cout << "4=delete a node" << endl;
    	cout << "5=Write out the File" << endl;
    	cout << "9=exit "<< endl;
    	cout << endl;
    	cin >> choice;
    	cin.ignore(100, '\n');
    	while(choice !=9)
    	{
    		switch (choice)
    		{
    		case 1: cout << "Enter a name for the list: " << endl;
    				entry.addPerson(someName, someAddress, somePhone);
    				break;
    		case 2: cout << "Enter the name of the file you want to read: " << endl;
    				getline(cin, nameOfFile);
    				entry.readFromFile(nameOfFile);
    				break;
    		case 3: cout << "Here is the List:"<<endl;
    				entry.printAddressBook();
    				break;
    		case 4: cout << "Which name would you like to delete?" << endl;
    				getline(cin, val);	
    				entry.deletePerson(val);
    				break;
    		case 5: cout << "File will now be saved." << endl;
    				entry.writeToFile(nameOfFile);
    				break;
    
    		default: cout << "Invalid Selection" << endl;
    		}//end switch
    	
    		cout << endl;
    		cout << "What would you like to do? "<< endl;
    		cout << "1=insert" << endl;
    		cout << "2=Read in a file:" << endl;
    		cout << "3=print the nodes" << endl; 
    		cout << "4=delete a node" << endl;
    		cout << "5=Write out the file " << endl;
    		cout << "9=exit "<< endl;
    		cout << endl;
    		cin >> choice;
    		cin.ignore(100, '\n');
    		
    		}//end while  
    
         cout << endl;
       return 0;
    } // end main
    That filename is (supposed to be) passed to a function readFromFile() that looks like this:

    Code:
    void AddressBook::readFromFile(string fileName)
    {
    	ifstream infile;
    
    	string newName;
    	string newAddress;
    	string newPhone;
    
    	infile.open("fileName.txt");
    	if(!infile)
    	{
    		cout<<"The input file does not exist."<< endl;
    	}
    	getline(infile, newName);
    	while(infile)
    	{
    		getline(infile, newAddress);
    		getline(infile, newPhone);
    		const Person *newPerson = new Person(newName, newAddress, newPhone);
    		addressTree->insertNode(*newPerson);
    
    		getline(infile, newName);
    	}//end while
    	infile.close();
    		//open the file
    }//end readFromFile
    for some reason, if I try to pass the filename it doesn't work. It says the file doesn't exist. So, in an effort to figure out what is wrong, I've tried debug and can't locate the problem. So, through the process of elimination I've tried the following:
    I've tried it with the quotes, without .txt with the user putting in the .txt, and neither work. If I remove the quotes and leave the .txt I get an error message:

    c:\program files\microsoft visual studio\myprojects\bst addressbook\addressbook.cpp(46) : error C2039: 'txt' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

    If I remove the quotes and remove the .txt I get this error message:

    c:\program files\microsoft visual studio\myprojects\bst addressbook\addressbook.cpp(46) : error C2664: 'void __thiscall std::basic_ifstream<char,struct std::char_traits<char> >:: open(const char *,int)' : cannot convert parameter 1 from 'class std::b
    asic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


    I am happy to post the entire program, which is very lengthy, if needed. It is a parameter of the assignment that I pass a file name this way, so I can't do it the easy way (which works) that is by simply putting the name of the file instead of the variable.

    Any ideas?

    Thank you for your help!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >infile.open("fileName.txt");
    This looks for filename.txt, not whatever you passed. Try this instead:
    Code:
    infile.open ( fileName.c_str() );
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    You've helped me before. Once again you've saved the day! Thank you! That is what was needed.

    tms

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-29-2008, 05:29 AM
  2. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. variable passing problem
    By bsimbeck in forum C Programming
    Replies: 3
    Last Post: 02-09-2003, 05:43 PM
  5. problem passing an option as command line argument
    By papous in forum C Programming
    Replies: 3
    Last Post: 11-22-2001, 05:12 PM

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