none
On saving configure paramters to disk file and read them into memory next time RRS feed

  • Question

  • Naturely, I comp up with an idea that use a large structure to save it as *.dat  file(binary file), using fwrite/fread to load and store. BUT this is not uncommon, I found some program uses *.ini file, and other uses xml file

    so,

    1) what's disavantage of .dat for using to save parameters? it's simple, using fwrite/fread only.

    what's the advantage of .ini file and xml file to save parameter? 

    2) I  write ONE structure data(4K data) to disk by using fwrite(),  VS2015 failed, prompting me error of access violation, if I reduce the structure size to 1K, It is ok, so I guess it's matter of time taken to writing, question are here: how to judge if  fwrite() is completed or not in case of writing only one data structure?   code as below

    FILE *fp;

    fp = open("c.dat","wb"); 

    fwrite(&myData, sizeof(my_data_t), 1, fp);

    thanks a lot




    • Edited by hitbuyi Tuesday, October 22, 2019 9:44 AM
    Tuesday, October 22, 2019 9:00 AM

Answers

  • One obvious reason is ease of use.  .ini and .xml files are text files.  They are easily viewed and edited.

    A binary format configuration file is more difficult to work with, especially if the file format is not documented.

    • Marked as answer by hitbuyi Tuesday, October 22, 2019 9:19 AM
    Tuesday, October 22, 2019 9:12 AM

All replies

  • One obvious reason is ease of use.  .ini and .xml files are text files.  They are easily viewed and edited.

    A binary format configuration file is more difficult to work with, especially if the file format is not documented.

    • Marked as answer by hitbuyi Tuesday, October 22, 2019 9:19 AM
    Tuesday, October 22, 2019 9:12 AM
  • Naturely, I comp up with an idea that use a large structure to save it as *.dat  file(binary file), using fwrite/fread to load and store. BUT this is not uncommon, I found some program uses *.ini file, and other uses xml file

    so,

    1) what's disavantage of .dat for using to save parameters? it's simple, using fwrite/fread only.

    what's the advantage of .ini file and xml file to save parameter? 

    2) I  write ONE structure data(4K data) to disk by using fwrite(),  VS2015 failed, prompting me error of access violation, if I reduce the structure size to 1K, It is ok, so I guess it's matter of time taken to writing, question are here: how to judge if  fwrite() is completed or not in case of writing only one data structure?   code as below

    FILE *fp;

    fp = open("c.dat","wb"); 

    fwrite(&myData, sizeof(my_data_t), 1, fp);

    thanks a lot




    You have changed your original question after RLWA32 has answerd and you have set the answer to your question. By now, it was only question number (1) before.

    Number (2) is a new question, so post it in another question because not many people will look here because you have closed this post already. For fwrite check its return value, read this: https://en.cppreference.com/w/c/io/fwrite

    Regards, Guido


    Tuesday, October 22, 2019 9:59 AM
  • 2) I  write ONE structure data(4K data) to disk by using fwrite(),  VS2015 failed, prompting me error of access violation, if I reduce the structure size to 1K, It is ok, so I guess it's matter of time taken to writing, question are here: how to judge if  fwrite() is completed or not in case of writing only one data structure?   code as below

    FILE *fp;

    fp = open("c.dat","wb"); 

    fwrite(&myData, sizeof(my_data_t), 1, fp);

    thanks a lot




    This code doesn't make sense.  if you want to use fwrite then the file should be opened with fopen or fopen_s.

    The _open function returns an int value, not a FILE* pointer.

    Tuesday, October 22, 2019 10:21 AM
  • sorry it is a typo,

    the code should be fopen() of type of FILE

    Monday, October 28, 2019 1:54 AM
  • There is nothing in your prior posts to indicate why your code would cause an access violation.  And there is no obvious reason why fwrite would be unable to write a 4K structure to disk.

    I suspect there are problems with code we have not seen.

    For example, using VS2015  -

    #define _CRT_SECURE_NO_WARNINGS
    
    #include <stdio.h>
    #include <tchar.h>
    #include <sys\stat.h>
    
    struct mystruct {
    	char buf[4096];
    };
    
    int main()
    {
    	FILE *fp = nullptr;
    	mystruct s;
    
    	constexpr size_t sz = sizeof(mystruct);
    
    	memset(&s, 'A', sz);
    
    	fp = fopen("F:\\RLWA32\\Testfile.dat", "wb");
    
    	if (fp)
    	{
    		struct _stat filestat;
    
    		fwrite(&s, sz, 1, fp);
    
    		fclose(fp);
    
    		if (_stat("F:\\RLWA32\\Testfile.dat", &filestat) == 0)
    			printf("File size is %u\n", filestat.st_size);
    	}
    
    	return 0;
    }

    Monday, October 28, 2019 11:35 AM