请问strncpy、memcpy、substring函数的参数有没有长度限制?
我有一个文件文件几十万行、每一行将近2000,
使用strncpy运行就会出错,我把文档减为5万行,一切都正常。
大伙有谁知道的吗?先谢谢了
我的程序:
(我把写文件部分省略掉了,写起来太长)
(程序执行到strncpy(RowStr,buf,len); 就会出错)
void __fastcall TMainForm::Button1Click(TObject *Sender)
{
TStruK TStru;
Init_TStruk(&TStru);
char ZipName[80],FileName[80],CmdStr[200],TmpStr[200],OutFile[80];
char *RowStr;
AnsiString WorkPath,RecoStr;
if(Edit1->Text==""){
Application->MessageBox("请输入数据日期!","提示信息",MB_ICONASTERISK);
return ;
}
WorkPath=GetCurrentDir();
sprintf(ZipName,"%s\\data\\%s_TStru.zip",WorkPath,Edit1->Text);
sprintf(FileName,"dwert.txt");
if(access(ZipName, 0)!=0){
Application->MessageBox("数据文件不存在,请确认!","提示信息",MB_ICONASTERISK);
return ;
}
sprintf(OutFile,"test.txt");
sprintf(TmpStr,"开始解压文件:[%s]",FileName);
RichEdit1->Lines->Add(AnsiString(TmpStr));
sprintf(CmdStr,"unzip.exe -j -o %s %s\\at01\\%s",ZipName,Edit1->Text,FileName);
if (system(CmdStr)!=0){
Application->MessageBox("解压文件失败,请检查!","提示信息",MB_ICONASTERISK);
RichEdit1->Lines->Add(AnsiString(CmdStr));
return ;
};
sprintf(TmpStr,"解压[%s]成功!",FileName);
RichEdit1->Lines->Add(AnsiString(TmpStr));
if((fpo=fopen(OutFile,"w+"))==NULL){
sprintf(TmpStr,"生成文件失败[%s],请检查!",OutFile);
RichEdit1->Lines->Add(AnsiString(TmpStr));
exit(0);
}
HANDLE hFile = CreateFile(FileName, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("创建文件对象失败,错误代码:%d ", GetLastError());
return;
}
HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (hFileMap == NULL)
{
printf("创建文件映射对象失败,错误代码:%d ", GetLastError());
return;
}
// 得到系统分配粒度
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
DWORD dwGran = SysInfo.dwAllocationGranularity;
// 得到文件尺寸
DWORD dwFileSizeHigh;
__int64 qwFileSize = GetFileSize(hFile, &dwFileSizeHigh);
qwFileSize |= (((__int64)dwFileSizeHigh) << 32);
// 关闭文件对象
CloseHandle(hFile);
// 偏移地址
__int64 qwFileOffset = 0;
// 块大小
DWORD dwBlockBytes = 1000 * dwGran;
if (qwFileSize < 1000 * dwGran)
dwBlockBytes = (DWORD)qwFileSize;
if (qwFileOffset >= 0)
{
TCHAR *lpbMapAddress = (TCHAR *)MapViewOfFile(hFileMap,FILE_MAP_ALL_ACCESS,
0, 0,
dwBlockBytes);
if (lpbMapAddress == NULL)
{
printf("映射文件映射失败,错误代码:%d ", GetLastError());
return;
}
// 对映射的视图进行访问
DWORD i = 0;
const char* buf = lpbMapAddress;
const char* start = lpbMapAddress;
int count=0;
int len;
while(start != NULL){
start = _get_line(buf, &len);
count++;
strncpy(RowStr,buf,len);
sscanf(RowStr,"%5s%5s%22s%1s%6s%61s%60s%60s%30s%19s\
%3s%10s%10s%30s%23s%23s%23s%23s%23s%23s\
%23s%30s%23s%23s%23s%23s%23s%23s%23s%5s\
%5s%19s%16s%40s%29s%29s%29s%23s%23s%23s\
%23s%23s%23s%23s%19s%23s%19s%23s%19s%23s\
%19s%23s%23s%23s%23s%23s%19s%23s%23s%23s\
%23s%22s%60s%30s%121s%121s%1s%10s%1s%66s\
%66s%9s%9s",
TStru.str1,TStru.str2,TStru.str3,TStru.str4,TStru.str5,TStru.str6,TStru.str7,
TStru.str8,TStru.str9,TStru.str10,TStru.str11,TStru.str12,TStru.str13,TStru.str14,
TStru.str15,TStru.str16,TStru.str17,TStru.str18,TStru.str19,TStru.str20,TStru.str21,
TStru.str22,TStru.str23,TStru.str24,TStru.str25,TStru.str26,TStru.str27,
TStru.str28,TStru.str29,TStru.str30,TStru.str31,TStru.str32,TStru.str33,TStru.str34,
TStru.str35,TStru.str36,TStru.str37,TStru.str39,TStru.str40,
TStru.str41,TStru.str42,TStru.str43,TStru.str44,TStru.str45,TStru.str46,
TStru.str47,TStru.str48,TStru.str49,TStru.str50,TStru.str51,TStru.str52,
TStru.str53,TStru.str54,TStru.str55,TStru.str56,TStru.str57,
TStru.str58,TStru.str59,TStru.str60,TStru.str61,TStru.str62,TStru.str63,
TStru.str64,TStru.str65,TStru.str66,TStru.str67,TStru.str68,TStru.str69,TStru.str70,
TStru.str71,TStru.str72,TStru.str73,TStru.str74
);
/* 写dwert.txt文件部分 */
buf = start;
}
// 撤销文件映像
UnmapViewOfFile(lpbMapAddress);
}
// 关闭文件映射对象句柄
CloseHandle(hFileMap);
Application->MessageBox("处理结束","提示信息",MB_ICONASTERISK);
}
const char* _get_line(const char* buf, int* len){
const char* tmp = buf;
while(*tmp && *tmp != 0x0d && *tmp != 0x0a){
++tmp;
}
*len = tmp - buf;
if (*tmp == 0) return NULL;
if (*tmp == 0x0d){
++tmp;
assert(*tmp == 0x0a);
++tmp;
return tmp;
}