| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 601 人关注过本帖
标题:编程题 文件字符处理请赐教
只看楼主 加入收藏
spawn0517
Rank: 1
等 级:新手上路
帖 子:5
专家分:1
注 册:2010-3-24
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:3 
编程题 文件字符处理请赐教
从D盘读取文件,文件中按文本方式存放了一段其他文章,其中有若干长度小于15的十进制或八进制数字,数字之间用“,”分开,数字内部存在且仅存在空格。
(3)八进制数以起始位“0”作为标示与十进制数区分。
(4)顺序读取这些数字将他们转变为十进制数后按从大到小的顺序排序后,输出到D盘根目录下new.txt,每个数字一行。
   eg:_235_,34__2,_043_1_,1_3   分别是:十进制235,十进制342.,八进制431,十进制13。_代表空格
这里的空格及每个字符段的读取怎么控制,请大虾帮帮忙!!
搜索更多相关主题的帖子: 文件 字符 
2010-03-24 13:50
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:20 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define FILENAME "readfile.txt" //自己在这里写文件路径
#define NEWFILE "new.txt" //自己在这里写文件路径

int Procstr(char *str, int len)
{
    int i, num = 0;
    if (*str == '0')
    {
        for (i = 1; i < len; i ++)
        {
            num += (str[i]-'0')*pow(8, len-1-i);
        }
    }else
    {
        for (i = 0; i < len; i ++)
        {
            num += (str[i]-'0')*pow(10, len-1-i);
        }
    }
    return num;
}
void Qsort(int *ary, int n)
{
    int i, j;
    for (i = 0; i < n; i ++)
    {
        for (j = i+1; j < n; j ++)
        {
            if (ary[i] < ary[j])                                                                                 
            {                                                                                                     
                ary[i] = ary[i] + ary[j];                                                                        
                ary[j] = ary[i] - ary[j];                                                                        
                ary[i] = ary[i] - ary[j];                                                                        
            }                                                                                                     
        }                                                                                                         
    }                                                                                                            
}
int *ReadFile(int *n)
{
    FILE *fp;
    char buf[16], c;
    int len = 0, cnt = 0;
    int *ary = NULL;
    if ((fp = fopen(FILENAME, "r")) == NULL)                                                                     
    {                                                                                                            
        printf("fopen %s error !\n", FILENAME);                                                                  
        exit(1);                                                                                                  
    }                                                                                                            
    while (1)                                                                                                     
    {                                                                                                            
        c = fgetc(fp);                                                                                            
        if (c == EOF)                                                                                             
        {                                                                                                         
            if (len != 0)                                                                                         
            {                                                                                                     
                ary = (int *)realloc(ary, sizeof(int)*(++cnt));                                                   
                ary[cnt-1] = Procstr(buf, len);                                                                  
                len = 0;                                                                                          
                printf("%d ", ary[cnt-1]);                                                                        
            }                                                                                                     
            break;                                                                                                
        }
        else if (c >= '0' && c <= '9')                                                                           
        {                                                                                                         
            if (len == 15)                                                                                       
            {                                                                                                     
                printf("data len override!\n");                                                                  
                if (ary) free(ary);                                                                              
                fclose(fp);                                                                                       
                exit(1);                                                                                          
            }                                                                                                     
            buf[len ++] = c;                                                                                      
        }else if (c == ',')                                                                                       
        {                                                                                                         
            ary = (int *)realloc(ary, sizeof(int)*(++cnt));                                                      
            ary[cnt-1] = Procstr(buf, len);                                                                       
            len = 0;                                                                                             
            printf("%d ", ary[cnt-1]);                                                                           
        }                                                                                                         
    }                                                                                                            
    printf("\n");                                                                                                
    fclose(fp);                                                                                                   
    *n = cnt;                                                                                                     
    return ary;                                                                                                   
}      
void WriteFile(int *a, int n)                                                                                    
{                                                                                                                 
    FILE *fp;                                                                                                     
    int i;                                                                                                        
    if ((fp = fopen(NEWFILE, "w")) == NULL)                                                                       
    {                                                                                                            
        printf("fopen %s error !\n", NEWFILE);                                                                    
        free(a);                                                                                                  
        exit(1);                                                                                                  
    }                                                                                                            
    for (i = 0; i < n; i ++)                                                                                      
    {                                                                                                            
        fprintf(fp, "%d ", a[i]);                                                                                 
    }                                                                                                            
    fclose(fp);                                                                                                   
}                                                                                                                 
int main(void)                                                                                                   
{                                                                                                                 
    int *ary = NULL, cnt=0;                                                                                         
    ary = ReadFile(&cnt);                                                                                         
    Qsort(ary, cnt);                                                                                             
    WriteFile(ary, cnt);                                                                                          
    free(ary);                                                                                                   
}               
2010-03-24 17:47
dayingyushi
Rank: 2
等 级:论坛游民
帖 子:23
专家分:14
注 册:2010-2-10
收藏
得分:0 
楼上的好强
2010-03-24 19:38
spawn0517
Rank: 1
等 级:新手上路
帖 子:5
专家分:1
注 册:2010-3-24
收藏
得分:0 
回复 2楼 ldg628
谢谢 大侠!!

[ 本帖最后由 spawn0517 于 2010-3-29 18:01 编辑 ]
2010-03-29 17:58
快速回复:编程题 文件字符处理请赐教
数据加载中...
 
   



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

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