简体   繁体   English

读取文件中的特定行/整数并转换为其他C#

[英]Reading a specific line/int in a file and converting to something else C#

I want to read a file, at a specific location/line (which will always remain the same) and convert it to a string. 我想在特定位置/行(始终保持不变)读取文件,然后将其转换为字符串。 For ex: read line 3 (which will contain numbers -3,-2, 2, 4, 7, 10 OR 12 ) and convert it to F,D,C,B,B-,A, OR A+ . 例如:读第3行(它将包含数字-3,-2、2、4、7、10 12 )并将其转换为F,D,C,B,B-,A A + Probably I will use an if statement to tell the app which number should convert to which letter, so you can skip this part. 可能我将使用if语句告诉应用程序哪个数字应转换为哪个字母,因此您可以跳过此部分。 To specify, I know how to read a file, but I need to know how to read at that specific location and convert it right away and print it. 要指定,我知道如何读取文件,但是我需要知道如何在该特定位置读取并立即将其转换并打印。

For reading the file at specific position look at here . 要在特定位置读取文件,请查看此处

For converting to string you can do this: 要转换为字符串,您可以执行以下操作:

Dictionary<string, string> matches = new Dictionary<string, string>()
{
   {"-3", "F"}, {"-2", "D"}, {"2", "C"}, {"4", "B"}, ...
};

now conversion would be like: 现在转换将是:

 string result = "notfound";
 result = matches.TryGetValue(valueYouHaveRead, out result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM