none
HOW TO ADD A NEW LINE IN A TEXT FILE ??? RRS feed

  • Question

  •       I need to write in a .txt file a new line and I don`t know exactly how to do this.

          I tried CreateFile() with WriteFile() method . I tried to read the content of the file first  into a buffer and then add the new line with "strcat(contentOfFile,newLine)"  but didn`t worked :(

         I don`t know how to specify \n character when writing into the file.

     

        Here is my code


        char *sirFinal;
        char *strInter;
        char *strNewline;



          TCHAR   szFile2[MAX_PATH] = TEXT("\\My Storage\\poi2.txt\0");
        char    *szFileContent2 = NULL;

        //char    *randul = NULL;
       // OPENFILENAME   ofn;

        HANDLE hFile2 = INVALID_HANDLE_VALUE;
        DWORD dwFileSize2 = 0, bytesToRead2 = 0, bytesRead2 = 0;
      
      
            //ofn.lpstrFile contains the full path to the file, get a handle to it
            hFile2= CreateFile(szFile2, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

            if (hFile2 == INVALID_HANDLE_VALUE)
                return FALSE;

            //get file size in bytes
            dwFileSize2 = GetFileSize(hFile2, NULL);

            szFileContent2 = (char*)malloc(dwFileSize2+1);
          
            memset(szFileContent2, 0, dwFileSize2+1);

            //read the files contents into a buffer
            if (ReadFile(hFile2, szFileContent2, dwFileSize2, &bytesRead2, NULL))
            {


            }
          TCHAR   szFile[MAX_PATH] = TEXT("\\My Storage\\poi2.txt\0");
        

           HANDLE hFile = INVALID_HANDLE_VALUE;
         

            HWND hEdit = NULL;
            DWORD dwTextLen = 0, bytesWritten = 0;
            TCHAR *wszEditText = NULL;
            //TCHAR *wszEditInter = NULL;
            WCHAR wszEditInter[30];
            char *szEditText = NULL;

           
            CloseHandle(hFile2);

            //ofn.lpstrFile contains the full path of the file, get a handle to it
            hFile = CreateFile(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

            // hFile = CreateFile(szFile2, GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

            if (hFile == INVALID_HANDLE_VALUE)
                return FALSE;

            hEdit = GetDlgItem(hDlg, IDC_EDIT1); //IDC_EDIT1

            //get the text length of the edit controls contents
            dwTextLen = GetWindowTextLength(hEdit);

            wszEditText = (TCHAR*)malloc((dwTextLen+1)*sizeof(TCHAR));

            memset(wszEditText, 0, (dwTextLen+1)*sizeof(TCHAR));

            //read edit controls contents into buffer
            GetWindowText(hEdit, wszEditText, dwTextLen+1);


            szEditText = (char*)malloc(dwTextLen+1);
          
            //convert the wide char read from edit control to char
           wcstombs(szEditText, wszEditText, dwTextLen);
       
                   
            sirFinal=strcat(szFileContent2,strNewline);
         
            bytesWritten=(DWORD)strlen(sirFinal);

            if (WriteFile(hFile, strNewline, strlen(strNewline), &bytesWritten, NULL))
            {}
            //free resources
            free(wszEditText);
            free(szEditText);
            CloseHandle(hFile);
           
       // }
      
        return TRUE;

          I would be very happy if someone could help me:)

        Andrei_DR

     

    Sunday, June 6, 2010 6:37 AM

Answers

  • Hi,

    Windows use "\r\n" for newline, please use it instead of "\n". Below is a sample, it works fine in my local.

    if (hFile != INVALID_HANDLE_VALUE)

                                            {

     

                                                    hFile = CreateFile (TEXT("\\storage card\\1.txt"), GENERIC_WRITE,0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

                                                    DWORD dwLen, dwWritten;

                                                    LPCTSTR m_Firstname=L"hello again",m_LastName=L" world";

                                                   

     

                                                    char szBuffer[1024] = "\0";

     

                                                    wchar_t szTmp[1024] = L"\0";

     

                                                    wsprintf(szTmp,L"%s\r\n, %s",m_Firstname,m_LastName);

     

                                                    wcstombs(szBuffer,szTmp,wcslen(szTmp)+1);

     

                                                    dwLen = strlen(szBuffer);

     

                                                    WriteFile(hFile,szBuffer,dwLen,&dwWritten,NULL);

     

                                                    CloseHandle(hFile);

                                            }

     

    If you still have question, please let me know.

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Tuesday, June 8, 2010 7:44 AM
    Moderator