Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 15, 2004 how to save / load files with D | ||||
---|---|---|---|---|
| ||||
hello, i'm wondering what is the best way to save / load files in D. Right now i'm using fopen fread fwrite and ftell to save and load files like this int main(char[][] argv) { int bob = 7; cTest test = new cTest(4,4,4); FILE *pFile; // write file pFile = fopen("testio","wb"); // open new file for writing fwrite(&bob, (ftell(pFile)+1), int.sizeof, pFile); fwrite(test, (ftell(pFile)+1), cTest.sizeof, pFile); fclose(pFile); // close file // change values bob = 8; printf("%d\n", bob); test = new cTest(5,6,7); test.print(); // read file pFile = fopen("testio","rb"); // openfile for reading fread(&bob, (ftell(pFile)+1), int.sizeof, pFile); fread(test, (ftell(pFile)+1), cTest.sizeof, pFile); fclose(pFile); printf("%d\n", bob); test.print(); return 0; } are there some built in D features to make saving/loading to files easier : ) or do i pretty much have it down? Also, for some reason i get a seg fault if i just try to read a file without calling the write file function and i can't figure out why. i need to find a reliable save / load method. |
May 15, 2004 Re: how to save / load files with D | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | You can do it just as you would in C, or you can use std.file.read() and std.file.write(). |
May 15, 2004 Re: how to save / load files with D | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | clayasaurus wrote: > hello, i'm wondering what is the best way to save / load files in D. Right now > i'm using fopen fread fwrite and ftell to save and load files like this I don't know if it's the best way, but I usually use std.stream: http://www.digitalmars.com/d/phobos.html#stream Here's an example of reading in a file: http://www.dsource.org/tutorials/index.php?show_example=28 > int main(char[][] argv) > { > int bob = 7; > > cTest test = new cTest(4,4,4); > > FILE *pFile; > > // write file > pFile = fopen("testio","wb"); // open new file for writing > fwrite(&bob, (ftell(pFile)+1), int.sizeof, pFile); > fwrite(test, (ftell(pFile)+1), cTest.sizeof, pFile); > fclose(pFile); // close file > > > // change values > bob = 8; > printf("%d\n", bob); > > test = new cTest(5,6,7); > test.print(); > > > // read file > pFile = fopen("testio","rb"); // openfile for reading > fread(&bob, (ftell(pFile)+1), int.sizeof, pFile); > fread(test, (ftell(pFile)+1), cTest.sizeof, pFile); > fclose(pFile); > > printf("%d\n", bob); > test.print(); > > return 0; > } > > are there some built in D features to make saving/loading to files easier : ) or > do i pretty much have it down? Also, for some reason i get a seg fault if i just > try to read a file without calling the write file function and i can't figure > out why. i need to find a reliable save / load method. -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/ |
May 15, 2004 Re: how to save / load files with D | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | In article <c8446u$2jb7$1@digitaldaemon.com>, J C Calvarese says... > >clayasaurus wrote: >> hello, i'm wondering what is the best way to save / load files in D. Right now i'm using fopen fread fwrite and ftell to save and load files like this > >I don't know if it's the best way, but I usually use std.stream: http://www.digitalmars.com/d/phobos.html#stream > >Here's an example of reading in a file: http://www.dsource.org/tutorials/index.php?show_example=28 Ok, I've decided to use the std.stream as well. However, there is a slight problem. Why doesn't the std.stream support reading and writing the 'bit' data type? I could just use an int, but I'd rather use bit because it is smaller. |
May 15, 2004 Re: how to save / load files with D | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | clayasaurus wrote: >In article <c8446u$2jb7$1@digitaldaemon.com>, J C Calvarese says... > > >>clayasaurus wrote: >> >> >>>hello, i'm wondering what is the best way to save / load files in D. Right now >>>i'm using fopen fread fwrite and ftell to save and load files like this >>> >>> >>I don't know if it's the best way, but I usually use std.stream: http://www.digitalmars.com/d/phobos.html#stream >> >>Here's an example of reading in a file: >>http://www.dsource.org/tutorials/index.php?show_example=28 >> >> > >Ok, I've decided to use the std.stream as well. However, there is a slight >problem. Why doesn't the std.stream support reading and writing the 'bit' data >type? I could just use an int, but I'd rather use bit because it is smaller. > > > > I would imagine that it would be difficult to implement a bit type into a stream without adding padding. Of course bit-arrays are a different story all together. -- -Anderson: http://badmama.com.au/~anderson/ |
Copyright © 1999-2021 by the D Language Foundation