要讀取的檔案:
逐個字元讀取:
方式一:>>讀取
#include#includeusing namespace std;
int main()
//infile >> noskipws; //強制讀入每乙個字元,包括空格、轉折
while (infile >> c[i])
cout << endl;
c[i] = '\0'; //注意,保證strlen(c)的值正確
cout << "length:" << strlen(c) << endl;
cout << c << endl;
infile.close();
return 0;
}
方式二:判斷eof符號
#include#includeusing namespace std;
int main()
//infile >> noskipws; //強制讀入每乙個字元,包括空格、轉折
while (!infile.eof())
cout << endl;
c[--i] = '\0'; //注意,此時 i 必須先減1,才能保證strlen(c)的值正確
cout << "length:" << strlen(c) << endl;
cout << c << endl;
infile.close();
return 0;
}
以上均可以替換成
#includevector《型別》 c;
同一型別 x;
while(infile>>x)
for(int i=0;i逐行讀取:
#include#include#includeusing namespace std;
int main()
//方式一:
string str;
while (getline(infile, str))
/*//方式二:
string str;
while (infile>>str)
*//*
//方式三:
string str;
while (!infile.eof())
*/ infile.close();
return 0;
}
C C 讀取檔案的多種方式
int fopentest while 1 printf c c fclose file return 0 include include int main char buf 1024 while 1 printf s n buf fclose fp return 0 windows include...
C 檔案讀取方式
1 使用file讀取 file fp tfopen szxmlfilepath,l rb if fp null return fseek fp,0,seek end uint nlen ftell fp fseek fp,0,seek set 寬字元型別 wchar t pstr read new ...
C 多種方式獲取檔案路徑
string str1 process.getcurrentprocess mainmodule.filename 可獲得當前執行的exe的檔名。string str2 environment.currentdirectory 獲取和設定當前目錄 即該程序從中啟動的目錄 的完全限定路徑。備註 按照定...