2017-07-14 81 views
0

我想逐行读取文本文件内容并分配给我的变量。我怎样才能做到这一点 ?我会写在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); 
} 
+0

指定该语言。 –

回答

0
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[0]); 
parity = Convert.ToChar(readText[0]); 
databits = Convert.ToChar(readText[0]); 
stopbit= Convert.ToChar(readText[0]);