-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringtest.c
65 lines (50 loc) · 1.31 KB
/
stringtest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1000
int main() {
char dataPort[6];
//char delim1[] = " ";
//char delim2[] = "0123456789";
int idx;
char fileName[BUFFER_SIZE];
char buffer[BUFFER_SIZE] = "g blorpts1.txt 1234";
/*
memset(dataPort, '\0', sizeof dataPort);
memcpy(dataPort, buffer+(strlen(buffer)-5), 5);
memcpy(fileName, buffer+2, strlen(buffer)-8); //get fileName - starts after 'g ' and before port ' #####\n'
fileName[strlen(buffer)-8] = '\0';
printf("buffer: %s\n", buffer);
printf("file name: %s\n", fileName);
printf("port: %s\n", dataPort);
*/
//idx = strcspn(buffer, delim1); //get position of 1st character in port
//printf("idx: %d, char: %c\n", idx, buffer[idx]);
int i = 0;
char * p;
printf ("Splitting string \"%s\" into tokens:\n", buffer);
p = strtok (buffer, " ");
//while (p != NULL) {
//command
printf ("%d: %s\n",i, p);
p = strtok (NULL, " ");
i++;
//file name
memcpy(fileName, p, strlen(p));
printf ("%d: %s\n",i, p);
p = strtok (NULL, " ");
i++;
//port
memcpy(dataPort, p, strlen(p));
printf ("%d: %s\n",i, p);
p = strtok (NULL, " ");
i++;
//}
printf ("%d: %s\n",i, p);
p = strtok (NULL, " ");
i++;
printf("buffer: %s\n", buffer);
printf("file name: %s\n", fileName);
printf("port: %s\n", dataPort);
return 0;
}