怎样用C语言实现删除一个文件中的重复的字符窜

wuhanlucsdn 2016-11-30 11:13:36
用C语言写个文字处理工具把以下文件里面每行中含有“drive”或“receive”或“detector”的所在行中双引号里面的那个NODE名字提取出来存到一个文件中.在下面已有的C程序基础上添加一些代码,怎么才能把存取的文件里面相同的NODE名字删掉,只保留一个.

文件:


!!!! 10 0 1 1460031171 0000
global
relay
wire "GND" to 21850 general
wire "RSP_IM_SENSE_L" to 21851 general
end relay


test analog "c723"

wire "LIU_TX_TIP_P1" to 21773 s
wire "LIU_TX_TIP_AC_P1" to 221148 i
end test

test analog "c724"

wire "LIU_TX_TIP_P3" to 221174 s
wire "LIU_TX_TIP_AC_P3" to 22254 i
end test

test analog "c725"

wire "LIU_TX_TIP_P6" to 222155 s alternate
wire "LIU_TX_TIP_AC_P6" to 22255 i
end test



test analog "t6%jp21"

wire "TX_RING_P42" to 221109 s
wire "TX_TIP_P42" to 220108 i
end test

test analog "t6%jp22"

wire "TX_RING_P41" to 219105 s
wire "TX_TIP_P41" to 22008 i
end test

test analog "t6%jp23"

wire "RX_RING_P41" to 216105 s
wire "RX_TIP_P41" to 21705 i alternate
end test


subtest "VP1P0"
wire "VP1P0" to 21513 detector high
wire "GND" to 21955 detector low
end subtest

subtest "VP0P75"
wire "VP0P75" to 210136 detector high
wire "GND" to 21064 detector low
end subtest

subtest "1V8_AD9554"
wire "1V8_AD9554" to 20813 detector high
wire "GND" to 21064 detector low
end subtest


test analog "u2333"
functional

subtest "Pin1_offset"
wire "CLK_19M44_TCXO_REF_P" to 20734 detector high
wire "GND" to 21064 detector low
end subtest

subtest "Pin1"
wire "CLK_19M44_TCXO_REF_P" to 20734 receive frequency
end subtest
end test

test analog "y3"
functional

wire "CLK_20M_TCXO" to 214134 receive frequency
end test



test digital "u1"
timing module 2
clock internal

wire "IM_I2C_INT_OUT_L" to 20955 drive
wire "IM_I2C_SCL" to 208137 drive
wire "IM_I2C_SDA" to 20837 drive
wire "QUACK_RESET_L" to 20834 drive
wire "RSP0_I2C_SCL" to 207149 drive
wire "RSP0_I2C_SDA" to 208148 drive
wire "RSP1_I2C_SCL" to 20747 drive
wire "RSP1_I2C_SDA" to 20744 drive
wire "RSP_IM_MUX_RST_L" to 20727 drive
wire "IM_I2C_SCL" to 208137 receive
wire "IM_I2C_SDA" to 20837 receive
wire "RSP0_I2C_INT_L" to 20753 receive
wire "RSP0_I2C_SDA" to 208148 receive
wire "RSP1_I2C_INT_L" to 20951 receive
wire "RSP1_I2C_SDA" to 20744 receive
end test

test digital "u3"
timing module 2
clock internal

wire "$12N395" to 21048 drive
wire "BOARD_I2C_SCL" to 21041 drive
wire "FPGA_SPI_CS_L" to 20947 drive
wire "FPGA_SPI_HOLD_L" to 20538 drive
wire "FPGA_SPI_MOSI" to 20841 drive
wire "FPGA_SPI_SCLK" to 20838 drive
wire "FPGA_SPI_WP_L" to 21127 drive
wire "FPGA_SPI_MISO_ST" to 20842 receive
end test



已有的C程序:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *fp1;
FILE *fp2;
fp1=fopen("src.txt","r");
fp2=fopen("生成.txt","w");
if(NULL==fp1)
{
printf("未发现原文件!!!\n");
exit(-1);
}
if(NULL==fp2)
{
printf("目标文件打开失败!!!\n");
exit(-1);
}
const char* Key[] = {"drive","receive","detector"};
char buf[1024] = {0};
char* p1 = NULL;
char* p2 = NULL;
int i;
while(!feof( fp1 ) )
{
memset(buf,0,sizeof(buf));
fgets(buf,sizeof(buf)-1,fp1);
for(i=0;i < sizeof(Key)/sizeof(char*);++i)
{
p1=strstr(buf,Key[i]);//对比关键字是否存在
if(NULL != p1)
{
p1 = strstr(buf,"\"");//查找第一个引号
if(NULL != p1)
{
p2 = strstr(++p1,"\"");
if(NULL != p2)
{
*p2 = '\0';
fprintf(fp2,"%s\n",p1);
}
}
}
}
}
fclose(fp1);
fclose(fp2);
return 0;
}
...全文
427 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
振翅高飞 2016-11-30
  • 打赏
  • 举报
回复
引用 2 楼 wuhanlucsdn 的回复:
是的,但是后面生成的文件里面有很多相同的内容,不知道怎么把重复的内容删掉?
你要是用纯C语言的话,在原来那个程序基础上,得自己创建一个数据结构(链表什么的)把每一个数据行单独摘出来存进这个数据结构,然后遍历这个数据结构,把相同的只生成一次写入到文本文件中。 具体的代码还要自己组织,我已经把方法告诉你了
wuhanlucsdn 2016-11-30
  • 打赏
  • 举报
回复
是的,但是后面生成的文件里面有很多相同的内容,不知道怎么把重复的内容删掉?
振翅高飞 2016-11-30
  • 打赏
  • 举报
回复
我记得你这个问题不是在另一个帖子里面写过代码给你吗?
wuhanlucsdn 2016-11-30
  • 打赏
  • 举报
回复
@赵4老师 可以用,代码看起来好复杂啊,我得花点时间消化消化
赵4老师 2016-11-30
  • 打赏
  • 举报
回复
仅供参考:
//文件1中的内容排序并去重,结果保存到文件2中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 128      //能处理的最大行宽,包括行尾的\n和字符串尾的\0
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
int c,n,hh,i,L;
FILE *f;
char ln[MAXCHARS];
int ignore_case=0;
int icompare(const void *arg1,const void *arg2) {
   return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
   return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
    if (argc<3) {
        printf("Unique line. Designed by zhao4zhong1@163.com. 2012-08-20\n");
        printf("Usage: %s src.txt uniqued.txt [-i]\n",argv[0]);
        return 1;
    }
    if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
    f=fopen(argv[1],"r");
    if (NULL==f) {
        printf("Can not find file %s!\n",argv[1]);
        return 1;
    }
    buf=(char *)malloc(MAXLINES*MAXCHARS);
    if (NULL==buf) {
        fclose(f);
        printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
        return 2;
    }
    n=0;
    hh=0;
    i=0;
    while (1) {
        if (NULL==fgets(ln,MAXCHARS,f)) break;//
        hh++;
        L=strlen(ln)-1;
        if ('\n'!=ln[L]) {//超长行忽略后面内容
            printf("%s Line %d too long(>%d),spilth ignored.\n",argv[1],hh,MAXCHARS);
            while (1) {
                c=fgetc(f);
                if ('\n'==c || EOF==c) break;//
            }
        }
        while (1) {//去掉行尾的'\n'和空格
            if ('\n'==ln[L] || ' '==ln[L]) {
                ln[L]=0;
                L--;
                if (L<0) break;//
            } else break;//
        }
        if (L>=0) {
            strcpy(buf+i,ln);i+=MAXCHARS;
            n++;
            if (n>=MAXLINES) {
                MAXLINES2=MAXLINES*2;
                if (MAXLINES2==1280000) MAXLINES2=2500000;
                buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
                if (NULL==buf2) {
                    printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
                    printf("WARNING: Lines >%d ignored.\n",MAXLINES);
                    break;//
                }
                buf=buf2;
                MAXLINES=MAXLINES2;
            }
        }
    }
    fclose(f);
    if (n>1) {
        if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
        else qsort(buf,n,MAXCHARS,compare);
    }
    f=fopen(argv[2],"w");
    if (NULL==f) {
        free(buf);
        printf("Can not create file %s!\n",argv[2]);
        return 2;
    }
    fprintf(f,"%s\n",buf);
    if (n>1) {
        if (ignore_case) {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if (stricmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        } else {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if ( strcmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        }
    }
    fclose(f);
    free(buf);
    return 0;
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧