Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

how to create a text file in C++


ghost's Avatar
0 0

hi, i want to create a c++ program to create a text file to be outputted onto the desktop but am not sure what the code is to do that, anyone know?


ghost's Avatar
0 0

i dont know of the code… but i will look after this post… BUT..to start off with instead of putting it on A desktop put it one everybodys desktop… it is easier.. and quickeer….

C:\docuemnts and settings\allusers\desktop

OR you could use the short crap

C:\DOCU~!\ALL U~!\Desktop\

i WILL have the code in a few monis hold on


ghost's Avatar
0 0

oh what version of C++…


ghost's Avatar
0 0

OKAY!

it was ONE simpkle google search ¬_¬..

// text_write.cpp // compile with: /clr using namespace System; using namespace System::IO;

int main() { String^ fileName = "textfile.txt";

StreamWriter^ sw = gcnew StreamWriter(fileName); sw->WriteLine("A text file is born!"); sw->Write("You can use WriteLine"); sw->WriteLine("…or just Write"); sw->WriteLine("and do {0} output too.", "formatted"); sw->WriteLine("You can also send non-text objects:"); sw->WriteLine(DateTime::Now); sw->Close(); Console::WriteLine("a new file ('{0}') has been written", fileName);

return 0; }

THATS FOR VISUAL C++


ghost's Avatar
0 0
// compile with: /clr
using namespace System;
using namespace System::IO;

int main() 
{
   String^ fileName = "textfile.txt";

   StreamWriter^ sw = gcnew StreamWriter(fileName);
   sw->WriteLine("A text file is born!");
   sw->Write("You can use WriteLine");
   sw->WriteLine("...or just Write");
   sw->WriteLine("and do {0} output too.", "formatted");
   sw->WriteLine("You can also send non-text objects:");
   sw->WriteLine(DateTime::Now);
   sw->Close();
   Console::WriteLine("a new file ('{0}') has been written", fileName);

   return 0;
}

SORRY FOR POSTING LOADS I AM ONLY TRY TO HELP


ghost's Avatar
0 0

How to write to an exsisting tex file….```markup// basic file operations #include <iostream> #include <fstream> using namespace std;

int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; }


ghost's Avatar
0 0

Thanks for the help Zureck :)


ghost's Avatar
0 0

YAY! he fancies me

Any time Mr spider


ghost's Avatar
0 0

dident work came up with 21 errors lool am using dev c++


mido's Avatar
Member
0 0

cause "i think" the last posts were on VC++


mikispag's Avatar
=> Penguin in black <=
0 0

Hi,

if you simply want a code snipped in standard C++ (not tested):

#include &lt;fstream&gt;
using namespace std;

int main () {
  ofstream MyFile;
  MyFile.open (&quot;test.txt&quot;);
  MyFile &lt;&lt; &quot;Writing this to a file.&#92;n&quot;;
  MyFile.close();
  return 0;
}```

This link could be very useful: [http://www.learn-programming.za.net/programming_cpp_learn09.html](http://www.learn-programming.za.net/programming_cpp_learn09.html)

ghost's Avatar
0 0

returned with error "std::ofstream MyFile' has initializer but incomplete type"


ghost's Avatar
0 0

Thanks mido ;) done it now :D


mikispag's Avatar
=> Penguin in black <=
0 0

Updated code. Now it is tested on my Linux box (using g++)! ;)


ghost's Avatar
0 0

thanks to mikispag aswell :happy:


mido's Avatar
Member
0 0

So, dude, you may include some GUI (API) to it, to become nicer :) but this when you get farther in C++


ghost's Avatar
0 0

yep, be a while yet only just started learning C++ monday :)


mido's Avatar
Member
0 0

Good luck, C++ is interesting :)


ghost's Avatar
0 0

ye, i thought it would be boring its actually pretty fun :happy:


mido's Avatar
Member
0 0

For a complete file editing, you may use ifstream f("test.txt"); to read the file, etc ,etc…