关于文本文件数据处理,求大神!回答的好加50分!
发布网友
发布时间:2022-04-20 08:37
我来回答
共3个回答
热心网友
时间:2023-07-16 23:38
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fread_line(char *dst, FILE *fp)
{
int i = 0;
int ret;
while(1)
{
ret = fread(&dst[i], 1, 1, fp);
if(ret != 1) break;
if(dst[i] == 0x0a) break;
i ++;
}
if(i>0 && dst[i - 1] == 0x0d)
dst[i-1] = 0;
else dst[i] = 0;
return i;
}
int find(char *src, char *str)
{
int len1, len2;
int i;
len1 = strlen(src);
len2 =strlen(str);
if(len1 < len2) return 0;
for(i = 0; i <= len1 - len2; i ++)
if(memcmp(src + i, str, len2) == 0) return 1;
return 0;
}
void parse_file(char * file)
{
FILE *fp;
char buffer[1024*1024];
int flag = 0;
fp = fopen(file, "rb");
if(fp == NULL) return;
printf("info in %s\n", file);
while(1)
{
if(fread_line(buffer, fp) == 0) break;
///printf("%s\n", buffer);
if(flag == 0)
{
if(find(buffer, "2013-09-30 十大流通股东情况"))
{
flag = 1;
}
}
else if(flag == 1)
{
if(find(buffer, "─────────────────────────────────────"))
flag = 2;
}
else if(flag == 2)
{
if(find(buffer, "─────────────────────────────────────"))
break;
printf("%s\n", buffer);
}
}
printf("\n");
}
int main(int argc, char **argv)
{
char path[1024];
int path_len;
FILE *fp;
char f_name[100];
if(argc == 1)
{
printf("input source file path\n");
gets(path);
}
else strcpy(path, argv[1]);
path_len = strlen(path);
if(path[path_len - 1] != '\\') path[path_len++] = '\\', path[path_len] = 0;
//open list file
strcat(path, "list.txt");
fp = fopen(path, "rb");
if(fp == NULL) {printf("no list file\n");return -1;}
while(1)
{
if(fread_line(f_name, fp))
{
strcpy(&path[path_len], &f_name[1]);
strcat(path, ".txt");
parse_file(path);
}
else break;
}
fclose(fp);
return 0;
}
使用说明
编译成功后生成可执行文件,假设为parse.exe
有两种使用方式
调用parse.exe file_path
调用parse.exe 然后在程序中设置file_path
比如 list.txt和其他txt存在于E:\test文件夹
可以使用parse.exe E:\test
也可以调用parse.exe
然后输入 E:\test
程序结果显示在dos窗口中
如果数量过大可以保存在文件中
方法为
parse.exe E:\test > save.txt
本机测试通过 如无问题 请尽快采纳
同样文件已发送到你邮箱中
追问谢谢你的帮助!!!
热心网友
时间:2023-07-16 23:38
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
bool ReadList(list<string>& fileList)
{
fstream fsList("list.txt", ios::in);
if (!fsList) {
return false;
}
vector<char> buffer(1024);
for(;;) {
fill(buffer.begin(), buffer.end(), 0);
fsList.getline(&buffer[0], 1024);
if (fsList.fail()) break;
if (buffer[0] != 0)
fileList.push_back( string(&buffer[1]) + ".txt" );
}
fsList.close();
return true;
}
bool ProcessFile(const string& fileName)
{
fstream fs(fileName.c_str(), ios::in | ios::binary);
if (!fs)
return false;
int fileSize;
fs.seekg(0, ios::end);
fileSize = fs.tellg();
fs.seekg(0);
vector<char> fileContent(fileSize + 1);
fill(fileContent.begin(), fileContent.end(), 0);
fs.read(&fileContent[0], fileSize);
fs.close();
vector<char>::iterator fileEnd = fileContent.end();
vector<char>::iterator filePos = fileContent.begin();
string tofind1("2013-09-30 十大流通股东情况");
vector<char> tofindvec1(tofind1.size());
fill(tofindvec1.begin(), tofindvec1.end(), 0);
copy(tofind1.begin(), tofind1.end(), tofindvec1.begin());
filePos = search(filePos, fileEnd,
tofindvec1.begin(), tofindvec1.end());
if (filePos == fileEnd) return false;
string tofind2("——————");
vector<char> tofindvec2(tofind2.size());
fill(tofindvec2.begin(), tofindvec2.end(), 0);
copy(tofind2.begin(), tofind2.end(), tofindvec2.begin());
filePos = search(filePos, fileEnd,
tofindvec2.begin(), tofindvec2.end());
if (filePos == fileEnd) return false;
filePos = find(filePos, fileEnd, '\n');
if (filePos == fileEnd) return false;
++filePos;
vector<char>::iterator outStart = filePos;
filePos = search(filePos, fileEnd, tofindvec2.begin(), tofindvec2.end());
if (filePos == fileEnd) return false;
vector<char>::iterator outEnd = filePos;
string outFileName = fileName.substr(0, fileName.size() - 4) + "-3.txt";
fstream outFile(outFileName, ios::out | ios::binary);
int outFileSize = outEnd - outStart;
outFile.write(&(*outStart), outFileSize);
outFile.close();
return true;
}
int main()
{
list<string> fileList;
if (ReadList(fileList) == false) {
cerr << "打开list.txt失败" << endl;
return 0;
}
list<string>::iterator file_iter, file_iter_end;
file_iter_end = fileList.end();
for(file_iter = fileList.begin(); file_iter != file_iter_end; ++file_iter)
if (ProcessFile(*file_iter) == false)
cerr << "处理" << *file_iter << "失败" << endl;
else
cerr << "处理" << *file_iter << "成功" << endl;
return 0;
}
你的那个横线到底是 —————— 还是 -------- 我没确定。
先给你用前者了。
试试看行不行
热心网友
时间:2023-07-16 23:38
用C写一个看看,自己搞文件测试中
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define STRTOFIND "2013-09-30 十大流通股东情况"
#define LISTFILE "list.txt"
int procList();
int procDetail( char *file );
int trim( char *s );
int trim( char *s )
{
int i, l, r, len;
for(len=0; s[len]; len++);
for(l=0; (s[l]==' ' || s[l]=='\t' || s[l]=='\n' || s[l] == '\r'); l++);
if(l==len)
{
s[0]='\0';
return(0);
}
for(r=len-1; (s[r]==' ' || s[r]=='\t' || s[r]=='\n' || s[r] == '\r'); r--);
for(i=l; i<=r; i++) s[i-l]=s[i];
s[r-l+1]='\0';
return(0);
}
int procList()
{
FILE *fList;
char istr[513];
char detailfile[513];
if ( (fList=fopen(LISTFILE, "rb")) == NULL )
{
printf( "open %s failed, %d|%s\n", LISTFILE, errno, strerror(errno) );
return -1;
}
while( 1 )
{
memset( istr, 0, sizeof(istr) );
if ( fgets( istr, 512, fList ) == NULL )
break;
trim( istr );
if ( istr[0] == 0 )
continue;
memset( detailfile, 0, sizeof(detailfile) );
memcpy( detailfile, istr+1, strlen(istr)-1 );
strcat( detailfile, ".txt" );
procDetail( detailfile );
}
fclose( fList );
}
int procDetail( char *file )
{
FILE *dFile, *ofp;
char istr[4096];
char ofile[513];
int sFlag, bFlag;
if ( (dFile=fopen(file,"rb")) == NULL )
{
printf( "open %s failed, %d|%s\n", file, errno, strerror(errno) );
return -1;
}
memset( ofile, 0, sizeof(ofile) );
memcpy( ofile, file, strlen(file)-4 );
strcat( ofile, "-3.txt" );
if ( (ofp=fopen(ofile,"wb+")) == NULL )
{
printf( "open %s failed, %d|%s\n", file, errno, strerror(errno) );
return -1;
}
sFlag = 0, bFlag = 0;
while( 1 )
{
memset( istr, 0, sizeof(istr) );
if ( fgets(istr, sizeof(istr)-1, dFile ) == NULL )
break;
trim( istr );
if ( strstr( istr, STRTOFIND ) != NULL )
sFlag = 1, bFlag = 0; /**find***/
if ( sFlag == 1 && strstr( istr, "----------" ) != NULL )
{
bFlag ++;
continue;
}
if ( bFlag == 2 )
break;
if ( sFlag == 1 && bFlag == 1 )
fprintf( ofp, "%s\n", istr );
}
fclose( ofp );
fclose( dFile );
}
void main( int argc, char **argv )
{
procList();
}