如何解决fopen打开CSV文件输出时汉字乱码

C语言 码拜 9年前 (2015-07-10) 1660次浏览 0个评论
 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>


int main()
{
	char str[100] = "F:\视频\8.csv";
	FILE *pf = fopen(str, "r");
	if (pf == NULL)
	{
		return -1;
	}
	else
	{
		while (!feof(pf))
		{
			char str[500] = { 0 };
			fgets(str, 500, pf);
			fputs(str, stdout);
		}
	}

	getchar();
	return 0;
}

应该如何解决.
如何解决fopen打开CSV文件输出时汉字乱码

20分

应该是格式的问题 .csv文件的格式,要不就用unicode去读,或者是有一个下划线什么open的一个函数也可以 哈哈

20分

FILE *pf = fopen(str, “r, ccs=UTF-8“);

In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this:

fopen(“newfile.txt”, “rw, ccs=<encoding>”);

Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file.

Flag
No BOM (or new file)
BOM: UTF-8
BOM: UTF-16

UNICODE
ANSI
UTF-8
UTF-16LE

UTF-8
UTF-8
UTF-8
UTF-16LE

UTF-16LE
UTF-16LE
UTF-8
UTF-16LE

If mode is “a, ccs=<encoding>”, fopen will first try to open the file with both read and write access. If it succeeds, it will read the BOM to determine the encoding for this file; however, if it fails, it will use the default encoding for the file. In either case, fopen will then re-open the file with write-only access. (This applies to mode a only, not a+.)


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明如何解决fopen打开CSV文件输出时汉字乱码
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!