| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 515 人关注过本帖
标题:关于文字写入txt文档时乱码的问题
只看楼主 加入收藏
Joke0519
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-3-16
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
关于文字写入txt文档时乱码的问题
代码如下:
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>

int word=0;// word 统计多少英语字符
char *creatEnglishArray()//创建字符数组将英文内容写入字符数组中
{
    FILE*fp;
    if((fp=fopen("a.txt","r+"))==NULL)
    {
        printf("Can't open the file\n");
        exit(0);
    }
    int sum=0,i=0;//sum统计共有多少个字符,包含空格等字符
//    int word=0;// word 统计多少英语字符
    while(fgetc(fp)!=EOF)
    sum++;
    char *array=new char[sum+1];//多创建一个字符空间来存储“\0”,以表示结尾
    fp=fopen("a.txt","r+");
    while(!feof(fp))
    {
        char p =fgetc(fp);
        if(( p>='a' && p <= 'z')||(p >='A' && p <= 'Z'))
        {
            array[i]=p;
            i++;
            word++;
        }
    }
    array[word]='\0';

    return array;//返回包含文件所有内容的数组
}

void English()
{    FILE*fp;
    if((fp=fopen("english.txt","w"))==NULL)
    {
        printf("Can't open the file\n");
        exit(0);
    }
    char *english = creatEnglishArray();
    for(int i=0;i<word+1;i++)
    {
        fprintf(fp,"%c ",*english+i);   
    }
    fclose(fp);
}
int Cnum=0;
char *creatChineseArray()
{
    FILE*fp;
    if((fp=fopen("a.txt","r+"))==NULL)
    {
        printf("Can't open the file\n");
        exit(0);
    }
    int sum=0,i=0;//sum统计共有多少个字符,包含空格等字符
//    int word=0;// word 统计多少英语字符
    while(fgetc(fp)!=EOF)
    sum++;
    char *array=new char[sum+1];//多创建一个字符空间来存储“\0”,以表示结尾
    fp=fopen("a.txt","r+");
    while(!feof(fp))
    {
        char p =fgetc(fp);
        if(p==' '||p==','||p=='.'||p=='\"'||p==':'||p=='!'||p=='\'')
        {
            continue;
        }         
        
        if( p<'a' || p >'z')
        {
            array[i]=p;
            i++;
            word++;
            Cnum++;
        }
    }
    array[word]='\0';

    return array;//返回包含文件所有内容的数组
}

void Chinese()
{
    FILE*fp;
    if((fp=fopen("chinese.txt","wb"))==NULL)
    {
        printf("Can't open the file\n");
        exit(1);
    }
    char *chinese = creatChineseArray();
    for(int i=0;i<word;i++)
    {
        fprintf(fp,"%c ",*(chinese+i));   
    }
    fclose(fp);
    int count=(Cnum-1)/2;
    printf("汉字的个数:%d\n",count);
}

int main()
{
    English();
    Chinese();
    return 0;
}
但是文件fprintf写入时需要进行转码,但是不是很会,有没有大神指导一下
搜索更多相关主题的帖子: include 英语 英文 file 统计 
2015-03-16 15:44
大爱CATs
Rank: 4
来 自:重庆
等 级:业余侠客
威 望:3
帖 子:108
专家分:250
注 册:2015-1-13
收藏
得分:10 

弹指一挥间
2015-03-16 15:54
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:10 
又英文又中文的,不知是什麽意思。

授人以渔,不授人以鱼。
2015-03-16 15:59
快速回复:关于文字写入txt文档时乱码的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015447 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved