Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create and write to file (basic question)

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
How do you create and write to a file?

(I know, it's a basic question, but I'm a Java programmer who doesn't know C++ so well.) Liam Morley
lmorley@wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."
 
#include<fstream>
using namespace std;
int main()
{
ofstream x;
x.open(&quot;xxx.txt&quot;);
x<<&quot;hello&quot;;
x.close();
return o;
} John Fill
1c.bmp


ivfmd@mail.md
 
There are two other posibilities:

1.Using MFC support:


CFile file;
CFileException e;
char* pFileName = &quot;test.txt&quot;;
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
{
TRACE0(&quot;File could not be opened&quot;);
TRACE0(&quot;Exception: &quot; + e.m_cause);
}
2.Using SDK functions:

LPSTR lpszMessage = &quot;Sample&quot;;
BOOL fResult;
HANDLE hFile;
DWORD cbWritten;

hFile = CreateFile (TEXT(&quot;\\MYFILE.TXT&quot;),// Open
GENERIC_READ, // for reading
FILE_SHARE_READ, // Shared
NULL, // No security
CREATE_ALWAYS, // Existing file only
FILE_ATTRIBUTE_NORMAL, // Normal file
NULL); //No template

if (hFile == INVALID_HANDLE_VALUE)
{
// Your handling code goes here.
return;
}
fResult = WriteFile(hFile, lpszMessage,
(DWORD) lstrlen(lpszMessage) + 1, // include terminating null
&cbWritten,
(LPOVERLAPPED) NULL);
CloseHandle(hFile);

Hope this helps you,s-)
Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
thanks.. the first post was all I needed. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top