操纵和编辑txt文件 [英] Manipulate and editing txt file

查看:76
本文介绍了操纵和编辑txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想创建一个带.txt文件的基本数据库。我使用c#smart device visual studio 2008构建一个应用程序。

i有一个包含3个字段文本框的表单。

每个文本框代表一个要设置的值。

例如textbox1.text =ID_14textbox2.txt =AER,textbox3.txt =men

我想按下按钮时我想要的。它会生成一个包含文本框中数据的txt文件。这种格式。



ID_14 :; AER;男士。



如果我添加其他信息示例

textbox1.text =ID_15textbox2.txt =AEl,textbox3.txt =women

i添加我上一个txt文件和输出的信息将是这样的



ID_14:; AER;男人。

ID_15 :; AEl;女性。



这是第一步。



和第二步

i想要打开这个数据库读取它和编辑最后的信息,例如,如果我想在保存后更改性别或身份。



i不知道怎么做。请帮助



我尝试过:



你好,那里,我想用.txt文件创建一个基本数据库。我使用c#smart device visual studio 2008构建一个应用程序。

i有一个包含3个字段文本框的表单。

每个文本框代表一个要设置的值。

例如textbox1.text =ID_14textbox2.txt =AER,textbox3.txt =men

我想按下按钮时我想要的。它会生成一个包含文本框中数据的txt文件。这种格式。



ID_14 :; AER;男士。



如果我添加其他信息示例

textbox1.text =ID_15textbox2.txt =AEl,textbox3.txt =women

i添加我上一个txt文件和输出的信息将是这样的



ID_14:; AER;男人。

ID_15 :; AEl;女性。



这是第一步。



和seconde步骤

i想要打开这个数据库读取它和编辑最后的信息,例如,如果我想在保存后更改性别或身份。



i不知道怎么做。请帮助

Hello, there , i want to creat a basic database with .txt file. i build an application with c# smart device visual studio 2008.
i have a form that contain 3 fields of textbox.
every textbox represent a value to set.
for example textbox1.text= "ID_14" textbox2.txt="AER",textbox3.txt="men"
me i want when i hit the button. it will generate me a txt file that contain data that was in textbox. in this format.

ID_14: ;AER ;men.

and if i add other information for example
textbox1.text= "ID_15" textbox2.txt="AEl",textbox3.txt="women"
i add those information of my last txt file and the output will be like these

ID_14: ;AER ;men.
ID_15: ;AEl ;women.

this is the first step.

and the second step
i want to open this database read it and to edit last information, for example if i want to change the sex or the id after i save it.

i have no idea ho to do that. please help

What I have tried:

Hello, there , i want to creat a basic database with .txt file. i build an application with c# smart device visual studio 2008.
i have a form that contain 3 fields of textbox.
every textbox represent a value to set.
for example textbox1.text= "ID_14" textbox2.txt="AER",textbox3.txt="men"
me i want when i hit the button. it will generate me a txt file that contain data that was in textbox. in this format.

ID_14: ;AER ;men.

and if i add other information for example
textbox1.text= "ID_15" textbox2.txt="AEl",textbox3.txt="women"
i add those information of my last txt file and the output will be like these

ID_14: ;AER ;men.
ID_15: ;AEl ;women.

this is the first step.

and the seconde step
i want to open this database read it and to edit last information, for example if i want to change the sex or the id after a save it.

i have no idea ho to do that. please help

推荐答案

整个问题是如此尴尬,不容易实现,所以很难给出全面的建议。但是,实现数据库存储的想法可能是一个有用的练习。 (也许一个目标是开发对现有数据库系统的欣赏。:-))



问题是:看起来你试图使用纯文本文件其中一条线代表一些记录。通常,它对于数据库存储来说是非常糟糕的介质,因为这些行可以具有不同的长度。想象一下,您需要修改靠近文件开头的一条记录。如果修改后的记录具有不同的行长度,则必须重写文件的其余部分,因为每个字符都将被移位。你无能为力:一般情况下,你必须读取整个文件,然后全部写,或者很大一部分,1 /平均文件大小为2。你甚至不能在阅读时跳过文件的任何部分,因为你永远不知道每一行的长度。



一种解决方案是创建另一个文件。你可以称之为索引文件。在其最简单的形式中,它可用于存储主文件中每行的位置,以及每个记录的长度。而且,您可能需要有几个索引。例如,一个索引将按一个字段排序,另一个索引按另一个字段排序。然后主要读数将通过在给定位置偷看线​​来完成。但是,修改的问题将保持不变:您需要重写文件的其余部分;另外,你应该保持索引文件和主文件之间的完整性。



请注意,如果这是一种关系型数据库,我们只讨论一个由文件。在关系模型中,你需要有多个表,键,外键,所有这些东西。



无论如何,单个表的另一种方法是固定大小记录。假设您在表格元数据中设置了所有数据类型的固定大小。字符串特别痛苦:对于每个属性,您需要有长度限制。使用固定大小的行,您甚至不需要行尾字符。您只需通过将记录数乘以记录大小来查看记录,转到文件中的此位置并读/写它,而无需重写文件的其余部分。但是弦长的限制是严苛的...



嗯,只是一些值得思考的东西...



< dd> -SA
The whole problem is so awkward and not easy to implement, so it's hard to give a comprehensive advice. However, the idea of implementation of the database storage might be a useful exercises. (Perhaps one goal would be to develop the appreciation of existing database systems. :-))

The problem is: it looks like you try to use a plain text file where a line represents some record. Generally, it's a very bad media for the database storage, because the lines can be of different lengths. Imagine you need to modify one record close to the beginning of the file. If the modified record makes different length of the line, you have to rewrite the rest of the file, because each and every character will be shifted. There is nothing you can do about it: in general case, you have to read the whole file, and write it all back, or a big part of it, 1/2 of the file size on average. You cannot even skip any part of the file on reading, because you never know the length of each line.

One solution is to create another file. You can call it "index file". In its simplest form, it can be used to store the position of each line in the main file, as well as the length of each record. Moreover, you may need to have several indices. For example, one index would be sorted by one field, another by another field. Then the main reading will be done via peeking the line at given position. However, the problem with modification will remain the same: you will need to rewrite the rest of the file; also, you should keep integrity between index files and main file.

Note that if this is a kind of relational database, we are only discussing a single table represented by the file. In relational model, you would need to have multiple table, keys, foreign keys, all that stuff.

Anyway, another approach for a single table would be fixed-size records. Let's say, in a table metadata you setup fixed sizes of all data types. It is especially painful for strings: for each attribute, you will need to have length limit. With fixed-size lines, you don't even need the end-of-line characters. You just peek a record by multiplying the record number by record size, go to this position in file and read/write it, without rewriting the rest of the file. But the limitation of string lengths is draconian…

Well, just some food for thought…

—SA


执行此操作的最佳方法是创建一个包含这些详细信息的对象类,将它们绑定到文本框并使用序列化将它们保存到支持商店。每个问题都有很多样本可以告诉你如何操作。
The best way to do this would be to create a class of objects that hold those details, bind them to the textboxes and use serialisation to save them to backing store. There are many samples of each issue that will show you how to do it.


这篇关于操纵和编辑txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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