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++] How to Create and Save .txt file?

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

Joined: 16 Jun 2006
Posts: 551

PostPosted: Tue Oct 11, 2011 1:20 am    Post subject: [C++] How to Create and Save .txt file? Reply with quote

I searched and have yet to find a solid example...

All I want to do is create a new text file, write to it, and then save it to my hard drive. It never shows up, despite whatever I try.

Code:
#include <iostream>
#include <fstream>


int main{


    ofstream fout;
    fout.open("C:\\fuuuuuuu.txt");
    fout << "Hello World" << endl;
    fout << "Tutorial : " << 37 << endl;
    fout << "fputs" << endl;
    fout.close();

//or this

  fstream myfile("c:\\muhfilez.txt",ios::app);
  myfile.open("C:\\new.txt", ios::beg);
  char* toWrite = "fuckIsuck";
  myfile.write(toWrite, 10);
  myfile.close();





   return 0;
}


Now tell me how stupid I am and please direct me to some help if you could @_@ Links/replies appreciated

_________________
Back to top
View user's profile Send private message
callmenuge
Cheater
Reputation: 0

Joined: 12 Feb 2011
Posts: 42

PostPosted: Tue Oct 11, 2011 1:54 am    Post subject: Reply with quote

I find fopen() to be easier to use. Create a FILE pointer, call fopen(), fwrite() and finally fclose()

Reference:
http://msdn.microsoft.com/en-us/library/yeby3zcb(v=vs.71).aspx
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Tue Oct 11, 2011 2:46 am    Post subject: Reply with quote

Code:
FILE* myFile = NULL;;
myFile = fopen("C:\\fileIWannaCreate.txt", "a");
fwrite("derp", 5, 0, myFile);
fclose(myFile);


I did this, but when it calls fclose, it throws a debugging error - it says that the stream is NULL still when it reaches that point Shocked

_________________
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Oct 11, 2011 6:42 am    Post subject: Reply with quote

Try using ofstream with specifically passing ios::out as the second param.

Code:

    std::ofstream outStream( "C:\\example_file.txt", std::ios::out );
    if( ! outStream.is_open() || ! outStream.good() )
    {
        std::cout << "Failed to create and/or open the file." << std::endl;
        return 0;
    }

    outStream << "Hello World" << std::endl;
    outStream << "Tutorial: " << 37 << std::endl;
    outStream << "fputs" << std::endl;
    outStream.close();


Works fine for me.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Tue Oct 11, 2011 1:18 pm    Post subject: Reply with quote

manc wrote:
Code:
FILE* myFile = NULL;;
myFile = fopen("C:\\fileIWannaCreate.txt", "a");
fwrite("derp", 5, 0, myFile);
fclose(myFile);


I did this, but when it calls fclose, it throws a debugging error - it says that the stream is NULL still when it reaches that point Shocked

Try creating the file with 'wt' permissions
Code:

FILE   *file;

file = fopen( "MyFile.txt", "wt" ); // wt stands for Write Text, rt for Read text
fputs( "Hello World", file );
fclose( file );

If you're trying to write / read a binary file, just replace the t (text) in a b (Binary) instead.

_________________
Stylo
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Wed Oct 12, 2011 2:29 am    Post subject: Reply with quote

Wiccaan wrote:
Try using ofstream with specifically passing ios::out as the second param.


Works fine for me.


I ran it, and "Failed to create and/or open the file." got called, so outStream.open() or outStream.good() failed..which is why Im confused

_________________
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed Oct 12, 2011 10:04 am    Post subject: Reply with quote

manc wrote:
Wiccaan wrote:
Try using ofstream with specifically passing ios::out as the second param.


Works fine for me.


I ran it, and "Failed to create and/or open the file." got called, so outStream.open() or outStream.good() failed..which is why Im confused


Odd; there isn't really a reason why it would just fail unless you have some type of anti-virus that is blocking file creation in your root system drive. (Being C:\ in this case.) Can you create files in other locations, perhaps in the same folder as the exe? Or does every call to ofstream fail?

You can call GetLastError within the fail output to see what happened to block it from being created/opened as well:

Code:

    std::ofstream outStream( "C:\\example_file.txt", std::ios::out );
    if( ! outStream.is_open() || ! outStream.good() )
    {
        std::cout << "Failed to create and/or open the file." << std::endl;
        std::cout << "Failed reason: " << GetLastError() << std::endl;
        return 0;
    }

    outStream << "Hello World" << std::endl;
    outStream << "Tutorial: " << 37 << std::endl;
    outStream << "fputs" << std::endl;
    outStream.close();


For example, if I manually create the file and set it to read-only, ofstream will fail with error code '5'. (Access violation.)

You can find a full list of error codes here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381%28v=vs.85%29.aspx

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Wed Oct 12, 2011 11:47 pm    Post subject: Reply with quote

Interesting.

When I try and create it in C:\ GetLastError() returns code 5 which is this:

ERROR_ACCESS_DENIED
5 (0x5)
Access is denied.

However, I tried creating it in my Project folder.. i.e. C:\users\me\my docs\vis2010\projects\exampleProjectname\myfile.txt, and it works fine. @_@ Gonna do more testing I guess

_________________
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Oct 13, 2011 9:39 am    Post subject: Reply with quote

Sounds like a problem with a virus scanner blocking the creation or possibly even UAC preventing it too.

Try running your application as an Administrator after you compile it again to create the file in C:\ and see if it works then. If so then you either need to disable UAC or add the proper permissions request in your projects manifest file.

The manifest should look similar to:
Code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <v3:trustInfo xmlns:v3="urn:schemas-microsoft-com:asm.v3">
    <v3:security>
      <v3:requestedPrivileges>
        <v3:requestedExecutionLevel level="highestAvailable"/>
      </v3:requestedPrivileges>
    </v3:security>
  </v3:trustInfo>
</assembly>


To request higher access. More info here if you need help embedding it:
http://msdn.microsoft.com/en-us/library/bb756929.aspx

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
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