Thread: FAQ: file i/o (C++)

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

    file i/o

    hello good people

    can someone plz give an example (if poss wit a brief explanation)
    on how to work with the funtions CreateFile, WriteFile, ReadFile.
    i'm a bit mixed up with the arguments.
    thanks in advance for your kind time

    1976

    "The truth is an amusing concept"

  2. #2
    Unregistered
    Guest

    try these

    HERE ARE 2 EXAMPLES
    USE ONE ATA TIME.





    /********************FIRST EXAMPLE**********************************/
    /****************
    writes or appends info to a text file
    ****************/

    #include<iostream.h>
    #include<fstream.h>

    void main()
    {
    //setup a new stream (tube) and give it a name

    ofstream outfile;

    //now connect it to a text file
    //then open it


    //delete the first "//" from either ONE of the next two lines
    //outfile.open("C:\\testfile.txt");//this writes over existing text
    //outfile.open("C:\\testfile.txt,ios::app");//this appends to existing text

    //send data
    outfile<<"YOUR NAME HERE"<<endl;
    outfile<<"PORTUGAL"<<endl;
    outfile<<"DID IT WORK?"<<endl;


    //now give some info on screen
    //all go well?????
    cout<<endl<<"file written\n";


    }//end first example

    /****************SECOND EXAMPLE**********************/

    /*******************
    reads from a file and displays info to screen
    **********************/

    #include<iostream.h>
    #include<fstream.h>//needed for string functions

    void main()
    {

    char first_name[25];//[25] characters allowed
    char last_name[25];
    char subject[25];
    char sector;

    //setup file stream
    //first setup and name an input file
    ifstream infile;//call it infile

    //now associate it with a file
    infile.open("C:\\readtest.txt");

    /* create a file C:\readtest.txt
    using notepad (or equivelant) and write this in file
    YOUR christian name
    YOUR surname
    YOUR location
    YOUR gender
    then save as C:\readtest.txt*/


    //now read it
    infile>>first_name;
    infile>>last_name;
    infile>>location;
    infile>>gender;

    //close it!!!
    infile.close();

    //now display
    cout<<endl<<"first name:- "<<first_name;
    cout<<endl<<"last name:- "<<last_name;
    cout<<endl<<"location:- "<<location;
    cout<<endl<<"gender:- "<<gender;

    cout<<endl<<endl;

    }//end reading example

    /************************************************** *********/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM