File Reading and storing to 1 variable

This is a discussion on File Reading and storing to 1 variable within the C Programming forums, part of the General Programming Boards category; ok still no luck with this working. i changed it from int to long and its still showing the same ...

  1. #31
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    ok still no luck with this working.
    i changed it from int to long and its still showing the same result

  2. #32
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I'm not sure what is wrong with your code, you'd have to post more of it. If you are interested, here is a set of functions that saves and loads files in UTF8 encoding(the defacto standard for international text).

    Sample usage:
    Code:
    /* Save a UTF8 file. */
    WriteUTF8File(TEXT("FileName.txt"), TEXT("This is a test ιλμ"));
    
    /* Load a UTF8 file into an edit control. */
    TCHAR* szTest = ReadUTF8File(TEXT("FileName.txt"));
    SetWindowText(hwndEdit, szTest);
    free(szTest);
    Code:
    Code:
    /** Header:
    
    #include <windows.h>
    wchar_t* MBStr2WStr(LPCSTR strIn, UINT codePage);
    char* WStr2MBStr(LPCWSTR strIn, UINT codePage);
    char* MBStr2MBStr(LPCSTR strIn, UINT codepageIN, UINT codepageOUT);
    BOOL WriteUTF8File(LPCTSTR szFileName, LPCTSTR szText);
    TCHAR* ReadUTF8File(LPCTSTR szFileName);
    
    **/
    
    
    #include <windows.h>
    #include <malloc.h>
    
    /* 
     * Converts a unciode string to a multi-byte string with the specified code
     * page(CP_ACP for the default). Returned string must be released with free().
     */
    char* WStr2MBStr(LPCWSTR strIn, UINT codePage)
    {
    	int   cchSize;
    	char* strOut;
    
    	if (!strIn) return NULL;
    
    	cchSize = WideCharToMultiByte(codePage, 0, strIn, -1, 0, 0, NULL, NULL);
    	if (0 == cchSize) return NULL;
    
    	strOut = (char*) malloc(cchSize * sizeof(char));
    	if (NULL == strOut) return NULL;
    
    	if (0 == WideCharToMultiByte(codePage, 0, strIn, -1, strOut, cchSize, NULL, NULL))
    	{
    		free(strOut);
    		return NULL;
    	}
    
    	return strOut;
    }
    
    
    /* 
     * Converts a multi-byte string with the specified code page(CP_ACP for the default)
     * to a unicode string. Returned string must be released with free().
     */
    wchar_t* MBStr2WStr(LPCSTR strIn, UINT codePage) 
    {
    	int cchSize;
    	wchar_t* strOut;
    
    	if (!strIn) return NULL;
    
    	cchSize = MultiByteToWideChar(codePage, 0, strIn, -1, 0, 0);
    	if (0 == cchSize) return NULL;
    
    	strOut = (wchar_t*) malloc(cchSize * sizeof(wchar_t));
    	if (NULL == strOut) return NULL;
    
    	if (0 == MultiByteToWideChar(codePage, 0, strIn, -1, strOut, cchSize))
    	{
    		free(strOut);
    		return NULL;
    	}
    
    	return strOut;
    }
    
    
    /* 
     * Converts a multi-byte string with the specified input code page to a
     * multi-byte string with the specified output code page. 
     * Returned string must be released with free().
     */
    char* MBStr2MBStr(LPCSTR strIn, UINT codepageIN, UINT codepageOUT)
    {
    	char*    strOut      = NULL;
    	wchar_t* strTextWide = MBStr2WStr(strIn, codepageIN);
    
    	if (strTextWide)
    	{
    		strOut = WStr2MBStr(strTextWide, codepageOUT);
    		free(strTextWide);
    	}
    
    	return strOut;
    }
    
    
    /*
     * Write out text to a file in UTF8 format.
     */
    BOOL WriteUTF8File(LPCTSTR szFileName, LPCTSTR szText)
    {
    	HANDLE hFile;
    	char*  szTextUTF8   = NULL;
    	BOOL   bRet         = FALSE;
    	DWORD  dwWritten;
    
    	/* Open file. */
    	hFile = CreateFile(szFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL,
    	                   CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    	if (INVALID_HANDLE_VALUE == hFile) return FALSE;
    
    	/* Convert text into UTF8. */
    #ifdef UNICODE
    	szTextUTF8 = WStr2MBStr(szText, CP_UTF8);
    #else
    	szTextUTF8 = MBStr2MBStr(szText, CP_ACP, CP_UTF8);
    #endif
    
    	/* Write UTF8 text to file. */
    	if (NULL != szTextUTF8)
    	{
    		bRet = WriteFile(hFile, szTextUTF8, lstrlenA(szTextUTF8), &dwWritten, NULL);
    	}
    
    	/* Cleanup. */
    	free(szTextUTF8);
    	CloseHandle(hFile);
    	return bRet;
    }
    
    
    /*
     * Read text from a file in UTF8 format. Returned text must be released with free().
     */
    TCHAR* ReadUTF8File(LPCTSTR szFileName)
    {
    	HANDLE hFile;
    	char*  szTextUTF8;
    	DWORD  dwFileSize;
    	DWORD  dwRead       = 0;
    	TCHAR* szTextOut    = NULL;
    
    	/* Open file. */
    	hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
    	                   OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    	if (INVALID_HANDLE_VALUE == hFile) return NULL;
    
    	/* Get file size. */
    	dwFileSize = GetFileSize(hFile, NULL);
    	if (INVALID_FILE_SIZE == dwFileSize) 
    	{
    		CloseHandle(hFile);
    		return NULL;
    	}
    
    	/* Allocate memory for buffer to read in UTF8 text. */
    	szTextUTF8 = malloc(dwFileSize + 1);
    	if (NULL == szTextUTF8)
    	{
    		CloseHandle(hFile);
    		return NULL;
    	}
    
    	/* Read text in UTF8 format. */
    	if (!ReadFile(hFile, szTextUTF8, dwFileSize, &dwRead, NULL))
    	{
    		free(szTextUTF8);
    		CloseHandle(hFile);
    		return NULL;
    	}
    
    	/* Terminate buffer. */
    	szTextUTF8[dwRead] = '\0';
    
    	/* Convert UTF8 text into TSTR. */
    #ifdef UNICODE
    	szTextOut = MBStr2WStr(szTextUTF8, CP_UTF8);
    #else
    	szTextOut = MBStr2MBStr(szTextUTF8, CP_UTF8, CP_ACP);
    #endif
    
    	/* Cleanup. */
    	free(szTextUTF8);
    	CloseHandle(hFile);
    	return szTextOut;
    }

  3. #33
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    theres a few undefined objects in that like

    CP_UTF8
    INVALID_FILE_SIZE

    any idea why that is?

  4. #34
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Because your header files are out of date. Just paste them in:

    Code:
    #define CP_UTF8                   65001       // UTF-8 translation
    #define INVALID_FILE_SIZE ((DWORD)0xFFFFFFFF)

  5. #35
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    thanks you very much it works.and every one else who helped.much appreciated

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM
  3. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  4. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21