C++ file reading

Discussion in 'Mac Programming' started by Duke Leto, Aug 1, 2009.

  1. Duke Leto macrumors regular

    Joined:
    Mar 17, 2008
    #1
    I made an empty text file called "menu" in XCode, and I am having trouble opening it in c++.

    Here is the relevant code:
    Code:
    #import <fstream>
    void populateMenu()
    {
    	using namespace std; 
    	{
    		char* line;
    		ifstream menuFile;
    		menuFile.open("menu");
    		
    		int i=0;
    		if(menuFile)
    		{
                                // The code here never gets run
    		}
    	}
    }
    
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Try this:

    Code:
    #include <fstream>
    void populateMenu()
    {
    	using namespace std; 
    	{
    		char* line;
    		ifstream menuFile;
    		menuFile.open("menu", ifstream::in);
    		
    		int i=0;
    		while(menuFile.good())
    		{
                                // The code here never gets run
    		}
    
                    menufile.close();
    	}
    }
    Edit: You may find this website useful: http://www.cplusplus.com/
     
  3. Duke Leto thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #3
    It is still not working.

    Now I remember something else, though. I tried to use this code to make 3d text (it read the vertex data from a file), and when I ran it, the file could not open. This was perfectly good code, so maybe something is wrong with my .plist or something.
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    Does menu have a file extension? If so you need include it in the code. At the moment it will try and open a file called "menu" in exactly the same directory as the executable. This is probably not what you want as Xcode puts files and the execuatables in different directories.

    You can either a) make a symlink to the original file in the relevant directory or b) hardcode the path into your code.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    If you're building a .app then you should use the CFBundle API to get the location of the file if it's in the Resources folder.
     
  6. Duke Leto thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #6
    Ok, now the file is reading, but I do have a new problem. The strings that are loaded are passed to the menu items, but the pointers don't point to where they used to ..

    In other words, at the time of initialization, the menu items have the right text, but by the time they are drawn, the text is empty. How can I ensure that the data does not get wiped when the function exits?
     
  7. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    Return a pointer either to the data itself or to a structure holding the data.
     
  8. Duke Leto thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #8
    Here's the code now...

    There is a menu and a menucell class, the menu positions the menu cell and handles events, and the menu cell draws the text.

    Code:
    void populateMenu()
    {
    	char line[10];
    	std::fstream menuFile;
    	// opens file
    	menuFile.open("menu.txt", std::ifstream::in);
    	
    	// creates a new menu with that frame
    	Menu *mainMenu = new Menu();
    	mainMenu->setFrame(makeRect(400.0f, 250.0f, 200.0f, 400.0f));
    
    	int i = 0;
    	while(menuFile.good())
    	{
    		menuFile.getline(line, 10);
    		char* ptr = line;
    		// creates new cell of height 30 and spacing 20 with the text read
    		MenuCell* newCell = new MenuCell(ptr, 30.0f, 20.0f);	
    		// adds the cell
    		mainMenu->addItem(newCell);
    	}
    	// add menu to linked list containing menus
    	menus->addObject(mainMenu);
    	menuFile.close();
    }
    
    menus is a linked list with menus, right now there is only one.

    By the time the display function get called, though, the text is gone.

    EDIT::
    I found something. ptr will always point to the same address, no matter what. I guess that is supposed to happen. I tried using
    Code:
    		char newChar[15];
    		int i=0;
    		while(i<15 && &ptr[i] != "\0")
    		{
    			newChar[i] = ptr[i++];
    		}		
    
    to make a "deep copy", but that didn't work.
     
  9. ncl macrumors member

    Joined:
    Aug 16, 2008
    #9
    ptr always points to line and line is local to populateMenu. When populateMenu is over, line is destroyed. That is why your text is gone.
    Your test should be ptr != '\0'. The way you wrote it, you compare the address of the ith char in ptr with the address of the string "\0".

    Since your using C++, why don't you use std::string ?
     

Share This Page