从文本文件到逐行读取的变量

时间:2017-07-14 09:00:01

标签: text getline

我想逐行读取文本文件内容并分配给我的变量。我怎样才能做到这一点 ?我会写 C ,不应该有' \ n'字符。

文字文件内容:

9600
502
N
1
8
N

变量

int     baudrate;
int     port;
char    parity;
char    databits;
char    stopbit;


while (fgets(line, sizeof(line), file)) {
    printf(line);
}

1 个答案:

答案 0 :(得分:1)

Specify the path of the file from where you would get the file in the path variable.

string path = '' // File path which needs to be read. 
string[] readText = File.ReadAllLines(path);

If the number of lines in the files is fixed and the variables in which you want the values to be saved remains the same it can be done as under : 

baudrate = Convert.ToInt32(readText[0]);
port = Convert.ToInt32(readText[1]);
parity = Convert.ToChar(readText[2]);
databits = Convert.ToChar(readText[3]);
stopbit= Convert.ToChar(readText[4]);