請問...我想要用C語言開啟一個檔案~讀取每一行的資料然後分別存到a,b,c變數里!!比如說檔案裡的3行數值: 100 200 300分別讓 a=100, b=200, c=300 要怎麼做?目前我只知道如何執行程式看檔案裡的資料但對怎麼抓每行資料到變數里~一直摸不著頭緒.....我是個新手~請求各位高手開導我一下....
#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){int a,b,c;printf("A=");scanf("%d",&a);printf("B=");scanf("%d",&b);printf("C=");scanf("%d",&c);printf("a=%d,b=%d,c=%d",a,b,c);system("PAUSE");return 0;}隨便打..我也正在學
要多學著自己查FILE IO在C++算是很基本的建議你可以上http://www.cppreference.com/wiki/看看fstream的用法!另外因為讀進來的字是字串而非數字需要做一個轉換的動作再去查這個istringstream (str)下面一點點小範例(是c++的)就寫檔、讀檔應該是有錯的,寫程式要強必須學會debug!!!#include<iostream>#include<fstream>using namespace std;MAX= 1024;int main(void){ofstream out;out.open("out.txt");out<<"output data"<<endl;out.close();out.clear();system("pause");ifstream in;in.open("In.txt");char line[MAX];while(in.getline(line,MAX)){cout<<line;}in.close();in.clear();system("pause");}