记录C语言学习经历
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void readFile(char* strInput,char* strOuput)
{
FILE* fp = fopen(strInput, "r");
printf("当前读取文件名:%s\n",strInput);
FILE* fp2 = fopen(strOuput, "a");
if (!fp)
{
puts("文件读取时打开失败!");
return ;
}
if (!fp2)
{
puts("文件写入时打开失败!");
return ;
}
char buf[256] = {0};
char* p;
while (1)
{
if (NULL == fgets(buf, 256, fp))
return;
if (p = strstr(buf, "Unc."))//匹配字符串
{
fprintf(fp2, "%.60s\n", p);
}
}
fclose(fp);
fclose(fp2);
}
int main()
{
system("dir D:\\file\\abc*.txt /B > D:\\file\\gdl.txt");//生成文件名列表
FILE* pf = fopen("D:\\file\\gdl.txt","r");
char buf[64];
int len;//截断字符串
int i = 0;
while(fgets(buf, 64, pf))
{
char tmp[32] = {0};
sprintf(tmp,"%s%s","D:\\\\file\\\\",buf);//拼接
len = strlen(tmp);
tmp[len - 1] = '\0';
readFile(tmp,"D:\\file\\a.txt");
++i;
}
printf("读取了%d个文件的数据!",i);
fclose(pf);
system("pause");
return 0;
}