Thread: Whats wrong with my Writefile?

  1. #1
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80

    Whats wrong with my Writefile?

    Code:
    	HANDLE File;
    	DWORD bytesWritten;
    	HWND hEdit;
    	int Length;
    	File = CreateFile("c:/testing.txt",GENERIC_WRITE,FILE_SHARE_READ, NULL,
    		              CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    	hEdit = GetDlgItem(hwnd,ID_MAINEDIT);
    	Length = GetWindowTextLength(hEdit);
    
    	char *Buffer = new char[Length+1];
    	WriteFile(File,Buffer,Length,&bytesWritten,NULL);
    	CloseHandle(File);
    	delete [] Buffer;
    Guys can someone tell me why did the output of my writefile is like this ÍÍÍÍ, looks like those in Character Maps. Can someone advice ?
    /* Have a nice day */

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Because there's nothing in Buffer except random garbage - try putting some meaningful data in there like, for example, with GetWindowText as it seems you may have been intending to do.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    OH, no wonder, i guess i missed the GetDlgItemText() ! damn, THANZ
    /* Have a nice day */

  4. #4
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    Ken, do you know of any functions for checking the directory my program will be in ? so that i can correctly save the file in. kinda like C:/program files/retard/ < - my program folder.
    /* Have a nice day */

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    If you are creating a distributable program you should not save files in your program directory, as it will be read-only after install. Typically, you should save in either My Documents (for user documents) or Application Data/Your App (for settings files, logs, etc). You can get these paths by calling SHGetSpecialFolderPath with CSIDL_APPDATA and CSIDL_PERSONAL respectively.

    That being said, you can get the path of your exe with the following code:
    Code:
    TCHAR szPath[MAX_PATH];
    
    GetModuleFileName(NULL, szPath, MAX_PATH);
    PathRemoveFileSpec(szPath);
    PathAddBackslash(szPath);

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    or with getCurrentDirectory() or GetWorkingDirectory()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    Great, thanz, i should be able to get the working path now. One more problem guys about the WriteFile(); I have 2 editbox - ID_TITLE and ID_MAIN. Its the Title box and the main details box. The problem is when i write the info to my file, theres no /n or paragraphs. Is it possible to add a paragraphing thru WriteFile() ? i tried using strcat() and add \n to the buffer but it didnt work, alien boxes came out instead.
    /* Have a nice day */

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    A newline in Windows is represented by "\r\n". When you use the c file functions in text mode, "\n" is automatically converted to "\r\n" on output and "\r\n" is converted to "\n" on input. WriteFile, on the other hand, treats its buffer as binary data and no conversion is performed. Therefore, you will have to use "\r\n" explicitly.

  9. #9
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    Any advice on how i could achieve it ? im trying to seperate my ID_TITLE and ID_MAIN text infos from each other, everithing is lined up together and its ugly.
    /* Have a nice day */

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Just like normal, but with "\r\n" instead of "\n".
    Code:
    strcat(buf, "\r\n");
    
    OR
    
    WriteFile(File,"\r\n",2,&bytesWritten,NULL);
    
    OR
    
    sprintf(buf, "%s\r\n%s", title_str, main_str);

  11. #11
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80
    anonytmouse, i tried the following but none seems to work perfectly, can you tell me the problems?
    Code:
    	WriteFile(File,Title,Length2,&TitleWritten,NULL);
    	WriteFile(File,"\r\n",4,&TitleWritten,NULL);
    	WriteFile(File,Buffer,Length,&bytesWritten,NULL);
    output for this is
    Code:
    testing
      ojojoj
    theres a spacing in front of the second info
    Code:
    	strcat(Title,"\r\n");
    	Length2 += 4;
    
    	WriteFile(File,Title,Length2,&TitleWritten,NULL);
    	WriteFile(File,Buffer,Length,&bytesWritten,NULL);
    Output is
    Code:
    Title
     ýTesting
    The newline command definitely works but both with funny results, can someone explain ?
    /* Have a nice day */

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > Length2 += 4;
    Length2 += 2;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  2. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  3. Is there anything wrong with doing this?
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-21-2002, 11:30 AM
  4. WriteFile help (Win32).
    By Sindolist in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2002, 04:23 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM