For some reason, unless you are going to Alexandria or taking the overnight to Luxor or Aswan, foreigners in Cairo can not buy train tickets at the window. And, if going to Alexandria, you will not be able to buy a ticket at the window for anything less than II class with AC. (These rules are not always enforced, and one with good enough Arabic might be able to get past ’em, especially if you buy your ticket the day before. Also, asking an Egyptian to get you a ticket works, too, like it did for us when we went to Tanta.)
More likely than not, though, is that you will be sent from ticket window to ticket window on opposite ends of the train station, and then told that you must buy a ticket on the train. This isn’t the worse thing; at best you’ll get stuck playing musical chairs after every stop and at worse you’ll have to stand the entire time. And though foreigners aren’t encouraged to take the II and III class trains, you can always get on.
Getting back to Cairo, however, is much easier. You can buy tickets to Cairo from any station, for any class, without any hassle (unless you are trying to get from Sohag to Cairo the Thursday afternoon before Presidential Elections, like we were). Once, we paid a whopping 2.5 LE for a 3 hour, III class train from Suez to Cairo. The windows were broken, the seats were broken, the doors wouldn’t shut, and by the time we got back we were nearly frozen. But, it was great!