关于一个文件操作的问题(百分以上)

时间:2021-12-18 21:16:53
我要实现这样一个程序:
就像数据库一样的操作:
比如说,我要把一些设备信息存起来,例如:设备编号,设备名称,设备购买人,设备价格,设备购买时间等信息。
这个程序要能够动态的添加字段,比如说,我要添加:设备使用时间,设备使用情况等。
还要有一般的数据库操作:
比如:添加新设备,修改设备信息,删除设备信息,怎么做呀,那位有原码,如果满意,可出重分,可高达五百分。最少一百分。

注意:用程序和自己定义的文件来实现,不要任何数据库。

10 个解决方案

#1


用结构看看
下面是我的程序里面对于类似问题的处理

#2


用数据结构的链表,
详细的可以去看数据结构的书。
原码……实在太大了。

而且可以考虑各种搜索,如深度/广度优先搜索,广义表等

#3


struct POS
{
LONG start;
LONG end;
POS()
{
start=-1;
end=-1;
}
};
struct INFO
{
char url[100];
char localFile[50];
BOOL ifFinish;
BOOL MulThread;
long fileLen;
POS pos[4];
long lenFinish;
CTime lastModify;
INFO()
{
url[0]='\0';
localFile[0]='\0';
ifFinish=TRUE;
MulThread=TRUE;
fileLen=-1;
lenFinish=0;
}
};

#4


这只是结点类的定义而已,很多东西还是你自己看书吧

而且各种插入/删除方式都有优缺点的。
象楼上这位的结构,如果要大量搜索/查找/插入/删除的话,是比较困难的。

#5


是阿
关于 操作能象想象的那样好的话
要自己重载文件类的
很烦的

#6


动态字段的处理,我想,或许可以用类似链表的结点插入。

做这么一个链表:设备编号->设备名称->设备购买人->设备价格->设备购买时间,那么需要什么添加什么就用插入了。
而且读数据的时候可以按上边的链表顺序来读,各种不同内容的结点从一个基类派生出来。

#7


struct POS
{
LONG start;
LONG end;
POS()
{
start=-1;
end=-1;
}
};
struct INFO
{
char url[100];
char localFile[50];
BOOL ifFinish;
BOOL MulThread;
long fileLen;
POS pos[4];
long lenFinish;
CTime lastModify;
INFO()
{
url[0]='\0';
localFile[0]='\0';
ifFinish=TRUE;
MulThread=TRUE;
fileLen=-1;
lenFinish=0;
}
};

#8


写文件就直接把结构写入文件


void CHttpBotDlg::ReadInfo(BOOL isFinish)
{
m_tList.DeleteAllItems();
// ifFinishList=IsFinish;
CFile file;
if(!file.Open("d:\\HttpBot.txt", CFile::modeRead))
{
file.Open("d:\\HttpBot.txt",CFile::modeCreate);
file.Close();
return;
}
else
{
INFO *info = new INFO();
file.SeekToBegin();
int index;
while(file.Read(info,sizeof(INFO)) == sizeof(INFO))
{
if(info->ifFinish != isFinish)
continue;
ITEMINFO *item = new ITEMINFO();
if(isFinish == FALSE)
{
CFile f;
LONG size=0;
for(int i=0; i<4; i++)
{
CString name;
name.Format(".bot%d",i);
name = info->localFile+name;
if(f.Open(name, CFile::modeRead))
{
size+=f.GetLength();
f.Close();
}
}
item->downloadSize = size;

}
item->fileSize = info->fileLen;
// int ti = info->fileLen / 100;
item->downloadSize = info->lenFinish;
item->per = (item->downloadSize / item->fileSize) * 100;


CString temp = info->localFile;
int j = temp.ReverseFind('\\');
item->fileName = temp.Mid(j + 1);
item->savePath = temp.Left(j+1);
item->remoteUrl = info->url;
item->per = (info->lenFinish * 100) / info->fileLen;
if(isFinish)
item->state = TA_FINISH;
else
item->state = TA_STOP;
index = m_tList.InsertItem(item);
delete item;
item = NULL;
}
file.Close();
}
}

#9


考虑了一下,最好还是做成矩阵,稀疏矩阵的十字链表
不过操作起来就没一维链表那么简单了。

如果能组织成树就好了……不过还没想好……

#10


一个小例子,留个email,一起研究研究

#1


用结构看看
下面是我的程序里面对于类似问题的处理

#2


用数据结构的链表,
详细的可以去看数据结构的书。
原码……实在太大了。

而且可以考虑各种搜索,如深度/广度优先搜索,广义表等

#3


struct POS
{
LONG start;
LONG end;
POS()
{
start=-1;
end=-1;
}
};
struct INFO
{
char url[100];
char localFile[50];
BOOL ifFinish;
BOOL MulThread;
long fileLen;
POS pos[4];
long lenFinish;
CTime lastModify;
INFO()
{
url[0]='\0';
localFile[0]='\0';
ifFinish=TRUE;
MulThread=TRUE;
fileLen=-1;
lenFinish=0;
}
};

#4


这只是结点类的定义而已,很多东西还是你自己看书吧

而且各种插入/删除方式都有优缺点的。
象楼上这位的结构,如果要大量搜索/查找/插入/删除的话,是比较困难的。

#5


是阿
关于 操作能象想象的那样好的话
要自己重载文件类的
很烦的

#6


动态字段的处理,我想,或许可以用类似链表的结点插入。

做这么一个链表:设备编号->设备名称->设备购买人->设备价格->设备购买时间,那么需要什么添加什么就用插入了。
而且读数据的时候可以按上边的链表顺序来读,各种不同内容的结点从一个基类派生出来。

#7


struct POS
{
LONG start;
LONG end;
POS()
{
start=-1;
end=-1;
}
};
struct INFO
{
char url[100];
char localFile[50];
BOOL ifFinish;
BOOL MulThread;
long fileLen;
POS pos[4];
long lenFinish;
CTime lastModify;
INFO()
{
url[0]='\0';
localFile[0]='\0';
ifFinish=TRUE;
MulThread=TRUE;
fileLen=-1;
lenFinish=0;
}
};

#8


写文件就直接把结构写入文件


void CHttpBotDlg::ReadInfo(BOOL isFinish)
{
m_tList.DeleteAllItems();
// ifFinishList=IsFinish;
CFile file;
if(!file.Open("d:\\HttpBot.txt", CFile::modeRead))
{
file.Open("d:\\HttpBot.txt",CFile::modeCreate);
file.Close();
return;
}
else
{
INFO *info = new INFO();
file.SeekToBegin();
int index;
while(file.Read(info,sizeof(INFO)) == sizeof(INFO))
{
if(info->ifFinish != isFinish)
continue;
ITEMINFO *item = new ITEMINFO();
if(isFinish == FALSE)
{
CFile f;
LONG size=0;
for(int i=0; i<4; i++)
{
CString name;
name.Format(".bot%d",i);
name = info->localFile+name;
if(f.Open(name, CFile::modeRead))
{
size+=f.GetLength();
f.Close();
}
}
item->downloadSize = size;

}
item->fileSize = info->fileLen;
// int ti = info->fileLen / 100;
item->downloadSize = info->lenFinish;
item->per = (item->downloadSize / item->fileSize) * 100;


CString temp = info->localFile;
int j = temp.ReverseFind('\\');
item->fileName = temp.Mid(j + 1);
item->savePath = temp.Left(j+1);
item->remoteUrl = info->url;
item->per = (info->lenFinish * 100) / info->fileLen;
if(isFinish)
item->state = TA_FINISH;
else
item->state = TA_STOP;
index = m_tList.InsertItem(item);
delete item;
item = NULL;
}
file.Close();
}
}

#9


考虑了一下,最好还是做成矩阵,稀疏矩阵的十字链表
不过操作起来就没一维链表那么简单了。

如果能组织成树就好了……不过还没想好……

#10


一个小例子,留个email,一起研究研究