- C++, .
:
- ;
- , ;
- ;
- .
C++ -. - C (, printf scanf, ) C++ . - , - . C++.
2.1 C++
-
C++ - string C++.
C- , 0. string , C-, . - . .net, , C-.
, , , . , . . , , -, , -, :
const int len_str = 100;
char msg[len_str];
-. , , , 100 , 99. , - , , :
const int len_str = 100;
char msg[len_str] = " ";
, ( , ):
char msg[ ] = " "; //13
char, new mall ( ).
char *p = new char[len_str];
, . , , .
|
|
- cin cout, , .
:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
const int n = 80;
char s[n];
cin >> s;
cout << s << endl;
return 0;
}
, .
, . , ( , '\n')
, , , getline get istream, cin.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
const int n = 80;
char s[n];
cin.getline(s, n);
cout << s << endl;
return 0;
}
getline n - 1 ( ) s, () , , '\0'. n - 1 , , . get , . .
, getl1n , :
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
const int n = 80;
char s[n];
while (cin.getline(s, n))
{
cout << s << endl;
};
return 0;
}
- , ++ . -, scanf, printf, %s.
, - . , , %c () , :
scanf("%10c", s);
. %s , :
printf("%10s", s);
. , , .
, - : gets puts.
gets(s) s ( , -).
puts(s) s , 0 . EOF .
|
|
printf , . , - gets puts.
, , . ( , ). , p a, strcpy strncpy, strlen.
#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a[100] = "Working with a strings";
size_t m = strlen(a) + 1; // 1 -
char *p = new char [m];
strcpy(p, a);
strncpy(p, a, strlen(a) + 1);
return 0;
}
. strcpy strncpy , -, . - . : strcpy_s strncpy_s, . , .
<string.h>.
strcpy(dst, src) , (src), 0, , (dst). strncpy(dst, src, n) , n , , . - , , n dst -. ( n src) - dst .
. , - -, , .
strlen(src) , -.
, - - ( m 100), , -.
atoi(str). , , . , . , 0.
(long) (double) atol atof .
//
#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a[] = "15) - - 249 . - 499.99 .";
int num;
long quantity;
double price;
num = atoi(a);
quantity = atol(&a[12]);//12 - -
|
|
price = atof(&a[27]); //27 -
cout << num << ' ' << quantity << ' ' << price;
return 0;
}
. . . ( setlocale(LC_ALL, "Russian")), , .. .
, , , .
char. - -, .
- , get() get(char).
, . , , get() get(char).
get() EOF, get(c) , , .
<stdi.h> getchar() , putchar() .
.
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
char a, b, c, d, e;
cin >> a >> b;
cout << a << ' ' << b << endl;
c = cin.get();
cin.get(d);
cout << c << ' ' << d << endl;
e = getchar();
putchar(e);
return 0;
}
, - , (isalfa), (isspace), (ispunct), (isdigit) . . .
2.1.4.1 <string.h> (<cstring>) C
void *memchr(const void *p, int ch, size_t n);
.
, ch p n.
int memcmp(const void *p1, const void *p2, size_t n);
: , strcmp.
void *memcpy(void *dest, const void *src, size_t n);
n src dest.
void *memmove(void *dest, const void *src, size_t n);
memcpy, dest src .
void *memset(const void *, int ch, size_t n);
, ch.
char *strcat(char *s1, char *s2);
s2 s1 s1. -.
|
|
char *strchr(char *s, int ch);
ch s, , NULL.
int strcmp(char *s1, char *s2);
( s1 s2), ( s1 s2) ( s1 s2) .
char *strcoll(char *s1, char *s2);
strcmp, .
char *strcpy(char *s1, char *s2);
s2 s1 s1.
size_t strcspn(char *s1, char *s2);
s2 s1.
char *strerror(size_t n);
n.
struct tm strftime(char *s, size_t size, fmt, const struct tm *ctm);
fmt tm
fmt. time_t, tm.
size_t strlen(char *s);
( ).
char *strncat(char *s1, char *s2, size_t n);
n
s2 s1 s1. s2 - s1. s2 n, s2. s1 -. , .
int strncmp(char *s1, char *s2, size_t n);
n
n ( s1 s2), ( s1 s2) ( s1 s2) .
char *strncpy(char *s1, char *s2, size_t n);
n
n s2 s1 s1. n, - s1 . - n- . , .
char *strpbrk(char *s1, char *s2);
, s2 s1, , NULL.
char *strrchr(char *s,int ch);
ch s , , NULL.
size_t strspn(char *s1, char *s2);
,
s1, s2.
char *strstr(char *s1, char *s2);
s2 s1. , s1, s2, NULL .
double strtod(const char *str, char **end);
. +/-HUGE_VAL 0. errno ERANGE. end , .
char *strtok(char *s1, char *s2);
s1, s2.
double strtol(const char *str, char **end, int radix);
radix . , . end char*; . LONG_ LONG_MIN. 0. errno ERANGE.
|
|
double strtoul(const char *str, char **end, int radix);
strtol, . ULONG_MAX.
size_t strxfrm(char *s1, char *s2, size_t n);
s2 s1 . n .
2.1.4.2 <stdio.h> (<cstdio>) - C
int snprintf(wchar_t *buffer, const wchar_t *format[, argument, ]);
buffer , , , , format. sprintf .
int swscanf(const wchar_t *buf, const wchar_t *format,...);
scanf , , , . buf , , format , , , . sscanf .
int sprintf(char *buffer, const char *format[,argument, ]);
buffer , , , , format.
int sscanf(const char *buf, const char *format [,par1, par2, ]);
scanf , , , . buf , , format , , , .
2.1.4.3<ctype.h> (<cctype>)
int tolower(int ch);
ch . ch .
int toupper(int ch);
ch . ch .
int towlower(wint_t ch);
ch . tolower .
int towupper(wint_t ch);
ch . toupper .
int isalnum(int ch);
,
ch true, ch , false .
int isalpha(int ch);
,
ch true, ch , false .
int iscntrl(int ch);
,
ch true, ch ( line feed, del, , 0x01 01F ( ASCII)), false .
int isdigit(int ch);
,
ch true, ch , false .
int isgraph(int ch);
,
ch true, ch ( , . .) false .
int islower(int ch);
,
ch true, ch , false .
int isprint(int ch);
,
ch true, ch (isgraph + ), false .
int ispunct(int ch);
,
ch true, ch ( , , , ), false .
int isspace(int ch);
,
ch true, ch , , ( ), false .
int isupper(int ch);
,
ch true, ch , false .
int iswalnum(wint_t h);
,
true, ch , false . isalnum .
int iswalpha(wint_t ch);
,
true, ch , false . isalpha .
int iswcntrl(wint_t h);
,
true, ch ( line feed, del, , 001 0x1F ( ASCII)), false . iscntrl .
int iswctype(wint_t , wctype_t desc);
, c desc, .
int iswdigit(wint_t h);
,
true, ch , false . isdigit .
int iswgraph(wint_t h);
,
true, ch ( , . .) false . isgraph .
int iswlower(wint_t h);
,
true, ch , false . islower .
int iswprint(wint_t h);
,
true, ch (iswgraph + ), false . isprint .
int iswpunct(wint_t h):
,
true, ch ( , , , ), false . ispunct .
int iswspace(wint_t h);
,
true, ch , , ( ), false . iss .
int iswupper(wint_t h);
,
true, ch , false . isupper .
int iswxdigit(wint_t ch);
,
true, ch (, F ), false . isxdigit .
int isxdigit(int h);
,
ch true, ch (, A F ), false .
-
C++ :
- streambuf , , , .
- ios -.
- istream ostream ios .
- iostream , , .
- ifstream, ofstream fstream -.
- istrstream ostrstream ( ).
<iostream>. , <fstream> ( -), <iomanip> ( ) <strstream> ( ).
- C++ -, .
11.1 - C++
cin | istream | (). |
cout | ostream | (). |
cerr | ostream | () . |
clog | ostream | () . |
- C++ istream ostream. (>>), . ostream (<<); .
- : -, .
- 2.
ios ( ) _state ( 11.2).
11.2
goodbit | ( - , 0 ). |
eofbit | , . |
failbit | . . |
badbit | , . , . |
ios .
- int rdstate(); .
- bool eof(); true, eofbit.
- bool good(); true, .
- bool fail(); true, failbit badbit.
- bool bad(); true, badbit.
- void clear(int = 0); ( ) .
- void setstate(int); .
- operator void *(); , - .
- bool operator!(); true, - .
- - . :
- ifstream .
- ofstream .
- fstream , .
istream, ostream iostream. , ( << >> , , ..).
, , -, , -, , .
.
- , :
- ifstream();
- ofstream();
- fstream();
- , , . , ( Windows );
- ifstream(const char *name, int mode=ios::in, long prot=0666);
- ofstream(const char *name, int mode=ios::out, long prot=0666);
- fstream(const char *name, int mode, long prot = 0666);
- , . :
- ifstream(int file);
- ofstream(int file);
- fstream(int file);
- , ; :
- ifstream(int file, char *buf, int len);
- ofstream(int file, char *buf, int len);
- fstream(int file, char *buf, int len);
, , open(), .
mode, , . ios , 11.3.
11.3. ios
. | |
ate | . |
binary | ( ) . |
in | . |
out | . |
trunc | , . |
OR. ifstream ofstream mode ios::in ios::out.
close(), .
, .
failbit.