#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <string.h>
using namespace std;
#define N 100
class File{
public:
void LodeFile(const string& path);
};
void File::LodeFile(const string& path)
{
FILE *p = fopen(path.c_str(),"rb");
if(!p)
exit(0);
char str[N+1];
strcpy(str,path.c_str());
//{
// printf("%s",str);
//}
//循环读取每一行shuju
while(fgets(str,N,p)!= NULL)
{
cout<<str<<"666"<<endl;
printf("%s",str);
}
printf("xx");
fclose(p);
}
int main()
{
File file;
file.LodeFile("/home/xl/demo/1.txt");
printf("1111");
return 0;
}
运行结果
-VirtualBox:~/demo/file$ ./fopen
12345aaa asd
666
12345aaa asd
其中1.txt为已经创建的文件,在c++中,因为没有fopen需要用path.c_str()来转换,然后用strcpy来拷贝