如何写c配置文件|标准C语言

|

『壹』 怎样编写Cmake的配置文件Cmakelist.txt

1

Cmake 有linux ,windows 多个平台版本,如图是windows下版本程序

『贰』 用C#如何读写配置文件

INI文件就是扩展名为"ini"的文件。其一般形式如下:[section1] // 配置节//键名 //键值keyword1 = valuelkeyword2 = value2……[section2]keyword3 = value3keyword4 = value4在Windows系统中,INI文件是很多,最重要的就是"System.ini"、"System32.ini"和"Win.ini"。该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。但自从Windows 95的退出,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点,使应用程序和系统都把许多参数和初始化信息放进了注册表中。以及XML文件的国际标准化给INI文件又一次打击。但在某些场合,INI文件还拥有其不可替代的地位。比如绿色软件的规定就是不向注册表和系统中填入新东西。对于软件需要储存的信息就需要存入到文件中了。XML虽然兼容性比较好,但对于仅仅保存几个自定义参数而言就显得大材小用了。这是就可以选择使用快速简单的储存方式:INI文件。本文就来探讨一下C#是如何对INI进行读写操作。主要思路是调用Win32 API。1.引入命名空间usingSystem.Runtime.InteropServices;2.声明(把一个Win32 API函数转成C#函数)//声明INI文件的写操作函数 WritePrivateProfileString()[DllImport("kernel32")]private static extern longWritePrivateProfileString(string section, string key, string val, stringfilePath);//声明INI文件的读操作函数 GetPrivateProfileString()[DllImport("kernel32")]private static extern intGetPrivateProfileString(string section, string key, string def, StringBuilderretVal, int size, string filePath);3.函数public void Writue(string section,string key, string value){// section=配置节,key=键名,value=键值,path=路径WritePrivateProfileString(section,key, value, sPath);}public string ReadValue(stringsection, string key){// 每次从ini中读取多少字节System.Text.StringBuilder temp =new System.Text.StringBuilder(255);// section=配置节,key=键名,temp=上面,path=路径GetPrivateProfileString(section,key, "", temp, 255, sPath);returntemp.ToString(); //注意类型的转换}到此基本功能已经实现了。下面我们将所有的代码重新整合一下:namespace Library.File{public class Ini{// 声明INI文件的写操作函数 WritePrivateProfileString()[System.Runtime.InteropServices.DllImport("kernel32")]private static extern longWritePrivateProfileString(string section, string key, string val, stringfilePath);// 声明INI文件的读操作函数 GetPrivateProfileString()[System.Runtime.InteropServices.DllImport("kernel32")]private static extern intGetPrivateProfileString(string section, string key, string def,System.Text.StringBuilder retVal, int size, string filePath);private string sPath = null;public Ini(string path){this.sPath = path;}public void Writue(string section,string key, string value){// section=配置节,key=键名,value=键值,path=路径WritePrivateProfileString(section,key, value, sPath);}public string ReadValue(stringsection, string key){// 每次从ini中读取多少字节System.Text.StringBuilder temp =new System.Text.StringBuilder(255);// section=配置节,key=键名,temp=上面,path=路径GetPrivateProfileString(section,key, "", temp, 255, sPath);return temp.ToString();}}}开始调用函数。// 写入iniIni ini = newIni("C:/config.ini");ini.Writue("Setting","key1", "HELLO WORLD!");ini.Writue("Setting","key2", "HELLO CHINA!");// 读取iniIni ini = newIni("C:/config.ini");string str1 =ini.ReadValue("Setting", "key1");MessageBox.Show(str1);二,在一些小的应用中,有时候不需要使用数据困这样大规模的数据管理工具,也很少进行数据的查询、修改等操作,而仅用文件来存储数据。这时就需要使用。net中的文件操作对象,如file、streamReader、streamWriter等。1,使用File对象操作文件System.IO.File类提供了一系类的静态办法,完成对晚间的常用操作,如新建、删除、拷贝、移动等2,使用StreamWriter写入文件在System.IO空间中定义了一个文件写入器对象StreamWriter,使用它可以以一种特定的编码向输出流中(Stream)写入字符。3,使用SteamReader读取文件与streamWrite对应

『叁』 怎样编写Cmake的配置文件Cmakelist.txt

CMake是一个编译配置工具, 它是一个跨平台c/c++ 编译配置工具。可以通过编写CMakeLists.txt配置文件,可以控制生成的Makefile或者windows下工程文件。还支持安装(make install)、测试安装的程序是否能正确执行(make test,或者ctest)、生成当前平台的安装包(make package)、生成源码包(make package_source)、产生Dashboard显示数据并上传等高级功能,只要在CMakeLists.txt中简单配置,就可以完成很多复杂的功能,包括写测试用例。如果有嵌套目录,子目录下可以有自己的CMakeLists.txt。所以写好CMakeLists.txt 是使用好Cmake的关键工具/原料Cmake方法/步骤1Cmake 有linux ,windows 多个平台版本,如图是windows下版本程序怎样编写Cmake的配置文件Cmakelist.txt2更具一个简单多目录c项目,学下cmakelist.txt编写规范 3根目录下cmakelist文件内容:set(CMAKE_INSTALL_PREFIX):设置程序的安装目录,优先级比cmake命令参数设置高。add_subdirectory(编译文件子目录) 4libhello 目录下的cmakelist文件内容: 5libhello 目录下的cmakelist文件内容: 6查看编译后结果: 7安装后目录结构:

『肆』 标准C语言,修改配置文件

在1.1后面带空格只要该行数据的最大长度确定,在你这行数据确定后,内不足部分全部用空格覆盖。容比如ver=1.0.2现在的长度是9个字符,如果我确定这行内容不会超过12个字符,那后面的内容我可以全用空格覆盖,最后只要加个回车即可。不知道你所谓的以后改不方便指什么

『伍』 c语言读写配置文件

#include <stdio.h>#include <string.h>#define MAX_BUF 20#define SERVER "localhost"#define CONFIG_FILE "1.conf"bool SetAuthServer(char* strServerAdd){ char buf[MAX_BUF], tempBuf[MAX_BUF]; memset(buf, 0, MAX_BUF); memset(tempBuf, 0, MAX_BUF); FILE *pF = fopen(CONFIG_FILE, "r"); if(!pF) { printf("打开文件失败!\n"); return false; } fread(buf, MAX_BUF, 1, pF); if(!feof(pF)) { printf("读取不完整,请把MAX_BUF设置为大一点, 当前大小为: %d\n", MAX_BUF); fclose(pF); return false; } fclose(pF); char *lpPos = buf; char *lpNewPos = buf; while(lpNewPos = strstr(lpPos, SERVER)) { strncpy(tempBuf+strlen(tempBuf), lpPos, lpNewPos-lpPos); strcat(tempBuf, strServerAdd); lpPos = lpNewPos + strlen(SERVER); } strcat(tempBuf, lpPos); pF = fopen(CONFIG_FILE, "w"); if(!pF) { printf("打开文件失败!\n"); return false; } fwrite(tempBuf, strlen(tempBuf), 1, pF); fclose(pF); return true;}void main(){ char buf[20]; printf("请输入一个字符串来修改服务器配置: "); scanf("%s", buf); if(SetAuthServer(buf) == true) printf("修改成功!\n"); else printf("修改失败!\n"); }


赞 (0)