Compare commits
3 Commits
a1f8b096e3
...
9d08631b32
Author | SHA1 | Date | |
---|---|---|---|
|
9d08631b32 | ||
|
db14adeb86 | ||
|
bfef02de49 |
1
code/C/.gitignore
vendored
1
code/C/.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
fgets-simple
|
fgets-simple
|
||||||
|
demo-strtok
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
# exemples pour le chapitre sur le C
|
# exemples pour le chapitre sur le C
|
||||||
# new Sat Feb 11 12:06:34 CET 2023
|
# new Sat Feb 11 12:06:34 CET 2023
|
||||||
|
|
||||||
|
all: no-op slowprint fgets-simple
|
||||||
|
|
||||||
no-op: no-op.c Makefile
|
no-op: no-op.c Makefile
|
||||||
gcc -Wall $< -o $@
|
gcc -Wall $< -o $@
|
||||||
|
|
||||||
@ -11,3 +13,5 @@ slowprint: slowprint.c Makefile
|
|||||||
fgets-simple: fgets-simple.c Makefile
|
fgets-simple: fgets-simple.c Makefile
|
||||||
gcc -Wall $< -o $@
|
gcc -Wall $< -o $@
|
||||||
|
|
||||||
|
demo-strtok: demo-strtok.c Makefile
|
||||||
|
gcc -Wall $< -o $@
|
||||||
|
52
code/C/demo-strtok.c
Normal file
52
code/C/demo-strtok.c
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* demo strtok
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
/*---------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void dump_line(char *titre, char *ligne, int combien)
|
||||||
|
{
|
||||||
|
int foo;
|
||||||
|
unsigned char *ptr;
|
||||||
|
|
||||||
|
printf(" --- %s\n", titre);
|
||||||
|
ptr = (unsigned char *)ligne;
|
||||||
|
for (foo=0; foo<combien; foo++) {
|
||||||
|
printf("%02x ", ptr[foo]);
|
||||||
|
}
|
||||||
|
puts("");
|
||||||
|
for (foo=0; foo<combien; foo++) {
|
||||||
|
printf(" %c ", isprint(ptr[foo]) ? ptr[foo] : '?');
|
||||||
|
}
|
||||||
|
puts("");
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
char input[] = "foo bar quux quiz";
|
||||||
|
char *cptr, *copie;
|
||||||
|
int foo;
|
||||||
|
|
||||||
|
dump_line("original", input, sizeof(input));
|
||||||
|
copie = strdup(input);
|
||||||
|
dump_line("copie", copie, sizeof(input));
|
||||||
|
puts("");
|
||||||
|
|
||||||
|
cptr = strtok(copie, " \t");
|
||||||
|
dump_line("1er strtok", copie, sizeof(input));
|
||||||
|
printf(" got [%s]\n", cptr);
|
||||||
|
puts("");
|
||||||
|
|
||||||
|
cptr = strtok(NULL, " \t");
|
||||||
|
cptr = strtok(NULL, " \t");
|
||||||
|
dump_line("2eme & 3me strtok", copie, sizeof(input));
|
||||||
|
foo = cptr - copie;
|
||||||
|
printf(" index = %d\n", foo);
|
||||||
|
printf(" and we have [%s]\n", input+foo);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------*/
|
@ -1,14 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
* exemple utilisation 'fgets'
|
* exemple d'utilisation de 'fgets'
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#define TL 8
|
#define TL 12
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char buffer[TL+1];
|
char buffer[TL+1];
|
||||||
while( fgets(buffer, TL, stdin) ) {
|
while( fgets(buffer, TL, stdin) ) {
|
||||||
printf(" %4ld\n", strlen(buffer));
|
printf(" %4ld %s\n", strlen(buffer), buffer);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -11,19 +11,21 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
#define BINDADDR "127.0.0.1"
|
#define BINDADDR "127.0.0.1"
|
||||||
#define TL 20
|
#define BINDPORT 5678
|
||||||
|
|
||||||
|
#define TL 20 /* important value */
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int foo, len, sock;
|
int foo, len, sock;
|
||||||
char *adr_ip = BINDADDR;
|
char *adr_ip = BINDADDR;
|
||||||
int port = 5678;
|
int port = BINDPORT;
|
||||||
unsigned int addrlen;
|
unsigned int addrlen;
|
||||||
int connected;
|
int connected;
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
struct in_addr ia;
|
struct in_addr ia;
|
||||||
FILE *fpin;
|
FILE *fpin;
|
||||||
char line[TL+5], *ptr;
|
char line[TL+5];
|
||||||
|
|
||||||
fprintf(stderr, "--- %s ---\n", argv[0]);
|
fprintf(stderr, "--- %s ---\n", argv[0]);
|
||||||
|
|
||||||
@ -71,7 +73,7 @@ for (;;) {
|
|||||||
}
|
}
|
||||||
// fprintf(stderr, "fopened...\n");
|
// fprintf(stderr, "fopened...\n");
|
||||||
|
|
||||||
while ( (ptr=fgets(line, TL, fpin)) ) {
|
while ( fgets(line, TL, fpin) ) {
|
||||||
len = strlen(line);
|
len = strlen(line);
|
||||||
fprintf(stderr, " %3d | %s", len, line);
|
fprintf(stderr, " %3d | %s", len, line);
|
||||||
for (foo=0; foo<len; foo++) {
|
for (foo=0; foo<len; foo++) {
|
||||||
@ -79,7 +81,8 @@ for (;;) {
|
|||||||
}
|
}
|
||||||
fputs("\n", fpin);
|
fputs("\n", fpin);
|
||||||
}
|
}
|
||||||
close(connected);
|
fclose(fpin);
|
||||||
|
// close(connected);
|
||||||
} /* end of the endless loop */
|
} /* end of the endless loop */
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user