Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Why do I get these disordered and unexpected result in the output file?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CHINA_yanglei




PostPosted: 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]
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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?
CHINA_yanglei




PostPosted: 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); "
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: