Thread: Creating text files

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    10

    Creating text files

    I'm not sure if you're to familiar with batch but here is a code...

    Code:
    echo Hello >c:\windows\desktop\hi.txt
    start /m c:\windows\desktop\hi.txt
    What that does is creates a text file on the desktop named hi.txt and inside the text file is the word Hello. Then, it starts the text file. My question is, how can I do this in C++?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Look into fstream. In particular, ofstream (output file stream). It works much like ostream (i.e. cout). If the file you are outputting to does not exist, it will be created.

    To launch the file, the simplest would be a system call:
    Code:
    #include <cstdlib>
    
    int main( )
    {
      std::system("start /m c:\windows\desktop\hi.txt");
      return 0;
    }
    Basically, what you'd put in the batch file (system calls) can be mirrored in a system call. Though, beware: system calls are slow and non-portable. It is a good idea to wrap them in some standard interface so that multiple platforms can be supported.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    It says cstdlib doesn't exist. Thats ok thought because I am just now going into the file I/0 tutorial. I guess I should've read that first

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    Oh it showed up as non exisiting because I put .h after cstdlib. I figured it out now. Thx for the help.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Likely reason is that your compiler isn't standards compliant. Try <stdlib.h>
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  2. Replies: 5
    Last Post: 02-11-2008, 01:36 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM