×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

OK i tried on VC 2005, works like a charm.

// testio.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[]) {

fstream iofile ("c:\\tmp\\test.txt", ios::in | ios::out | ios::binary| ios::ate);

//doing some reading and updating.....
const char c = 'c';
char *s = new char[100];

iofile.seekg( 0, ios_base::end );
long sizeoffile = iofile.tellg();

iofile.seekg( 0, ios_base::beg );
iofile.read( s, 5 );

iofile.seekp(sizeoffile, ios_base::beg);
iofile.write((char *)&c, 1); //nothing is written. try put function. same result.

delete[] s;
return 0;
}
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / USING iostream.h, 突然发现同时读写文件的情况下,没有办法APPEND文件.请问,谢谢.
    • code here:
      fstream iofile (FileName, ios::in | ios::out | ios::binary| ios::ate);

      //doing some reading and updating.....


      iofile.seekp(sizeoffile, ios_base::beg);

      iofile.write((char *)&c, 1); //nothing is written. try put function. same result.
      • 试了,没有问题。两个猜测:1、文件是只读的或被其它软件打开了;2、(char *)&c的内容不是你要写的东西,而是不可见的字符.
        #include <iostream>
        #include <fstream>

        using namespace std;

        int main(void) {

        fstream iofile ("c:\\tmp\\test.txt", ios::in | ios::out | ios::binary| ios::ate);

        const char c = 'c';
        char *s = new char[100];

        iofile.seekg( 0, ios_base::end );
        long sizeoffile = iofile.tellg();

        iofile.seekg( 0, ios_base::beg );
        iofile.read( s, 5 );

        iofile.seekp(sizeoffile, ios_base::beg);
        iofile.write((char *)&c, 1);

        delete[] s;
        return 0;
        }
        • ah. thanks. but I try and try.
        • I try your code. it doesn't work. I am using visual c++ express version. what is your compiler?
          • Oh, sorry, g++, I don't have VC6 on my machine now, maybe i should try on my vc2005...
            • fxxk c++, it seems both vc and g++ has some bugs here and there.
              • Maybe it is just some trival typos, for example, file name ?
                • This is exactly the problem. Very good guess. You are even faster than I who is reading the code in visual C.
            • try mingw g++. It desen't work neither. I am getting crazy..
          • OK i tried on VC 2005, works like a charm.
            // testio.cpp : Defines the entry point for the console application.
            //

            #include "stdafx.h"

            #include <iostream>
            #include <fstream>

            using namespace std;

            int _tmain(int argc, _TCHAR* argv[]) {

            fstream iofile ("c:\\tmp\\test.txt", ios::in | ios::out | ios::binary| ios::ate);

            //doing some reading and updating.....
            const char c = 'c';
            char *s = new char[100];

            iofile.seekg( 0, ios_base::end );
            long sizeoffile = iofile.tellg();

            iofile.seekg( 0, ios_base::beg );
            iofile.read( s, 5 );

            iofile.seekp(sizeoffile, ios_base::beg);
            iofile.write((char *)&c, 1); //nothing is written. try put function. same result.

            delete[] s;
            return 0;
            }
            • content of my c:\tmp\test.txt
              sdjfdslkjflds

              dsf
              dsf
              sdf
              ds
              fds
              fds
              fds
              f
              dsf
              dsf
              dsf
              -----------------------------
            • sorry. I know the reason.
            • The filename is not right in your code, because rolia cut the \\
        • full code is here. It used to add some check sum per page in the database system. 代码死活调不通.先放这.也不知道是不是因为数据文件太大?
          This is the C code:
          http://us.f13.yahoofs.com/bc/4405bf93mdc7255b0/bc/PLAYVC.cpp?bfz3bkEB.NyHGzkK

          This is the data file.
          http://us.f13.yahoofs.com/bc/4405bf93mdc7255b0/bc/Btree.BIN?bfz3bkEB5g3U.qu.
          • shit. Problem is solved. C++ io stream, 笨重的傻瓜的 API.写两个月不到C++文件处理程序,其中化在IOSTREAM上的调试时间,起码有3天.
            I need to add after last read error.
            iofile.clear();

            This is not the first time I make such mistake..
    • visual c
      • 不太懂.
        • question closed. 轮胎 is really good at programming.
          • 你是在夸他还是在骂他?
            • 是他吗?还是她?
              • 轮胎?
        • which part you don't understand?