-
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"
-
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
/************************************************** *********/