You are on page 1of 7

i hc Quc Gia Thnh ph H Ch Minh

Trng i hc Khoa Hc T Nhin


Khoa Cng Ngh Thng Tin









Hng dn
THAO TC
VI TP TIN TRONG C++













________________________


________________________



Thng 9/2011
Thao tc vi tp tin

Khoa CNTT | H KHTN TP HCM Trang 2

1 Phn loi
Tp tin l hnh thc lu tr ph bin trn b nh ph, gm 2 loi:
Tp tin vn bn: l tp tin ch lu tr thun ty vn bn, trong cc k t c biu din
bng m ASCII ca n, ngi dng c th c c.
o Tnh cht
D truy xut v x l
bo mt km
Tc truy xut km
Kch thc ln
o V d: Lu s nguyn 12345 dng binary (2 byte) v dng chui (5 byte)
o Cc dng thng gp
Tp tin cu hnh: *.INI, *.CFG
*.TXT, *.HTML, *.XML
Tp tin nh phn: l tp tin cha d liu m c t nht mt vi chui bit khng th biu din
dng vn bn trn. Do tp tin ny ch c my c c, cn ngi khng c c.
o Tnh cht
Truy xut v x l phc tp
Tc truy xut nhanh
Tnh bo mt cao hn
o Cc dng thng gp
Cc tp tin c cu trc t nh ngha
*.DOC, *XLS, *.PDF, *.PPT
*.JPG, *.PCX, *.BMP, *.TIF
*.MP3, *.WAV, *.AVI
2 Mt s hm thao tc tp tin vi C++
Nhiu b th vin cung cp mt tp cc hm thao tc vi tp tin vi C++. y gii thiu 2 cch
ph dng:
Tp hm trong cstdio (stdio.h)
Tp hm trong namespace std
2.1 S dng cstdio (stdio.h)
2.1.1 Mt s hm x l chung
Tn hm Chc nng
fopen M tp tin
fclose ng tp tin
fcloseall ng tt c tp tin
fflush Lm sch vng m ca mt tp tin ang m
fflushall Lm sch vng m ca tt c
remove/unlink Xa tp tin
feof Kim tra xem c n cui tp tin
Thao tc vi tp tin

Khoa CNTT | H KHTN TP HCM Trang 3

2.1.2 Mt s hm x l cho tp tin vn bn
Tn hm Chc nng
fprintf Ghi gi tr dng text ln tp tin
fscanf c gi tr dng text t tp tin
putc/fputc Ghi ln tp tin mt k t (s dng tp tin vn bn hoc nh phn c khc bit )
getc/fgetc c t tp tin mt k t (s dng tp tin vn bn hoc nh phn c khc bit)
fputs Ghi mt chui vo tp tin
fgets c mt chui t tp tin
2.1.3 Mt s hm x l cho tp tin nh phn
Tn hm Chc nng
putw Ghi mt s nguyn ln tp tin
getw c mt s nguyn t tp tin
fwrite Ghi cc mu tin (c cu trc nh trc) ln tp tin
fread c cc mu tin (c cu trc nh trc) t tp tin
fseek Di chuyn con tr n v tr mong mun
ftell Cho bit v tr hin ti ca con tr

Chi tit ca mi hm c th xem t MSDN hoc link sau:
http://www.cplusplus.com/reference/clibrary/cstdio/
2.1.4 V d:
Ghi tp tin vn bn
/* fprintf example */
#include <stdio.h>
int main ()
{
FILE * pFile;
int n;
char name [100];

pFile = fopen ("myfile.txt","w");
for (n=0 ; n<3 ; n++)
{
puts ("please, enter a name: ");
gets (name);
fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
}
fclose (pFile);

return 0;
}
Thao tc vi tp tin

Khoa CNTT | H KHTN TP HCM Trang 4

c tp tin vn bn
/* fscanf example */
#include <stdio.h>

int main ()
{
char str [80];
float f;
FILE * pFile;

pFile = fopen ("myfile.txt","w+");
fprintf (pFile, "%f %s", 3.1416, "PI");
rewind (pFile);
fscanf (pFile, "%f", &f);
fscanf (pFile, "%s", str);
fclose (pFile);
printf ("I have read: %f and %s \n",f,str);
return 0;
}

Ghi tp tin nh phn
#include <stdio.h>
int main ()
{
FILE * pFile;
char buffer[] = { 'x' , 'y' , 'z' };
pFile = fopen ( "myfile.bin" , "wb" );
fwrite (buffer , 1 , sizeof(buffer) , pFile );
fclose (pFile);
return 0;
}

c tp tin nh phn
#include <stdio.h>
#include <stdlib.h>

int main () {
FILE * pFile;
long lSize;
char buffer[255];
Thao tc vi tp tin

Khoa CNTT | H KHTN TP HCM Trang 5


pFile = fopen ( "myfile.bin" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); return;}

// copy the file into the buffer:
result = fread (buffer,1,255,pFile);
if (result != lSize) {fputs ("Reading error",stderr); return;}

fclose (pFile);
return 0;
}

2.2 S dng namespace std
ofstream: class gm cc phng thc ghi tp tin
ifstream: class gm cc phng thc c tp tin
fstream: class gm cc phng thc cho c thao tc c v ghi tp tin
y ch gii thiu fstream.
2.2.1 Mt s hm x l chung
Tn phng thc Chc nng
open M tp tin
close ng tp tin
is_open Kim tra xem tp tin c ang m hay khng.
flush Lm sch vng m ca tp tin ang m
good Kim tra trng thi ca tp tin c tt c/ghi khng
eof Kim tra xem c n cui tp tin cha
2.2.2 Mt s hm x l cho tp tin vn bn
Tn phng thc Chc nng
operator >> c d liu t tp tin
operator << Ghi d liu vo tp tin
getline c mt dng d liu t tp tin
get c mt k t hoc mt chui t tp tin, ty thuc vo tham s ca phng
thc.
put Ghi mt k t xung tp tin
2.2.3 Mt s hm x l cho tp tin nh phn
Tn phng thc Chc nng
read c mu tin (theo cu trc nh trc) t tp tin
write Ghi mu tin (theo cu trc nh trc ) vo tp tin
seekg Di chuyn con tr file n v tr xc nh
tellp Cho bit v tr hin ti ca con tr file

Thao tc vi tp tin

Khoa CNTT | H KHTN TP HCM Trang 6

Chi tit ca mi hm c th tham kho trn MSDN hoc link sau:
http://www.cplusplus.com/reference/iostream/fstream/
2.2.4 V d:
Ghi tp tin vn bn
#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;
}

c tp tin vn bn
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
myfile >> line;
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Ghi tp tin nh phn
#include <fstream>
Thao tc vi tp tin

Khoa CNTT | H KHTN TP HCM Trang 7

using namespace std;
int main ()
{
ifstream file ("example.bin", ios::out|ios::binary|ios::ate);
char buffer[] = { 'x' , 'y' , 'z' };
if(file.is_open())
file.write(buffer , sizeof(buffer) );
file.close();
return 0;
}

c tp tin nh phn
// c ton b ni dung ca tp tin
#include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {
ifstream file ("example.bin", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();

cout << "the complete file content is in memory";

delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}

You might also like