急求朋友的帮助!有关C语言 比较两个文件
发布网友
发布时间:2022-07-04 14:56
我来回答
共2个回答
热心网友
时间:2022-07-09 03:25
Sorry啊,一直没什么时间,只写了一部分,把文件内容读到内存里了。
#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#define N 10
typedef struct
{
char acClass[8];
char acID[12];
char acName[8];
}Info;
int ReadFirstFile(void **p);//读取文件内容到内存
int ReadSecondFile(void **p);//读取文件内容到内存
int AlocMem(void *p);
int main()
{
void *p = NULL;
char *q = malloc(6*N);
memset(q,0,6*N);
AlocMem(&p);
ReadFirstFile(p);
ReadSecondFile(&q);
return 0;
}
int AlocMem(void **p)
{
*p = malloc(sizeof(Info)*N);
if (NULL== *p)
{
return -3; //内存分配失败
}
if (!memset(*p,0,sizeof(Info)*N))
{
return -4; //内存初始化失败
}
return 0;
}
int ReadFirstFile(void *p)
{
FILE *fp = fopen("1.txt","r");
char ch = 0;
int i = 0; //第i条记录
int j = 0; //记录内的第j列
int k = 0;
Info *pInfo = NULL;
if (NULL == fp)
{
return -1; //文件打开失败
}
if (NULL == p)
{
return -2; //传入参数为空
}
pInfo = (Info*)p;
while (ch=fgetc(fp))
{
if (' ' == ch)
{
continue;
}
if ((ch!='N') && (j==0))
{
(pInfo+i)->acClass[k++] = ch;
continue;
}
for (k=0,j=1; k<11; k++) //学号长度为11个字符
{
(pInfo+i)->acID[k] = ch;
ch = fgetc(fp);
if (ch=='\n')
{
break;
}
}
j = 2;
k = 0;
while (ch!='\n')
{
if (ch==' ')
{
ch = fgetc(fp);
continue;
}
(pInfo+i)->acName[k++] = ch;
ch = fgetc(fp);
if (ch == EOF)
{
return 0; //读取结束
}
}
k = 0;
j = 0;
i++; //读取下一条记录
}
return 0;
}
int ReadSecondFile(void **p)
{
FILE *fp = fopen("2.txt","r");
char ch = 0;
int i = 0; //第i条记录
int k = 0;
char *pcID ;
if (NULL == fp)
{
return -1; //文件打开失败
}
if (NULL == p)
{
return -2; //传入参数为空
}
// ch = fgetc(fp);
pcID = (char*)(*p);
while (ch=fgetc(fp))
{
while (ch!='\n')
{
if (ch==' ')
{
ch = fgetc(fp);
continue;
}
(pcID+i*6)[k++] = ch;
ch = fgetc(fp);
if (ch == EOF)
{
return 0; //读取结束
}
}
i++;
k = 0;
}
return -1;
}
热心网友
时间:2022-07-09 03:25
对啊,你这个说得不详细,怎么帮忙啊?