View previous topic :: View next topic |
Author |
Message |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue Oct 11, 2011 1:20 am Post subject: [C++] How to Create and Save .txt file? |
|
|
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 |
|
 |
callmenuge Cheater
Reputation: 0
Joined: 12 Feb 2011 Posts: 42
|
|
Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue Oct 11, 2011 2:46 am Post subject: |
|
|
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
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Tue Oct 11, 2011 6:42 am Post subject: |
|
|
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 |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Tue Oct 11, 2011 1:18 pm Post subject: |
|
|
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  |
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 |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Wed Oct 12, 2011 2:29 am Post subject: |
|
|
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Oct 12, 2011 10:04 am Post subject: |
|
|
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 |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Wed Oct 12, 2011 11:47 pm Post subject: |
|
|
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Oct 13, 2011 9:39 am Post subject: |
|
|
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 |
|
 |
|