c#对txt文件的操作

2025-04-06 11:26:17
推荐回答(4个)
回答1:

using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
using
System.Runtime.InteropServices;
using
System.Text;
namespace
你的命令空间
{
class
RwIni
{
[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);
///


///
写入INI文件
///

///
name=^Section^>节点名称
///
name=^Key^>关键字
///
name=^Value^>值
///
name=^filepath^>INI文件路径
static
public
void
IniWriteValue(string
Section,
string
Key,
string
Value,
string
filepath)
{
WritePrivateProfileString(Section,
Key,
Value,
filepath);
}
///

///
读取INI文件
///

///
name=^Section^>节点名称
///
name=^Key^>关键字
///
name=^filepath^>INI文件路径
///

static
public
string
IniReadValue(string
Section,
string
Key,
string
filepath)
{
StringBuilder
temp
=
new
StringBuilder(255);
int
i
=
GetPrivateProfileString(Section,
Key,
^^,
temp,
255,
filepath);
return
temp.ToString();
}
}
}
保存为RwIni.cs
记得改命名空间
写INI
RwIni.IniWriteValue("类","参数","参数值","d:\\setting.txt")
读INI
string
str=RwIni.IniReadValue("类","参数","d:\\setting.txt");
str就是取到的参数值

回答2:

1.fopen的函数原型:FILE * fopen(const char * path,const char * mode);

  fopen函数的第一个参数是文件路径,第二个参数是打开方式,有以下几种方式:

r 以只读方式打开文件,该文件必须存在。

r+ 以可读写方式打开文件,该文件必须存在。

rb+ 读写打开一个二进制文件,允许读数据。

rw+ 读写打开一个文本文件,允许读和写。

w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。

w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。

a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)

a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 (原来的EOF符不保留)

wb 只写打开或新建一个二进制文件;只允许写数据。

wb+ 读写打开或建立一个二进制文件,允许读和写。

wt+ 读写打开或着建立一个文本文件;允许读写。

at+ 读写打开一个文本文件,允许读或在文本末追加数据。

ab+ 读写打开一个二进制文件,允许读或在文件末追加数据。

上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b 字符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。

    返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno中。


2.例程:

#include
#define F_PATH "d:\\myfile\\file.dat"
char c;
int main(){
    FILE*fp=NULL;//需要注意
    fp=fopen(F_PATH,"r");
    if(NULL==fp) return -1;//要返回错误代码
    while(fscanf(fp,"%c",&c)!=EOF) printf("%c",c); //从文本中读入并在控制台打印出来
    fclose(fp);
    fp=NULL;//需要指向空,否则会指向原打开文件地址    
    return 0;
}

回答3:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;

namespace 你的命令空间
{
class RwIni
{
[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);

///


/// 写入INI文件
///

/// 节点名称
/// 关键字
///
/// INI文件路径
static public void IniWriteValue(string Section, string Key, string Value, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
}

///
/// 读取INI文件
///

/// 节点名称
/// 关键字
/// INI文件路径
///
static public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, ^^, temp,
255, filepath);
return temp.ToString();

}
}
}

保存为RwIni.cs 记得改命名空间
写INI RwIni.IniWriteValue("类","参数","参数值","d:\\setting.txt")

读INI
string str=RwIni.IniReadValue("类","参数","d:\\setting.txt");

str就是取到的参数值

回答4:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string FileName = "F:\\M.txt";
string strInput = "";
string GetStream = "";

if (File.Exists(FileName))
{
StreamReader sr = new StreamReader(FileName, UnicodeEncoding.GetEncoding("gb2312"));
strInput = sr.ReadLine();
while (strInput != null)
{
GetStream += strInput + "
";
strInput = sr.ReadLine();

}
sr.Close();
Label1.Text = GetStream;
}
else
{
Label1.Text = "myFile.txt does not exist!";

}
}

}
}

以第行来读取.你可以把读取的数据放大数据组里.然后再作判断.就可以得到你想要的结果了