cfile设置文件结尾|C语言里面的二进制文件的结束标志是什么

1. VC写文件

方法1: try{ CFile f; f.Open("D:/test.txt",CFile::modeWrite); // char myBuf[1024]={'1','2','3','4'};//初始化4个 //memset(myBuf, '0', sizeof(txtBuf)); f.SeekToEnd();//用来标注写入位置在最后 即在末尾写入不覆盖原有内容 // f.Write(&myBuf,4);//sizeof(myBuf)或1024或大于4会出现许多空格 f.Write("the end",sizeof("the end")); f.Flush(); f.Close(); }catch(…){ MessageBox("未成功写入"); }方法2:使用CStdioFile CStdioFile 为CFile子类故可使用CFile所有方法 CStdioFile sf; CString str="abcdef12345gg"; sf.Open("D:/test.txt",CFile::modeWrite); sf.SeekToEnd(); sf.WriteString("1234567890");//CStdioFile 的方法 sf.Flush(); sf.Close();备注:1:方法2 未加try catch 建议加上 2:方法1 先不要理开头有//的行,那是我用来测试的 3: c++兼容c 可以试一试fopen fwrite等方法 本以为很简单,有点高估自己啦,自己反倒学了不少知识

2. CFile怎样判断是否读到文件结束了

1,先获取内容长度,在搜索\r\n得到最后一行位置,在把指针调整到那里。2,读文件版是有个文件指针,调用权Read时文件指针制动向前移动。3,下次再调用是是接着上次文件指针读。mile.Read(sRead,2),第二个参数指定了最多读几个字符,自己指定了读两个字符。例如,CFile fr;…//打开文件char readbuf[65535];int n;while((n = fr.Read(readbuf, 65535)) > 0)//读文件的长度大于0,说明文件没有结束,否则已经结束了{//操作}

3. 怎么在文件末尾写数据我用CFile::SeekToEnd好像不行。

打开文件的方式中,有一种是在文件尾,写新的数据——————————-MFC的话,你看是不是用这个CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Furthermore, if the file being created already exists, the underlying file pointer will point to the beginning of the file. This flag guarantees the file to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.

4. C语言里面的二进制文件的结束标志是什么

1、可以使用feof函数来判断二进制文件是否结束。

EOF是文本文件结束的标志。在文本文件中,数据是以字符的ASCⅡ代码值的形式存放,普通字符的ASCⅡ代码的范围是32到127(十进制),EOF的16进制代码为0xFF(十进制为-1),因此可以用EOF作为文件结束标志。当把数据以二进制形式存放到文件中时,就会有-1值的出现,因此不能采用EOF作为二进制文件的结束标志。为解决这一个问题,ASCI C提供一个feof函数,用来判断文件是否结束。feof函数既可用以判断二进制文件又可用以判断文本文件。2、例程:

#include<stdio.h>intmain(void){FILE*stream;/*openafileforreading*/stream=fopen("DUMMY.FIL","r");/*readacharacterfromthefile*/fgetc(stream);/*checkforEOF*/if(feof(stream))//使用feof函数检查是否到达文件末尾printf("Wehavereachedendoffile");/*closethefile*/fclose(stream);return0;}

5. 怎样用C++在文件每行末尾再写入一个数

大概思路是:ifstream ifs("1234.txt ");ofstream ofs("1234_.txt ");string line;while(!ifs.eof()){getline(ifs,line);line+="你要加的数字";ofs<<line;}

6. Cfile如何使用

文件读写的最普通的方法是直接使用CFile进行,如文件的读写可以使用下面的方法: //对文件进行读操作 char sRead[2]; CFile mFile(_T("user.txt"),CFile::modeRead); if(mFile.GetLength()<2) return; mFile.Read(sRead,2); mFile.Close(); //对文件进行写操作 CFile mFile(_T("user.txt "), CFile::modeWrite|CFile::modeCreate); mFile.Write(sRead,2); mFile.Flush(); mFile.Close(); 如果你要进行的文件操作只是简单的读写整行的字符串,建议你使用CStdioFile,用它来进行此类操作非常方便,如下例。 CStdioFile mFile; CFileException mExcept; mFile.Open( "d:\\temp\\aa.bat", CFile::modeWrite, &mExcept); CString string="I am a string."; mFile.WriteString(string); mFile.Close();

7. mfc中用cfile怎样从头到尾读取文件

使用CFile::SeekToBegin()定位到文件头开始读。然后读取得到的文件长度的字节数或者判断到了文件末尾,就OK了。


赞 (0)