typedef :
typedef struct st_tag{
char name[30]; int kurs; char group[3]; int index;
} STUDENT;
struct st_tag avar; STUDENT ava;
:
. . . malloc() free(). . malloc() , free() . stdlib.
void *malloc(size_t size); void *free(void *p);
malloc() void, . malloc() . , 0 ( ). , , sizeof.
:
#include <stdio.h>
#include <stdlib.h>
/* */
Void main(void)
{
int *p,i;
p=malloc(100*sizeof(int)); /* 100 */
if(!p)
{
printf( !\n);
Exit(1);
}
for(i=0;i<100;i++) *(p+i)=i;
for(i=0;i<100;i++) printf(%d, *(p++));
free(p); /* */
}
C++ new delete. :
pointer_var = new var_type;
delete pointer_var;
pointer_var - var_type;
new . NULL. delete , pointer_var.
new , var_type , .
pointer_var = new var_type[SIZE];
delete [SIZE] pointer_var; // or delete pointer_var;
:
#include <iostream.h>
Void main(void)
{
int *p=new int; //
if(!p) {
cout<<\n !\n;
Return 1;
}
*p=20;
cout<<p<<endl;
delete p; //
}
:
#include <iostream.h>
Void main(void)
{
Unsigned int size;
|
|
cout<<\n :;
cin>>size;
int *p=new int[size];
if(!p) {
cout<<\n !\n;
Return 1;
}
for(int i=0; i<size; i++)
p[i]=i*i; // *p++=i*i
//
int *q=p;
for(i=0; i<size; i++)
cout<<*q++<< ; //
cout<<endl;
Delete p;
}
, , .
-
/ . ANSI, (buffered) (formatted) /. , /, UNIX -, ( ) /. ++ - /.
(file) (stream). / , , /, . . . . . (, , , ..), ANSI , . , , . : (text) (binary).
. , , .
, . . , . , . , .
, , , FILE. stdio.h.
/
printf() scanf() . , , .
printf() stdio.h
int printf(char *_,);
: , , ( ), , . %, . :
%c ;
%d ;
%i ;
%e x.xx e xx;
%E x.xx E xx;
|
|
%f xx.xxxx;
%F xx.xxxx;
%g - %f %e, ;
%G - %F %E, ;
%o ;
%u ;
%x ( 5a5f);
%X ( 5A5F);
%% - %;
%p ;
%n ;
:
%ld long int;
%hu short unsigned;
%Lf long double;
% , , . , , . , , . :
printf(%05d,15); 00015.
, , , . , , , .
. , % -. , , .
scanf() . . stdio.h : int scanf(char *_, );
: , . %. :
%c ;
%d ;
%i ;
%e float;
%h short int;;
%o ;
%s ;
%x ;
%p ;
%n ;
. . (discard) . , , , . , .
, . * % , . , scanf(%d%*c%d,&i,&j); 50+20 i 50, j 20, + .
, . ,
scanf(%5s,str); 5 . 123456789 str 12345, .
- , , . 5plus10
scanf(%dplus%d,&i,&j); i 5, j 10, plus , .
getche() . , , , . . int getche(void); conio.h.
|
|
putchar(). , , . int putchar(int c); stdio.h.
getche() getchar() getch(). getchar() , Enter. stdio.h. getch() , getche(), , conio.h. getch() - , .
gets() puts() . gets() char gets(chat *s); s , . Enter. \0, .
puts() . int puts(char *s);
, printf(), . printf() puts() , , printf(). puts() . , , EOF. puts() gets() stdio.h.
- ANSI , (file pointer). , : , , . , . FILE stdio.h. stdio.h .
fopen() | |
fclose() | |
fputc() | |
fgetc() | |
fputs() | |
fgets() | |
fseek() | |
fprintf() | |
fscanf() | |
feof() | , . |
ferror() | , |
fread() | |
fwrite() | |
rewind() | |
remove() |
, FILE *fput;
fopen() : -, ; -, , .
FILE *fopen(char *filename, char *mode); mode , . :
r | |
w | |
a | |
rb,rt | () |
wb,wt | () |
ab,at | () |
r+ | |
w+ | |
a+ | |
r+b,r+t | () |
w+b,w+t | () |
a+b,a+t | () |
test ,
|
|
FILE *fp;
fp=fopen(test,w);
:
FILE *fp;
if(fp=fopen(test,w))==NULL)
{
puts( \n);
Exit(1);
}
. NULL stdio.h. exit() void exit(int val); , val . , , .
fputc() int fputc(int ch, FILE *fptr); , , EOF.
fgetc() , fopen().
int getc(FILE *fptr); fputc(), fgetc() .
ch=getc(fptr);
while(ch!=EOF)
{
ch=fgetc(fptr);
}
fputs()
int fputs(char *, FILE *fptr);.
fgets()
char *fgets(char *s, int n, FILE *fptr); n .
fprintf() fscanf() () . , , .
fprintf(),fscanf() stdio.h
int fprintf(FILE *fptr, char *_,);
int fscanf(FILE *fptr, char *_,);
feof() int feof(FILE *fptr);
, , . .
while(!feof(fptr))
{
ch=fgetc(fptr);
}
fclose() . : int fclose(FILE *fptr); () , , , .
, EOF. , , ferror() : int ferror(FILE *fptr);, . ferror() , .
rewind() , . : void rewind(FILE *fptr);
fread(), fwrite() ;
unsigned fread(void *buf, int bytes, int c,FILE *fptr);
unsigned fwrite(void *buf, int bytes, int c,FILE *fptr);
buf , ;
, bytes ();
bytes ;
fptr .
, fseek(), . :
int fseek(FILE *fptr, long numbytes, int origin);
fptr ;
numbytes ;
origin , stdio.h.
SEEK_SET | ||
SEEK_CUR | ||
SEEK_END |
, 5 . (stdin), (stdout) (stderr) , . stdin, stdout, stderr , FILE. , stdprn stdaux, . .
|
|
remove() . int remove(char *filename);
UNIX, -. UNIX. io.h. :
read() ;
write() ;
open() ;
close() ;
lseek() ;
unlink() .
, .
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
Int main()
{
char *tmp=new char[80];
char *fname=new char[20];
char *word=new char[80];
char *ptr;
FILE *fr;
FILE *fw;
Clrscr();
fw=fopen("bbb.cpp","w");
int a=0;
cout<<"Give name file:"<<endl;
Do
{
Cin.getline(fname,20);
if ((fr=fopen(fname,"r"))==NULL)
{
a=0;
cout<<"Invalid name file!! Give correct name:"<<endl;
}
else a=1;
} while(a==0);
cout<<"\nGive word:";
Cin.getline(word,80);
while(!feof(fr))
{
// fscanf(fr,"%s",tmp); // read only to space
Fgets(tmp,80,fr);
if ((ptr=strstr(tmp,word))!=NULL) // search word
{
cout<<tmp<<endl;
fprintf(fw,"%s",tmp);
}
}
Fclose(fr);
Fclose(fw);
Getch();
Return 0;
}
C++
fstream.h. ++ . ifstream, , ofstream, . , fstream.
Ifstream input_from;
Ofstream out;
Fstream in_out_stream;
, open().
void open(char *filename, int mode, int access);
filename ;
mode , ;
access . ( )
NORMAL | ||
READ_ONLY | ||
HIDDEN | ||
SYSTEM | ||
ARCHIVE |
, fstream.h. ( )
ios::app | |
ios::ate | |
ios::in | |
ios::nocreate | , |
ios::out | |
ios::trunc | - , |
ios::noreplace | , |
ios::text | |
ios::binary |
ifstream ofstream ios::in ios::out.
open() . ,
Ofstream out;
out.open(myfile,ios::out,0);
Ofstream out;
out.open(myfile);
.
.
Fstream m_in_out;
m_in_out.open(myfile,ios::in|ios::out);
close(). m_in_out.close();
:
Ofstream m_out;
m_out.open(myfile,ios::out,0);
if(!m_out)
{
cout<< ;
Return 1;
}