64,651
社区成员
发帖
与我相关
我的任务
分享
//
// main.cpp
// File
//
// Created by apple on 15/4/15.
// Copyright (c) 2015年 BraveTang. All rights reserved.
//
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class File{
string name;
ifstream fin;
public:
File(const string name){
//fstream fin;
fin.open(name);
if(!fin.is_open())
{
fin.clear();
fin.open(name,ios::out);
fin.close();
fin.open(name);
}
cout << "文件已打开" << endl;
}
~File(){
fin.close();
cout << "文件已关闭" << endl;
}
void displayFile()
{
// fin.seekg(0,fin.end); //设置当前文件位置指针相对于文件末尾的位置。
// long length = fin.tellg(); //获得当前文件位置指针的位置,即文件长度。
// fin.seekg(0,fin.beg); //设置当前文件位置指针到文件首部
// string str;
// str.resize(length, ' '); //初始化字符串
// char* begin = &*str.begin();//
// fin.read(begin, length);
// //cout << str ;
// fin.seekg(0, fin.beg); //整个文件一起读
long pos1= 0,pos2; //分块读
char buf[100];
bool ok = true;
while (ok) {
fin.seekg(pos1);
fin.read(buf, sizeof(buf));
if (!fin) {
fin.clear();
ok = false;
}
cout.write((char*)buf, fin.gcount());
pos2= fin.tellg();
pos1 =pos2;
}
fin.seekg(0);
}
void getLine(char* buf)
{
printf("buf = %p\n",buf);
//char buff[100];
// cout << "buff= " << &buff << endl;
cout << "***buf size:" << sizeof(buf) << endl; //sizeof[buf] ==8;
fin.getline(buf,sizeof(buf),'\n');
cout << "****buf= " << &buf << endl;
fin.clear();
//cout << "gcout = " << fin.gcount() << endl;
}
};
int main(int argc, const char * argv[]) {
// insert code here...
File f("jun.txt");
char buf1[100];
cout << "buf1= " << &buf1 << endl;
cout << "buf1 size:" << sizeof(buf1) << endl;//sizeof(buf1)== 100
f.getLine(buf1);
f.getLine(buf1);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char a[100];
char *b;
cout<<"sizeof a="<<sizeof a<<endl;
cout<<"sizeof(char[100])="<<sizeof(char[100])<<endl;
cout<<"sizeof b="<<sizeof b<<endl;
cout<<"sizeof(char*)="<<sizeof(char*)<<endl;
return 0;
}
/*
输出:
sizeof a=100
sizeof(char[100])=100
sizeof b=8
sizeof(char*)=8
*/
你代码中的buf1的类型是char[100],buf的类型是char*,这两个的大小不一样。