C#怎么来读写数据?
在C里面可以用fwrite()和fread()来读写数据到,如下面所示:
%%%%%%%%%%%%%%%%%%%%%%%写数据%%%%%%%%%%%%%%%%%%%%%%%%
fpcw = fopen("parameter.dat","wb");
fwrite(&RanNum,sizeof(int),1,fpcw);
fwrite(&AziNum,sizeof(int),1,fpcw);
fwrite(&Kr,sizeof(double),1,fpcw);
fwrite(&Rtc,sizeof(double),1,fpcw);
fwrite(&Rrc,sizeof(double),1,fpcw);
fclose(fpcw);
%%%%%%%%%%%%%%%%%%%%%%%%读数据%%%%%%%%%%%%%%%%%%%%%%%
parameter= fopen("parameter.dat","rb");
fread(&RanNum,sizeof(int),1,parameter);
fread(&AziNum,sizeof(int),1,parameter);
fread(&Kr,sizeof(double),1,parameter);
fclose(parameter);
请问在C#里面怎么实现类似的功能呢,就是先把数据写到一个.dat文件,然后要用的时候可以直接读取?如上面两段代码所示。如果可以的话希望能给些简单的代码示例。谢谢。
------解决方案--------------------写文件
StreamWriter sw = new StreamWriter(fileName);
sw.Write(textBox1.Text);
sw.Close();
读文件
StreamReader sr = new StreamReader(fileName);
textBox1.Text = sw.ReadToEnd();
sr.Close();
------解决方案--------------------
------解决方案--------------------
//创建文件
FileStream fs;
string filename = "Data\\" + title + ".txt";
fs = File.Create(filename);
string content = richTextBox1.Text;
//写入之前把字符串按照默认编码方式转换为数组
byte[] str = System.Text.Encoding.Default.GetBytes(content);
fs.Write(str,0,str.Length);
fs.Flush();
fs.Close();
//读取文件内容,无乱码
string title = treeView1.SelectedNode.Text;
string text = "Data\\" + title + ".txt";
StreamReader sr = new StreamReader(text, System.Text.Encoding.Default);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();