Q. Is someone had some food in his pockets and entered a cemetery, can he eat the food after he goes out? Is there an issue of ruach ra (bad spirit) involved?

A. Shulchan Aruch (Y.D. 368: ) rules that one should honor, behave respectfully and not eat while visiting a cemetery. It would seem that the food brought therein does not become prohibited. So it would appear from Talmud Eiruvin (31a) that for Kohanim there is an issue of placing the food eruv on a kever, apparently for others it does not matter. Vayilaket Yosef (132) quotes from sidur Nahar Sholom a minhag to give out on Erev Yom Kippur sweet foods in the Bais Olam.
However, Eliahu Rabbah (O.H. 224) quoted by Hagaos R.A.Eiger (y.D. 376), explains that one of the reasons of not taking out food from an avel’s home during the shiva days, is due to the ruach-ra that permeates the site. (See question 11)
Nevertheless, Chaye Adam (2: 2, in regards to food touched before washing hands after a night-sleep), Nishmas Yisroel (p. 911), Vaylaket Yosef (ibid.), Shevet Hakehosi (4: 288) and others, permit after the fact the food that entered a cemetery.
Horav Shlomo miller’s Shlit’a opinion is similar.
In regards to food that is maintained in a container that has a free space of a tefach (about 10 cm.) above the food, the Rov maintains that it is preferable. The Rov added that even on the onset it is permitted to bring food maintained inside a car entering a cemetery, as it is considered a separate domain. (See question 613 in regards to one sitting in a car and standing for an elderly person walking by).

Rabbi A. Bartfeld as revised by Horav Shlomo Miller Shlit’a