• 0

C/C++ Help Thread. Post Questions Here!


Question

//*******************************************

//*******************************************

//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

  • 0

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

  • 0

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

  • 0

If you want to use MFC for files, you could use the likes of CFile (for general files) or CStdioFile (mostly for text).

eg.

&lt;pre&gt;

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-&gt;Delete();

}

&lt;/pre&gt;

You can also use these classess for Internet files...

eg.

&lt;pre&gt;

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-&gt;ReadString(sDataLine) == FALSE)

 	 break;



  AfxMessageBox(sDataLine);

	}

}

catch(CInternetException* ex)

{

	ex-&gt;Delete();

}



if (pFileTemp != NULL)

	delete pfileTemp;

&lt;/pre&gt;

Hope this helps somewhat... and even works...

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.