Computer Science Canada

Why do I get these disordered and unexpected result in the output file?

Author:  CHINA_yanglei [ Thu Apr 09, 2009 6:33 pm ]
Post subject:  Why do I get these disordered and unexpected result in the output file?

c++:
#include <iostream>
#include <fstream>
int main()
{
        char str[]="yanglei";
        char sta[]=" welcome to you!";
        static char buffer[20];
        std::ofstream os("test.txt");
        os.write(str,sizeof(str));
        os.clear();
        os.close();
        std::ifstream is("test.txt");
        is.seekg(0);
        is.read(buffer,sizeof(buffer));
        std::cout.write(buffer,sizeof(buffer));
        is.close();
        std::ofstream appos("test.txt",std::ios::app);
        appos.seekp(0);
        appos<<'Y';
        appos.seekp(4);
        appos<<'L;
        appos.seekp(sizeof(str));
        appos.write(sta,sizeof(sta));
        appos.close();
        system("pause");
        return 0;
}


I want to be a result like "YangLeiwelcome to you!",but in fact,the result is not a unexpected one.Thank you for your help very much!

Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="cpp"]Code Here[/syntax]

Author:  Insectoid [ Thu Apr 09, 2009 6:34 pm ]
Post subject:  RE:Why do I get these disordered and unexpected result in the output file?

..can you describe the result?

Author:  CHINA_yanglei [ Fri Apr 10, 2009 10:27 am ]
Post subject:  RE:Why do I get these disordered and unexpected result in the output file?

The result is like "Y L welcome to you!"
Oh,I'm sorry about a negligence.
"std::ofstream appos("test.txt",std::ios::app); "
should be
"std::ofstream appos("test.txt",std::ios::ate); "


: