把文件中大量的数据去掉重复的再写入文件

今天学习了一下vector容器,解决了这个问题,现附上代码:

#include <iostream>
#include <algorithm>   
#include <set>

using namespace std;

void Dedup(FILE* infile,FILE* outfile)
{
	int iTemp;
	unsigned int nCount = 0;

	typedef set<int> IntSet;
	IntSet iArray;

	while(NULL == feof(infile))
	{
		fscanf(infile,"%d",&iTemp);
		iArray.insert(iTemp);

		nCount ++;
	}

	printf("nCount = %d\n",nCount);//total numbers
       
	for(IntSet::iterator iter = iArray.begin(); iter != iArray.end(); ++ iter)
	{
		fprintf(outfile,"%d ",*iter);
	}

}

int main()
{
	FILE *fpInput,*fpOutput;

	if(NULL == (fpInput = fopen("D:\\input.txt","r")))
	{
		printf("File read error!\n");
		exit(1);
	}

	if(NULL == (fpOutput = fopen("D:\\output.txt","w")))
	{
		printf("File write error!\n");
		exit(1);
	}

	Dedup(fpInput,fpOutput);	
	printf("\n");

	fclose(fpInput);
	fclose(fpOutput);

	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <iostream> 
#include <string> 
#include <vector> 
#include <set> 
#include <algorithm>

using   namespace   std; 

#define   ARRAY_LENGTH(array)   (sizeof(array) / sizeof(array[0])) 

FILE *fpInput,*fpOutput;

template   <typename   T> 
void print_element(T value) 
{ 	
	//print
	//std::cout<<value<<std::endl; 
	//print into output.txt
	fprintf(fpOutput,"%d ",value);
} 

template <typename T> 
unsigned int MakeUniqueArray(T* array,unsigned int length) 
{ 
	std::vector <T> v(array,array + length); 
    std::sort(v.begin(),v.end()); 
    T* end = std::unique_copy(v.begin(),v.end(),array); 
    return   end - array; 
} 

void main()
{
	if(NULL == (fpInput = fopen("input.txt","r")))
	{
		printf("File read error!\n");

		exit(1);
	}

	if(NULL == (fpOutput = fopen("output.txt","w")))
	{
		printf("File write error!\n");

		exit(1);
	}

	//read
	const int MAXLEN = 1024;
	int array[MAXLEN];
	int nCount = 0;
	while(NULL == feof(fpInput))
	{
		fscanf(fpInput,"%d",&array[nCount]);

		//printf("%d ",array[i]);

		nCount ++ ;
	}
	void (*pfi)(int) = print_element; 
	unsigned int len = MakeUniqueArray(array,nCount); 
    std::for_each(array,array + len,print_element <int>); 
    //std::cin.get(); 

	fclose(fpInput);
	fclose(fpOutput);	
}

纯C实现的代码:

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

int* XRemove(int Dst[], int Src[], int Size,int &nSize)  
{  
    bool* Index = new bool[Size];  
  
    for(int i = 0; i < Size; ++i)  
        Index[i] = true;  
  
    int* Crt = Dst;      
  
    nSize = 0;  
    for(int x = 0; x < Size; ++x)  
    {  
        if(0 == Index[x])  
            continue;  
  
        for(int y = x + 1; y < Size; ++y)  
            if(Src[x] == Src[y])  
                Index[y] = false;  
  
        if(Index[x]){  
            nSize ++;  
            *Crt++ = Src[x];  
        }  
    }  
  
    delete [] Index;  
    return Dst;  
}  

void main()
{
	FILE *fpInput,*fpOutput;

	if(NULL == (fpInput = fopen("input.txt","r")))
	{
		printf("File read error!\n");

		exit(1);
	}

	if(NULL == (fpOutput = fopen("output.txt","w")))
	{
		printf("File write error!\n");

		exit(1);
	}

	//read
	const int MAXLEN = 1024;
	int array[MAXLEN];
	int nCount = 0;
	while(NULL == feof(fpInput))
	{
		fscanf(fpInput,"%d",&array[nCount]);
	//	printf("%d ",array[nCount]);
		nCount ++ ;
	}

	int Dst[MAXLEN];
	int nSize;
	XRemove(Dst,array,nCount,nSize);

	for(int i = 0; i < nSize;i ++)
		//printf("%d ",Dst[i]);
		fprintf(fpOutput,"%d ",Dst[i]);


	fclose(fpInput);
	fclose(fpOutput);	
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值