CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    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);

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to add data by Using Function CreateFile(...)

    Add '\n' as a line break.
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to add data by Using Function CreateFile(...)

    Quote Originally Posted by mecheil.edwar View Post
    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
    Quote Originally Posted by mecheil.edwar
    Code:
    int a = sizeof(TEXT("x"));
    And, please, read documentation for WriteFile API
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    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

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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;
    }
    Best regards,
    Igor

  7. #7
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    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
  •  





Click Here to Expand Forum to Full Width

Featured