2017-07-14 94 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]);