在文件上读写数据 [英] read and write data on file

查看:85
本文介绍了在文件上读写数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些数据存储在稍后将在程序中使用的文本文件中。数据包含每个记录上的一个sting数据和一个数字数据。它只需要存储一条记录。你可以告诉我>>如何从不同的文本框中存储不同的数据,并在不同的变量中读取和输入。

I want to store some data on text file which will be used in the program later. Data contains one sting data and one numeric data on each record. It needs to store only a single record. Can u tell me >> How can I store different data from different text boxes and read and input in different variables.

推荐答案

这不是一件容易的事,你必须先考虑你的数据。如果您的字符串可以包含任何字符,那么您需要先输入数字,然后是分隔符以显示结束的位置,然后是字符串值:

It''s not trivial, you have to think about your data first. If your string can contain any character, then you need to put the number first, followed by a delimiter to show where it ends, then the string value:
string writeMe = string.Format("{0};{1}", numericValue, stringValue);

将生成一个字符串以安全地写入文件。

然后您可以将其读回来,并再次将其转换回可用值:

will genare a string to write to the file safely.
You can then read it back, and convert it back to usable values again:

string input = "12345.67;This is the text part";
int sep = input.IndexOf(';');
double numericValue = double.Parse(input.Substring(0, sep));
string strVal = input.Substring(sep + 1);


这篇关于在文件上读写数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆