首页 新闻 搜索 专区 学院

C#读取txt

0
悬赏园豆:10 [已解决问题] 解决于 2012-06-07 09:53

我是新手,刚刚学习

执行下面语句,会得到一个4为字母的code,是按顺序增加的,现在能写入文本,记录abcd选项最后输出的CODE,但是结束语句,在执行还是从aaaa这开始,希望能从记录的地方开始

请高手指点,谢谢 最好讲的细点,查了好久都没结果

 

using System;
using System.IO;
using System.Text;

namespace code查询
{
class Program
{
private static string code1 = "";
private static string code2 = "";
private static string code3 = "";
private static string code4 = "";
private static int ge2 = 65;
private static int ge3 = 65;
private static int ge4 = 65;
private static int ge5 = 65;
private static int shi2 = 65;
private static int shi3 = 65;
private static int shi4 = 65;
private static int shi5 = 65;
private static int bai2 = 65;
private static int bai3 = 65;
private static int bai4 = 65;
private static int bai5 = 65;
static void Main(string[] args)
{
const string aname = "A";
const string bname = "B";
const string cname = "C";
const string dname = "D";
Console.WriteLine("\n请输入您需要的code的选项!\nA:医院code,B:药店code,C:经销商code,D:其它code,(输入错误会退出)");
string name;
exitPoint:
name = Console.ReadLine();
using (StreamWriter sw = new StreamWriter("TestFile.txt"))
{
sw.WriteLine("{0} ", code1);
sw.WriteLine("{0}", code2);
sw.WriteLine("{0} ", code3);
sw.WriteLine("{0} ", code4);
}

switch (name.ToUpper())
{
case aname:
{
情况1();
goto exitPoint;
}
case bname:
{
情况2();
goto exitPoint;
}
case cname:
{
情况3();
goto exitPoint;
}
case dname:
{
情况4();
goto exitPoint;
}
default:
Console.WriteLine("输入错误,请重新输入!");
goto exitPoint;
}
}

private static void 情况4()
{
if (ge5 == 91)
{
shi5++;
ge5 = 65;
}
if (shi5 == 91)
{
bai5++;
shi5 = 65;
}
char ge1 = Convert.ToChar(ge5);
char shi1 = Convert.ToChar(shi5);
char bai1 = Convert.ToChar(bai5);
string code = "Z" + bai1 + shi1 + ge1;
ge5++;
code4 = code;
Console.WriteLine("其它code: {0}", code4);
}

private static void 情况3()
{
if (ge4 == 91)
{
shi4++;
ge4 = 65;
}
if (shi4 == 91)
{
bai4++;
shi4 = 65;
}
char ge1 = Convert.ToChar(ge4);
char shi1 = Convert.ToChar(shi4);
char bai1 = Convert.ToChar(bai4);
string code = "V" + bai1 + shi1 + ge1;
ge4++;
code3 = code;
Console.WriteLine("经销商code: {0}", code3);
}

private static void 情况2()
{
if (ge3 == 91)
{
shi3++;
ge3 = 65;
}
if (shi3 == 91)
{
bai3++;
shi3 = 65;
}
char ge1 = Convert.ToChar(ge3);
char shi1 = Convert.ToChar(shi3);
char bai1 = Convert.ToChar(bai3);
string code = "P" + bai1 + shi1 + ge1;
ge3++;
code2 = code;
Console.WriteLine("药店code: {0}", code2);
}

private static void 情况1()
{
if (ge2 == 91)
{
shi2++;
ge2 = 65;
}
if (shi2 == 91)
{
bai2++;
shi2 = 65;
}
char ge1 = Convert.ToChar(ge2);
char shi1 = Convert.ToChar(shi2);
char bai1 = Convert.ToChar(bai2);
string code = "A" + bai1 + shi1 + ge1;
ge2++;
code1 = code;
Console.WriteLine("医院code: {0}", code1);
}
}
}

C#
夏天online的主页 夏天online | 初学一级 | 园豆:193
提问于:2012-05-21 09:12
< >
分享
最佳答案
0

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
private static string code1 = "";
private static string code2 = "";
private static string code3 = "";
private static string code4 = "";
private static int ge2 = 65;
private static int ge3 = 65;
private static int ge4 = 65;
private static int ge5 = 65;
private static int shi2 = 65;
private static int shi3 = 65;
private static int shi4 = 65;
private static int shi5 = 65;
private static int bai2 = 65;
private static int bai3 = 65;
private static int bai4 = 65;
private static int bai5 = 65;
static void Main(string[] args)
{

const string aname = "A";
const string bname = "B";
const string cname = "C";
const string dname = "D";
exitPoint:
Console.WriteLine("\n请输入您需要的code的选项!\nA:医院code,B:药店code,C:经销商code,D:其它code");
string name;
name = Console.ReadLine();
switch (name.ToUpper())
{
case aname:
{
qk1();
goto exitPoint;
}
case bname:
{
qk2();
goto exitPoint;
}
case cname:
{
qk3();
goto exitPoint;
}
case dname:
{
qk4();
goto exitPoint;
}
default:
Console.WriteLine("输入错误,请重新输入!");
goto exitPoint;
}
}

private static void qk4()
{
TXTcls txt = new TXTcls();
StringBuilder temp = new StringBuilder();
txt.ReadConfig("D", "D", "", temp, 255, ".\\TestFile.ini");

if (temp.Length != 0)
{
ge5 = Convert.ToInt32(temp[3]);
shi5 = Convert.ToInt32(temp[2]);
bai5 = Convert.ToInt32(temp[1]);
ge5++;
}
if (ge5 == 91)
{
shi5++;
ge5 = 65;
}
if (shi5 == 91)
{
bai5++;
shi5 = 65;
}
char ge1 = Convert.ToChar(ge5);
char shi1 = Convert.ToChar(shi5);
char bai1 = Convert.ToChar(bai5);
string code = "Z" + bai1 + shi1 + ge1;
txt.Writeconfig("D", "D", code, ".\\TestFile.ini");
code4 = code;
Console.WriteLine("其它code: {0}", code4);
}

private static void qk3()
{
TXTcls txt = new TXTcls();
StringBuilder temp = new StringBuilder();
txt.ReadConfig("C", "C", "", temp, 255, ".\\TestFile.ini");

if (temp.Length != 0)
{
ge4 = Convert.ToInt32(temp[3]);
shi4 = Convert.ToInt32(temp[2]);
bai4 = Convert.ToInt32(temp[1]);
ge4++;
}
if (ge4 == 91)
{
shi4++;
ge4 = 65;
}
if (shi4 == 91)
{
bai4++;
shi4 = 65;
}
char ge1 = Convert.ToChar(ge4);
char shi1 = Convert.ToChar(shi4);
char bai1 = Convert.ToChar(bai4);
string code = "V" + bai1 + shi1 + ge1;
txt.Writeconfig("C", "C", code, ".\\TestFile.ini");
code3 = code;
Console.WriteLine("经销商code: {0}", code3);
}

private static void qk2()
{
TXTcls txt = new TXTcls();
StringBuilder temp = new StringBuilder();
txt.ReadConfig("B", "B", "", temp, 255, ".\\TestFile.ini");

if (temp.Length != 0)
{
ge3 = Convert.ToInt32(temp[3]);
shi3 = Convert.ToInt32(temp[2]);
bai3 = Convert.ToInt32(temp[1]);
ge3++;
}
if (ge3 == 91)
{
shi3++;
ge3 = 65;
}
if (shi3 == 91)
{
bai3++;
shi3 = 65;
}
char ge1 = Convert.ToChar(ge3);
char shi1 = Convert.ToChar(shi3);
char bai1 = Convert.ToChar(bai3);
string code = "P" + bai1 + shi1 + ge1;
txt.Writeconfig("B", "B", code, ".\\TestFile.ini");
code2 = code;
Console.WriteLine("药店code: {0}", code2);
}

private static void qk1()
{
TXTcls txt = new TXTcls();
StringBuilder temp = new StringBuilder();
txt.ReadConfig("A", "A", "", temp, 255, ".\\TestFile.ini");

if (temp.Length != 0)
{
ge2 = Convert.ToInt32(temp[3]);
shi2 = Convert.ToInt32(temp[2]);
bai2 = Convert.ToInt32(temp[1]);
ge2++;
}

if (ge2 == 91)
{
shi2++;
ge2 = 65;
}
if (shi2 == 91)
{
bai2++;
shi2 = 65;
}
char ge1 = Convert.ToChar(ge2);
char shi1 = Convert.ToChar(shi2);
char bai1 = Convert.ToChar(bai2);
string code = "A" + bai1 + shi1 + ge1;
txt.Writeconfig("A", "A", code, ".\\TestFile.ini");
code1 = code;
Console.WriteLine("医院code: {0}", code1);
}
}
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
class TXTcls
{
[DllImport("kernel32")]
private static extern bool 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);
public TXTcls()
{

}
public bool Writeconfig(string senction, string key, string val, string filepath)
{
if (WritePrivateProfileString(senction, key, val, filepath))
{
return true;
}
else
{
return false;
}

}
public int ReadConfig(string section, string key, string def, StringBuilder retval, int size, string filepath)
{
return GetPrivateProfileString(section, key, def, retval, size, filepath);

}
}
}

 

已解决

夏天online | 初学一级 |园豆:193 | 2012-05-23 11:39
其他回答(4)
0

seek方法好像可以。

收获园豆:4
悟行 | 园豆:12240 (专家六级) | 2012-05-21 09:34

怎么用  求帮助  

支持(0) 反对(0) 夏天online | 园豆:193 (初学一级) | 2012-05-21 10:47
0

语句有问题   死循环   goto exitPoint;

收获园豆:2
路边的油条 | 园豆:313 (菜鸟二级) | 2012-05-21 09:57

default:
Console.WriteLine("输入错误,请重新输入!");
goto exitPoint;把这个goto改成dreak就好了

支持(0) 反对(0) 夏天online | 园豆:193 (初学一级) | 2012-05-21 17:51

打错  break

支持(0) 反对(0) 夏天online | 园豆:193 (初学一级) | 2012-05-21 17:52

@、習慣就好″: 恩恩 

支持(0) 反对(0) 路边的油条 | 园豆:313 (菜鸟二级) | 2012-05-21 17:54
0

switch()循环怎么没有break跳出,循环语句啊

收获园豆:2
danni | 园豆:124 (初学一级) | 2012-05-21 16:30

因为用了goto就不用break了

支持(0) 反对(0) 夏天online | 园豆:193 (初学一级) | 2012-05-21 17:50
0

goto 名声不好,而且很容易用乱。代码我在仔细看看

收获园豆:2
K战神 | 园豆:93 (初学一级) | 2012-05-23 11:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册