Thread: add text to file

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    Cool add text to file

    could anyone tell me how to add text to a file?

  2. #2
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    Code:
    #include <fstream.h>
    int main()
    {
       ofstream file
       file.open(anyfile.txt , ios::app)
       file<<"use this like cout"<<endl;
       file.close();
       return 0;
    }
    I didn't try this, but i think it is right.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    Unhappy thanks

    cool thanks

  4. #4
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    You can also use the C code (which I think is faster)

    FILE* myFile;
    int yo;
    yo=16;

    myFile=fopen("file.txt","rt"); //To open for read access
    myFile=fopen("file.txt","wt"); //To open for write access
    //YOU CAN ONLY OPEN A FILE ONCE BEFORE YOU CLOSE IT

    fprintf(myFile,"Hello, I am %d years old",yo); //to output to the file
    fscanf(myFile,"%d",&yo); //to input from a file

    fclose(myFile); //Closes the file
    --------------------------------------------------------------------
    Or, you could use Win32 API code

    HANDLE CreateFile(
    LPCTSTR lpFileName, // file name
    DWORD dwDesiredAccess, // access mode
    DWORD dwShareMode, // share mode
    LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
    DWORD dwCreationDisposition, // how to create
    DWORD dwFlagsAndAttributes, // file attributes
    HANDLE hTemplateFile // handle to template file
    );

    like this:

    HANDLE myFile;
    char buffer[100000];
    myFile=CreateFile("c:\my documents\hello.txt",GENERIC_READ ¦ GENERIC_WRITE,FILE_SHARE_READ ¦ FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE _NORMAL,NULL);

    and then
    BOOL WriteFile(
    HANDLE hFile, // handle to file
    LPCVOID lpBuffer, // data buffer
    DWORD nNumberOfBytesToWrite, // number of bytes to write
    LPDWORD lpNumberOfBytesWritten, // number of bytes written
    LPOVERLAPPED lpOverlapped // overlapped buffer
    );

    like this:

    WriteFile(myFile,"Hello",5,0,NULL);

    and

    BOOL ReadFile(
    HANDLE hFile, // handle to file
    LPVOID lpBuffer, // data buffer
    DWORD nNumberOfBytesToRead, // number of bytes to read
    LPDWORD lpNumberOfBytesRead, // number of bytes read
    LPOVERLAPPED lpOverlapped // overlapped buffer
    );

    ReadFile(myFile,buffer,100000,0,NULL);

    the buffer can be anything since it's a void pointer, such as an int, or a DWORD, or even a struct!!

    if you want to find out more about Win32 API File I/O functions, go to http://msdn.microsoft.com , go to Platform SDK, to Win32 API, Reference, Functions by category, File I/O (I think).

    good luck
    oskilian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM