January 5th, 2013 12:27 PM
#1
How to add data by Using Function CreateFile(...)
I try to Create one text File By Using CreateFile(..) Function,
I want to add data ( x,y,x) coordinate to file By This way :
X Y Z
1 3 8
0 13 11
But when I write code I see Data Like this :
XYZ13801311,
I request any sample example to show me how to add and read data :
this is my code :
Code:
HANDLE hf; DWORD FileSize ;
hf = CreateFile(TEXT("AAA.txt"),GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,NULL,NULL);
FileSize = GetFileSize(hf,NULL);
int a = sizeof(TEXT("x"));
WriteFile(hf,TEXT("x"),a,&FileSize,NULL);
WriteFile(hf,TEXT("y"),a,&FileSize,NULL);
WriteFile(hf,TEXT("z"),a,&FileSize,NULL);
WriteFile(hf,TEXT("1"),a,&FileSize,NULL);
WriteFile(hf,TEXT("3"),a,&FileSize,NULL);
...........
............
CloseHandle(hf);
January 5th, 2013 01:21 PM
#2
Re: How to add data by Using Function CreateFile(...)
Add '\n' as a line break.
Victor Nijegorodov
January 5th, 2013 02:10 PM
#3
Re: How to add data by Using Function CreateFile(...)
Thanks Victor for your reply,
I wrote this code :
//~~~~~~~~~~~~~~~~~~~~~~~
WriteFile(hf,TEXT("3 \n"),a,&FileSize,NULL);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~
But unlucky it is not working ( I Build Win32 Application and Use Win7 64Bit OS)
I try to learn How can I find a format to help me how to add spaces between Data and How to add New Lines,
once again I thank you for your answer and helping.
January 5th, 2013 02:19 PM
#4
Re: How to add data by Using Function CreateFile(...)
Originally Posted by
mecheil.edwar
Thanks Victor for your reply,
I wrote this code :
//~~~~~~~~~~~~~~~~~~~~~~~
WriteFile(hf,TEXT("3 \n"),a,&FileSize,NULL);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And what is the value of a in this case? Hopefully, NOT the
Originally Posted by
mecheil.edwar
Code:
int a = sizeof(TEXT("x"));
And, please, read documentation for WriteFile API
Victor Nijegorodov
January 7th, 2013 07:34 PM
#5
Re: How to add data by Using Function CreateFile(...)
Thanks Victor ,
But I do many Trials But I could not write file as per required format..
Hoping I can get support..
My Best Reagrds
January 8th, 2013 12:56 PM
#6
Re: How to add data by Using Function CreateFile(...)
Code:
#include <windows.h>
#include <tchar.h>
int _tmain()
{
HANDLE hf;
hf = CreateFile(TEXT("AAA.txt"),GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,NULL,NULL);
DWORD writtenBytes = 0;
#ifdef UNICODE
WORD bom = 0xFEFF;
WriteFile(hf, &bom, sizeof(bom), &writtenBytes, NULL);
#endif
LPCTSTR line = TEXT("X Y Z\r\n");
WriteFile(hf, line, _tcslen(line) * sizeof(TCHAR), &writtenBytes, NULL);
line = TEXT("1 ");
WriteFile(hf, line, _tcslen(line) * sizeof(TCHAR), &writtenBytes, NULL);
line = TEXT("3 ");
WriteFile(hf, line, _tcslen(line) * sizeof(TCHAR), &writtenBytes, NULL);
line = TEXT("8\r\n");
WriteFile(hf, line, _tcslen(line) * sizeof(TCHAR), &writtenBytes, NULL);
CloseHandle(hf);
return 0;
}
January 8th, 2013 10:38 PM
#7
Re: How to add data by Using Function CreateFile(...)
How Can I thank You Igor , for your effective efforts and your perfect code...
Really I have to thank you very much.. for this valuable code..
It is working very well.. Excellent...
My Best Regards Igor..
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks