c中string用法及文件訪問

#include <iostream>
using namespace std;

int main()
{
 int a[300];
 FILE *stream;

 string str="D:\\new.tx";

 string fileFullName=str; 
 fileFullName.append("t");

//轉換爲char類型
 const char *c_file=fileFullName.c_str();
 //const char *s=str.c_str();
 if ((stream=fopen(c_file,"r"))!=NULL)
 {
  fread(a,1,5,stream);

 }
 else
 {
  printf("can't open file");
  exit(1);


 }
 fclose(stream);

 

 

 for(int i=0;i<5;i++)
 {
  cout<<a[i]<<endl;

 }
 //cout<<s<<endl;
 cout<<c_file;


 system("pause");

 return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章