16,471
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
FILE *f;
int c;
long offset;
char ln[100];
void main() {
f=fopen("a.txt","rb");//必须用b方式打开,否则文件当前读位置会受\x0d \x0a \x1a等字符的干扰
fseek(f,-1,SEEK_END);//倒数最后一个字节之前
while (1) {
c=fgetc(f);
if ('\n'==c) {
offset=ftell(f);//记住当前位置,即刚读字符后面
fgets(ln,100,f);
printf("%s",ln);
fseek(f,offset-2,SEEK_SET);//回到刚才读位置之前一个位置,即c所在位置之前一个位置
} else
if (fseek(f,-2,SEEK_CUR)) {//回到刚才读位置之前一个位置,即c所在位置之前一个位置失败,说明已读到文件开头
fseek(f,0,SEEK_SET);
fgets(ln,100,f);//读文件第一行内容
printf("%s",ln);
break;
}
}
fclose(f);
}
char* pszFileName="C:\\myfile.txt";
CStdioFile myFile;
CFileException fileException;
if(myFile.Open(pszFileName,CFile::typeText|CFile::modeReadWrite),&fileException)
{
myFile.SeekToBegin();
CString str1="";
CString str2="";
while(myFile.ReadString(str1))
{
str2+=str1;
}
myFile.Close();