使用CStdioFile的对象进行文件读写,使用ReadString()读文件,再用WriteString()写文件,为了保证写出来的文件和读入的文件格式一样,每次ReadString()后都加了一个"\r\n",但这样写出来的文件会比原文件稍稍大了那么一点,请问各位有没有什么方法使写出来的文件与原文件格式一样,文件大小也一样。谢谢。

解决方案 »

  1.   

    就只有文件大小不一样,原文件大小是1113kb,写出来的文件是1119kb,文件内容一模一样
      

  2.   

    可能原文件是非DOS格式,换行符是'\n'或'\r'
      

  3.   

    不明白 原文件就是一个很普通的TXT文档
      

  4.   


    猜测而已,
    各个系统的换行符是不同的
    Windows是'\r\n'
    Unix是'\n'
    苹果是'\r'
      

  5.   

    再写一遍还是一样的 如果把写出来的文件当原文件,再写出来的文件还是会增大
    下面是我写的源码
    void OperFile::Oper(LPCTSTR InFileName,LPCTSTR OutFileName)
    {
    CStdioFile InFile,OutFile;
    CString InText="",OutText="";
    InFile.Open(InFileName,CFile::modeRead|CFile::typeText);
    OutFile.Open(OutFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeText);
    while(InFile.ReadString(InText))
    {
    OutText=InText;
    OutText+="\r\n";
    OutFile.WriteString(OutText);
    }
    InFile.Close();
    OutFile.Close();
    }
      

  6.   

    OutText=InText;
    OutText+="\r\n";
    OutFile.WriteString(OutText);// 每次写入,每行都多了"\r\n";
    // WriteString会自动在行尾加上"\r\n"
      

  7.   

    但是如果不加,写出来的文件格式和原来的文件不一样,而且不加写出来的文件大小会偏小,原文件1113kb,写出来的文件1110kb
      

  8.   

    就是英文版的哈利波特的一部分
    Harry Potter and the Deathly HallowsC0="Chapter One: The Dark Lord Ascending"
    Chapter One 
    The Dark Lord Ascending
    The two men appeared out of nowhere, a few yards apart in the narrow, moonlit lane. For a second they stood quite still, wands directed at each other's chests; then, recognizing each other, they stowed their wands beneath their cloaks and started walking briskly in the same direction. "News?" asked the taller of the two. "The best," replied Severus Snape. The lane was bordered on the left by wild, low-growing brambles, on the right by a high, neatly manicured hedge. The men's long cloaks flapped around their ankles as they marched. "Thought I might be late," said Yaxley, his blunt features sliding in and out of sight as the branches of overhanging trees broke the moonlight. "It was a little trickier than I expected. But I hope he will be satisfied. You sound confident that your reception will be good?" Snape nodded, but did not elaborate. They turned right, into a wide driveway that led off the lane. The high hedge curved into them, running off into the distance beyond the pair of imposing wrought-iron gates barring the men's way. Neither of them broke step: In silence both raised their left arms in a kind of salute and passed straight through, as though the dark metal was smoke.
    The yew hedges muffled the sound of the men's footsteps. There was a rustle somewhere to their right: Yaxley drew his wand again pointing it over his companion's head, but the source of the noise proved to be nothing more than a pure-white peacock, strutting majestically along the top of the hedge.
    "He always did himself well, Lucius. Peacocks " Yaxley thrust his wand back under his cloak with a snort.
    A handsome manor house grew out of the darkness at the end of the straight drive, lights glinting in the diamond paned downstairs windows. Somewhere in the dark garden beyond the hedge a fountain was playing. Gravel crackled beneath their feet as Snape and Yaxley sped toward the front door, which swung inward at their approach, though nobody had visibly opened it.
    The hallway was large, dimly lit, and sumptuously decorated, with a magnificent 
      

  9.   

    CStdioFile的WriteString会把\r和\n都转义解释成\r\n
    如果你的代码是sf.WriteString("ab\n");
    那么得到的结果数据是ab\r\n
      

  10.   

    OutText=InText;
    OutText+="\r\n";
    OutFile.WriteString(OutText);
      

  11.   

    OutText=InText;
    OutText+="\r\n";
    OutFile.WriteString(OutText);// 改为
    OutText=InText;
    OutText+="\n"; // 如果加了"\r\n",输出就成了"\r\n\r\n"
    OutFile.WriteString(OutText);
      

  12.   

    // msdn上的
    virtual void WriteString(
       LPCTSTR lpsz 
    );
    The terminating null character ('\0') is not written to the file. Any newline character in lpsz is written to the file as a carriage return–linefeed pair.// 以前没注意过。