Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Duke Leto

macrumors regular
Original poster
Mar 17, 2008
166
0
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
		}
	}
}
 
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/
 
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.
 
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.
 
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.
 
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?
 
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?

Return a pointer either to the data itself or to a structure holding the data.
 
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.
 
Here's the code now...
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.
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.
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.
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 ?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.