VS2013 ASP.NET C#语言 我想通过读取文件夹下为图片的名字,把这些名字保存在一个数组中

VS2013 ASP.NET C#语言 我想通过读取文件夹下为图片的名字,把这些名字保存在一个数组中,第1张

var imageFiles = from img in System.IO.Directory.GetFiles("D:\\TestTxt").Where(a =>a.EndsWith(".jpg") || a.EndsWith(".bmp"))

select new

{

FileName = img

}

一句话取指定目录下面的指定文件,然后Linq到一个匿名对象了。你如果取得要存其他内容,就加属性。然后就调你写的存数据的方法了

你这么做肯定是有问题的,sizeof(pe) 或sizeof(People) 获取的并不是写入实际数据的长度, 而只是People的长度, 这个长度是固定的。

正确的做法可以通过两种方式来处理:

1、 通过重载流输出/输入流

ostream&operator <<(ostream&os, People&p) {

// people 转换成公有成员, 或在类中friend 该函数

os <<p.people.name <<p.people.weight // .... 按需输出成员

return os

}

读取通过重载流输入

istream&operator >>(istream&in, People&p) {

in >>p.people.name >>p.people.weight // .... 按照写入成员顺序将所有成员读取完成

return in

}

main() 中的file.write() 改成 file <<pe

main() 中的file.read() 改成 file >>pe2

2、将people 结构中的成员都换成定长数组。

如:

struct

{

char name[32]

char weight[32]

....

}people

main() 函数的file.write 改成:file.write(&pe.people, sizeof(pe.people))

main() 函数的file.read 改成:file.read(&pe.people, sizeof(pe.people))


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11421992.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存