宽字符转单字符wchar_t转char,中文不乱码的函数
inline char *UnicodeToAnsi(const wchar_t *szStr) {
int nLen = WideCharToMultiByte(CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL);
if (nLen == 0) {
return NULL;
}
char *pResult = (char *)malloc(nLen);
WideCharToMultiByte(CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL);
return pResult;
}
字符串截取
/*从字符串的左边截取n个字符*/
char *left(char *dst, char *src, int n) {
char *p = src;
char *q = dst;
int len = strlen(src);
if (n > len)
n = len;
/*p += (len-n);*/ /*从右边第n个字符开始*/
while (n--)
*(q++) = *(p++);
*(q++) = '\0'; /*有必要吗?很有必要*/
return dst;
}
/*从字符串的中间截取n个字符*/
char *mid(char *dst, char *src, int n, int m) /*n为长度,m为位置*/
{
char *p = src;
char *q = dst;
int len = strlen(src);
if (n > len)
n = len - m; /*从第m个到最后*/
if (m < 0)
m = 0; /*从第一个开始*/
if (m > len)
return NULL;
p += m;
while (n--)
*(q++) = *(p++);
*(q++) = '\0'; /*有必要吗?很有必要*/
return dst;
}
/*从字符串的右边截取n个字符*/
char *right(char *dst, char *src, int n) {
char *p = src;
char *q = dst;
int len = strlen(src);
if (n > len)
n = len;
p += (len - n); /*从右边第n个字符开始*/
while (*(q++) = *(p++))
;
return dst;
}
判断文件行数
FILE *fp = fopen("1.txt", "r");
if (fp == NULL)
return -1; //表示文件打开错误
char c;
while ((c = fgetc(fp)) != EOF) {
if (c == '\n')
linenum++;
}
linenum++;
fclose(fp);
回到文件头
fseek(fp,0,SEEK_SET);
网友评论