Hey David Thanks for the quick reply.. i ended up getting the it sorted in the end. you were right the problem was that they were in the same function. i was trying to use outfile for both input and output to the text file and it didnt like that one bit!. once i put thme in separate functions it worked perfect i dont know what std::string? ill check it out later The int score was me being lazy and leaving out the i :) i fixed that too and once angain dont know what std::vector is but will look into it later once i get the coffee refil i also changed the fstream to ifstream and ostream. i presume using them makes it more efficent or why is it better the using fstream? thanks again for the help i was going mad here
>From: David <[EMAIL PROTECTED]> >Reply-To: [email protected] >To: [email protected] >Subject: Re: [c-prog] Writing to text files >Date: Sun, 04 Mar 2007 22:43:11 +0100 > >glogic_1 a écrit : > > Hey all > > >Hi, I am tired so, I excuse if I am a bit "hard" ;) >I have some questions about your code. > > > Im having a problem writing to text files. I am able to read from > > them no problem but just cant write to it. > > > > struct sScore { > > char cName[40]; > > >why did not you use a std::string ? > > > int score; > > >Since, you prefix your struct (sScore) and the cName, why did not you >prefix score with i (iScore) ? > > }TopTen[SCORES]; > > > > > > > >a std::vector could have been a good choice. > > > fstream OutFile; > > > >I would have preffered an ifstream to read and a ofstream to write >(inside a separate function) > > OutFile.open("HighScore.txt",ios::in); > > if (OutFile){ > > while (!OutFile.eof()) > > { > > OutFile >> TopTen[iNumber].cName; > > OutFile >> TopTen[iNumber].score; > > iNumber++; > > } > > } > > >Usually, you did not check a read error on end of file. >while( OutFile >> TopTen[iNumber].cName >> TopTen[iNumber].score ) > ++iNumber; > >but probably an operator << on sScore could have been welcome. >and take care of "space" in your name entries. > > > > > > The above code works fine for taking in player name and score no > > prob but the below code, when im sending an amended topten back out > > to the txt, just clears the txt file completely > > > > OutFile.open("HighScore.txt",ios::out); > > > >OutFile.close() ? > > if (OutFile) > > { > > for (int i = 0; i < (iNumNames+1);i++) > > { > > >what is iNumNames ? >Your loop seems strange, if iNumNames is 2, I expect to have 2 index in >TopTen so i =0, 1. >This is not the case here, i = 0, 1, 2 > > OutFile << TopTen[i].cName << " " << TopTen[i].score << endl; > > > > } > > } > > OutFile.close(); > > > > Now any help would be appreciated as im only starting out on C++ and > > am completely lost and confusd at this moment in time > > cheers > > g > > > > > > >Regards, >David > _________________________________________________________________ Mortgage rates as low as 4.625% - Refinance $150,000 loan for $579 a month. Intro*Terms https://www2.nextag.com/goto.jsp?product=100000035&url=%2fst.jsp&tm=y&search=mortgage_text_links_88_h27f6&disc=y&vers=743&s=4056&p=5117 ------------------------ Yahoo! Groups Sponsor --------------------~--> Something is new at Yahoo! Groups. Check out the enhanced email design. http://us.click.yahoo.com/kOt0.A/gOaOAA/yQLSAA/EbFolB/TM --------------------------------------------------------------------~-> To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>. Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/c-prog/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/c-prog/join (Yahoo! ID required) <*> To change settings via email: mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
