h71y6 Posted January 17, 2002 Share Posted January 17, 2002 //******************************************* //******************************************* //I made this thread so people with C/C++ questions can //ask from instead of making a new thread for every //question. This way, a database of questions and //answers can be compiled. Feel free to ask. //******************************************* //******************************************* Link to comment Share on other sites More sharing options...
0 neowin_hipster Posted January 18, 2002 Share Posted January 18, 2002 How can I use a random-access binary file. Link to comment Share on other sites More sharing options...
0 h71y6 Posted January 18, 2002 Author Share Posted January 18, 2002 Originally posted by Goalie_CA How can I use a random-access binary file. You need to include Link to comment Share on other sites More sharing options...
0 Flip_Kid Posted January 18, 2002 Share Posted January 18, 2002 Sample code (MSVC++): //Binary files. #include #include void main (void) { fstream yourfile; yourfile.open("c:whatever.txt",ios::in | ios::out); // Remember //to use drive:directory <-- so it doesn't think you're using //another escape character if (yourfile.fail()) { cout << "error."; } else { yourfile.seekp(0,ios::end); //Output pointer at end of file yourfile << "Text at end of file ! :)nUsed like cout."; //If you need more help, post here. //for input, use seekg to position the input pointer and get or //>> to get data. //hope this helps! } } //--EOF Can anyone please post tutorials for windows programming and/or beginning mfc? Thanks! :) Link to comment Share on other sites More sharing options...
0 Vlad Posted January 18, 2002 Share Posted January 18, 2002 I could be misunderstanding this, but didn't he ask for "binary" not "ascii"? *shrug* That's a nice example though, Flip_kid Link to comment Share on other sites More sharing options...
0 Flip_Kid Posted January 19, 2002 Share Posted January 19, 2002 aregh.... you're right LOL just saw the random access part and thought - hey i can do this LOL so ... anyone have any mfc/win32/anything windows tutorials i could use? I can make message boxes! ;) Link to comment Share on other sites More sharing options...
0 freestyle Posted January 19, 2002 Share Posted January 19, 2002 If you want to use MFC for files, you could use the likes of CFile (for general files) or CStdioFile (mostly for text). eg. <pre> CFile fileTemp; try { CString sSomeData = _T("here is some datarn"); long lValue = 123456; fileTemp.Open(_T("Test 1.dat"), CFile::modeRead | CFile::modeWrite); //Seek to End - Write a String fileTemp.SeekToEnd(); fileTemp.Write(sSomeData, sSomeData.GetLength()); //Seek to End - Write a long fileTemp.SeekToEnd(); fileTemp.Write(lValue, sizeof(long)); fileTemp.Close(); } catch (CFileException *ex) { ex->Delete(); } </pre> You can also use these classess for Internet files... eg. <pre> CString sUrl = _T("somesite.html"); CInternetSession session; CStdioFile* pfileTemp = NULL; try { pfileTemp = session.OpenURL(sUrl, 0, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_DONT_CACHE); CString sDataLine; while(1) { if (pfileTemp->ReadString(sDataLine) == FALSE) break; AfxMessageBox(sDataLine); } } catch(CInternetException* ex) { ex->Delete(); } if (pFileTemp != NULL) delete pfileTemp; </pre> Hope this helps somewhat... and even works... Link to comment Share on other sites More sharing options...
Question
h71y6
//*******************************************
//*******************************************
//I made this thread so people with C/C++ questions can //ask from instead of making a new thread for every //question. This way, a database of questions and //answers can be compiled. Feel free to ask.
//*******************************************
//*******************************************
Link to comment
Share on other sites
6 answers to this question
Recommended Posts