如何创建in文件

如何创建in文件,第1张

建立in.dat文件程序:

#include <stdio.h>

#include <stdlib.h>

void main(void)

{

FILE *fp = fopen("in.dat", "wb+")

fprintf(fp, "%04d\r\n", rand())

fclose(fp)

}

你所需要的程序架改铅腊构激敏如下:(这样你就知道out.dat是的生核滑成过程)

...

FILE *fp = fopen("out.dat", "wb+")

...

fprintf(fp, ...)

...

fclose(fp)

...

public class IniOP

{

#region 声明读写INI文件的API函数

[DllImport("kernel32")]

private static extern long WritePrivateProfileString(string section, string key, string val, string filePath)

[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath)

#endregion

#region 变量声明

//初始化路径名,主键名,字符串长度,这里需要加一个说明,在对应函数才需要做类似 *** 作,初始值应该为空

private string m_strFilePath = ""

private string m_strKey = ""

private string m_strSubKey = ""

private int m_intSize = 255

private string _strFilePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"弊帆\Default.ini"

private string _strKey = "ErrorKeyIsNull"

private string _strSubKey = "ErrorSubKeyIsNull"

#endregion

#region 属性设置

//设置路径属性

public string FilePath { get { return m_strFilePath} set { m_strFilePath = value} }

//设置主键名属性

public string Key { get { return m_strKey} set { m_strKey = value} }

//设置字符串长度属性

public int Size { get { return m_intSize} set { m_intSize = value} }

//设置子慧饥键名属性

public string SubKey { get { return m_strSubKey} set { m_strSubKey = value} }

//版本信息

public static string Version

{

get

{

return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()

}

}

#endregion

#region 构造函数

//构造函数,无参数

public IniOP()

{

}

//构造函数,路径名

public IniOP(string strPath)

{

m_strFilePath = strPath

}

//构造函数,路径租碧雹名,主键名

public IniOP(string strPath, string strKey)

{

m_strFilePath = strPath

m_strKey = strKey

}

//构造函数,路径名,主键名,字符串长度

public IniOP(string strPath, string strKey, int intSize)

{

m_strFilePath = strPath

m_strKey = strKey

m_intSize = intSize

}

//构造函数,路径名,主键名,字符串长度,子键名

public IniOP(string strPath, string strKey, int intSize, string strSubKey)

{

m_strFilePath = strPath

m_strKey = strKey

m_intSize = intSize

m_strSubKey = strSubKey

}

#endregion

#region 写入ini文件时的默认值判断

private void IniCheckDefaultValue()

{

if (m_strFilePath == "" || System.IO.File.Exists(m_strFilePath) == false)

{

_strFilePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Default.ini"

}

else

{

_strFilePath = m_strFilePath

}

if (m_strKey == "")

{

_strKey = "ErrorKeyIsNull_" + System.DateTime.Now.ToString("yyyyMMddHHmmss")

}

else

{

_strKey = m_strKey

}

if (m_strSubKey == "")

{

_strSubKey = "ErrorSubKeyIsNull_" + System.DateTime.Now.ToString("yyyyMMddHHmmss")

}

else

{

_strSubKey = m_strSubKey

}

}

#endregion

#region 写入文件函数(非静态)

//写INI文件,输入键值

public void WriteValue(string strValue)

{

IniCheckDefaultValue()

WritePrivateProfileString(_strKey, _strSubKey, strValue, _strFilePath)

}

//写INI文件,输入子键名,键值

public void WriteValue(string strSubKey, string strValue)

{

IniCheckDefaultValue()

WritePrivateProfileString(_strKey, strSubKey, strValue, _strFilePath)

}

//写INI文件,输入主键名,子键名,键值

public void WriteValue(string strKey, string strSubKey, string strValue)

{

IniCheckDefaultValue()

WritePrivateProfileString(strKey, strSubKey, strValue, _strFilePath)

}

//写INI文件,输入主键名,子键名,文件路径,键值

public void WriteValue(string strKey, string strSubKey, string strPath, string strValue)

{

WritePrivateProfileString(strKey, strSubKey, strValue, strPath)

}

#endregion

#region 写入文件函数(静态)

//写INI文件,输入主键名,子键名,文件路径,键值

public static void staticWriteValue(string strKey, string strSubKey, string strPath, string strValue)

{

WritePrivateProfileString(strKey, strSubKey, strValue, strPath)

}

#endregion

#region 读取文件函数(非静态)

//读取INI文件,所有参数提前设置好

public string ReadValue()

{

StringBuilder strTemp = new StringBuilder(m_intSize)

int i = GetPrivateProfileString(m_strKey, m_strSubKey, "", strTemp, m_intSize, m_strFilePath)

return strTemp.ToString()

}

//读取INI文件,输入子键名

public string ReadValue(string strSubKey)

{

StringBuilder strTemp = new StringBuilder(m_intSize)

int i = GetPrivateProfileString(m_strKey, strSubKey, "", strTemp, m_intSize, m_strFilePath)

return strTemp.ToString()

}

//读取INI文件,输入主键名,子键名

public string ReadValue(string strKey, string strSubKey)

{

StringBuilder strTemp = new StringBuilder(m_intSize)

int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, m_intSize, m_strFilePath)

return strTemp.ToString()

}

//读取INI文件,输入主键名,子键名,文件路径

public string ReadValue(string strKey, string strSubKey, string strPath)

{

StringBuilder strTemp = new StringBuilder(m_intSize)

int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, m_intSize, strPath)

return strTemp.ToString()

}

//读取INI文件,输入主键名,子键名,取字符串长度

public string ReadValue(string strKey, string strSubKey, int intSize)

{

StringBuilder strTemp = new StringBuilder(intSize)

int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, m_strFilePath)

return strTemp.ToString()

}

//读取INI文件,输入主键名,子键名,文件路径,取字符串长度

public string ReadValue(string strKey, string strSubKey, string strPath, int intSize)

{

StringBuilder strTemp = new StringBuilder(intSize)

int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, strPath)

return strTemp.ToString()

}

#endregion

#region 读取文件函数(静态)

//读取INI文件,输入主键名,子键名,文件路径,取字符串长度

public static string staticReadValue(string strKey, string strSubKey, string strPath, int intSize)

{

StringBuilder strTemp = new StringBuilder(intSize)

int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, strPath)

return strTemp.ToString()

}

#endregion

}

//*****************************

// 以上是类的部分,只要指定文件名和路径写入,如果没有文件就会自动创建

//*****************************

请采纳

记事本 文件另存为 保存类型选择 “所有文件”, 文件名歼唤天 XXX.in ,不改保存类型 即使你填文件名 XXX.in

实际庆灶保存的是 XXX.in.txt

方法二 保存类氏差凯型 用默认的 *.txt ,填文件名时 文件名前后 加 英文半角 双引号 即 【 "XXX.in" 】的【 】内的

你可以去试试 fopen 的 w 方式创建文件了 亲


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存