Linux上wfopen(打开宽字符版的文件名和模式)的实现

Linux上没有直接可用的wfopen函数用来打开宽字符的文件名,那我们可以将宽字符转换为char型,再调用fopen函数。

#include 
#include 
#include 

#define MAX_PATH 1024

FILE* wfopen(const wchar_t* filename, const wchar_t* mode)
{
    char fn[MAX_PATH];
    char m[MAX_PATH];
    wcstombs(fn, filename, MAX_PATH);
    wcstombs(m, mode, MAX_PATH);
    return fopen(fn, m);
}

#define SIZE 4

struct person
{
    char name[10];
    int year;
    int month;
    int day;
}personArray[SIZE];


int main()
{
    wchar_t *filename = L"/Perforce/sandbox/1.txt";
    wchar_t *mode = L"r";
    
    FILE* pFile = wfopen(filename, mode);
    if(pFile)
        printf("File open successeed!\n");
    else
        printf("File open failed!\n");
        
    /* int i;
    for(i = 0; i < SIZE; i++)
    {
        if(fread(&personArray[0], sizeof(struct person), i, pFile) != 1)
        {
            printf("File read failed at fread!\n");
        }
    }
        
    fclose(pFile);
      
      
      for( i = 0; i < SIZE; i++)
      {
        printf("%s, %d, %d, %d", personArray[i].name, personArray[i].year, personArray[i].month, 
                                 personArray[i].day);
        printf(" ");
      }
      
    printf("\n");*/
          
    /* use the second test case to test*/
    int c;
    while( (c = fgetc(pFile)) != EOF)
    {
        printf("%c", c);
    }
   fclose(pFile);
    
    return 0;
}


你可能感兴趣的:(C/C++,Linux)