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++] Problem writting to a txt...

 
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: Sun Aug 10, 2008 6:22 am    Post subject: [c++] Problem writting to a txt... Reply with quote

Hello, I'm making a file that writes to a txt file (just for testing), but it only writes the first word of the string Confused
Here's the script:
Code:
/* Text File Writer */
/* Includes */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/* Function */
int main( void ) {
    string writethis;
    ofstream write ("input.txt");
   
    if (write.is_open()) {
    std::cout << "Write down the output for 'input.txt', please.\n";
    std::cin >> writethis;
    write << writethis;
    write.close();
    std::cout << "Done. Press enter to close.";
}
    else { cout << "Error: Unable to locate file.\n"; }
    std::cin.sync();
    std::cin.ignore();
    return 0;
}

How can I fix this problem?
Back to top
View user's profile Send private message
redhead
Cheater
Reputation: 0

Joined: 21 Mar 2007
Posts: 47

PostPosted: Sun Aug 10, 2008 6:41 am    Post subject: Reply with quote

try std::cin.getline() instead of std::cin .
Code:
/* Text File Writer */
/* Includes */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/* Function */
int main( void ) {
    char writethis[256];
    ofstream write ("input.txt");
   
    if (write.is_open()) {
    std::cout << "Write down the output for 'input.txt', please.\n";
   std::cin.getline (writethis,256);
    write << writethis;
    write.close();
    std::cout << "Done. Press enter to close.";
}
    else { cout << "Error: Unable to locate file.\n"; }
    std::cin.sync();
    std::cin.ignore();
    return 0;
}
Back to top
View user's profile Send private message
Engineer
Expert Cheater
Reputation: 1

Joined: 25 Nov 2007
Posts: 170

PostPosted: Sun Aug 10, 2008 6:48 am    Post subject: Reply with quote

redhead wrote:
try std::cin.getline() instead of std::cin .
Code:
/* Text File Writer */
/* Includes */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/* Function */
int main( void ) {
    char writethis[256];
    ofstream write ("input.txt");
   
    if (write.is_open()) {
    std::cout << "Write down the output for 'input.txt', please.\n";
   std::cin.getline (writethis,256);
    write << writethis;
    write.close();
    std::cout << "Done. Press enter to close.";
}
    else { cout << "Error: Unable to locate file.\n"; }
    std::cin.sync();
    std::cin.ignore();
    return 0;
}

Thanks, It worked.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sun Aug 10, 2008 7:58 am    Post subject: Reply with quote

If you want to get rid of that 256 character limit:
Code:
#include <string>

std::string s;
std::getline(std::cin, s);
file << s;
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