I am trying to save a string with a char * string in a file.
struct d_object {int flags; Int time; Int offset; Char * filename; }; The problem is that when I do this, it will obviously save the address of that indicator instead of the string. So what I have done uses only one character array, but I have been forced to set the maximum size of the string. It works fine, though I was wondering if there is a file to store the stratch in any file (at some point I can maulok) and then retrieve it. I can save the string and straight separately and then retrieve it but it is very messy. It would be better if I could load and save the entire string (above) in the file. Thanks!
The code with the four arrays is below:
#include & lt; Stdio.h & gt; #include & lt; String.h & gt; # Include & lt; Fcntl.h & gt; Struct d_object {int flags; Int time; Int offset; Four filenames [255]; }; Int main (int argc, char ** argv) {struct d_object fcb; Fcb.flags = 5; Fcb.time = 100000; Fcb.offset = 220; Strncpy (fcb.filename, "myFile", 255); Int fd = open ("testfile", O_RDWR); Write (FD, & FCB, sizeof (FCB)); Close (FD); Int fd2 = open ("testfile", O_RDONLY); Struct d_object new_fcb; Read (fd2, & new_fcb, sizeof (new_fcb)); Printf ("Read from file testfile:% s \ n", new_fcb.filename); Return 0; } PS: I am not using stream functions only because it really means running on an embedded OS which is not them. I have just optimized the code for BSD / Linux, so asking more questions makes it more understandable.
I understand that portability is not a problem because you are working for an embedded system In other cases, you should use something like XML.
You can change your code back in:
struct d_object {int flags; Int time; Int offset; Char * filename; }; And then save each piece of data individually:
(FD, and record.flags, size (int)); Write (FD, and record; timing, size); Write (FD, and record. Offset, Sizes (int)); Int filename_length = strlen (filename); Write (fd, & amp; filename_lamb, size (int)); Write (fd, record.filname, filename_log); To read, you must read each item separately, and then file name:
int filename_length; Read (FD, and blank record. Fags, size (integer)); Read (FD, and empty record time, size (int)); Read (FD, and blank record. Offset, size (intestine)); Read (filename_land, size (intestine), 1, file); Blank record.filenium = (four *) molk (size) * (filename_lamp +1)); Read (FD, blank record.filname, filename_log); * (Richtercord.filename + filename_long) = 0;
Comments
Post a Comment