Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


C++ ofstream.write(string, int) Creating unrelated text?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Engineer
Expert Cheater
Reputation: 1

Joined: 25 Nov 2007
Posts: 170

PostPosted: Mon Aug 23, 2010 8:28 am    Post subject: C++ ofstream.write(string, int) Creating unrelated text? Reply with quote

Hello, I wrote this so I can cin>> something into a file

Code:

void selectedCreate()
{
    textInScreen = "";
    string filelocation = "";
    cout << "Save as file (files might be overwritten): ";
    cin >> filelocation;
    ofstream outputstream (filelocation.c_str(), ios::ate);

    if (!outputstream.is_open())
    {
        cout << "Error creating/opening file";
    }
    else
    {
        cout << endl << endl;
        string textToSave = "";
        cin >> textToSave;
        textInScreen = textToSave;
        outputstream.write(textToSave.c_str(), 249);
    }
    outputstream.close();
}


It seems to work fine, but when I open the text files, I get messy results that normally relate to my steam team fortress folder and windir, example

Code:

watthefuck am\steama  l l °> °> am fortress 2\tf windir=C:\WINDOWS      E  ]     ä                                                                                                           

(the only thing i wrote was 'watthefuck')

I really don't know why this happens, any fixes?
edit: probably writing the default vars in cmd? like computername, windir, etc?
Back to top
View user's profile Send private message
XaLeX
Expert Cheater
Reputation: 0

Joined: 19 Aug 2008
Posts: 226

PostPosted: Mon Aug 23, 2010 9:14 am    Post subject: Re: C++ ofstream.write(string, int) Creating unrelated text? Reply with quote

Engineer wrote:
outputstream.write(textToSave.c_str(), 249);

that line of code reads 249 bytes from memory and writes them to the file (and goes past the null terminator). change the 249 (why 249?) to textToSave.length and it should work.
Back to top
View user's profile Send private message
Engineer
Expert Cheater
Reputation: 1

Joined: 25 Nov 2007
Posts: 170

PostPosted: Mon Aug 23, 2010 10:18 am    Post subject: Re: C++ ofstream.write(string, int) Creating unrelated text? Reply with quote

XaLeX wrote:
Engineer wrote:
outputstream.write(textToSave.c_str(), 249);

that line of code reads 249 bytes from memory and writes them to the file (and goes past the null terminator). change the 249 (why 249?) to textToSave.length and it should work.

That only saves the first word on the string. I guess it would do the same thing with my broken code anyway Confused
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Mon Aug 23, 2010 12:59 pm    Post subject: Reply with quote

Consider using std::getline, and fstream.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites