-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringMaker.c
36 lines (34 loc) · 975 Bytes
/
stringMaker.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
/* @Copyright Fred Zhang Rose-Hulman Instuti
A simple stricp that simplies your life in OS after Milestone 3.
Simple compile it with gcc and run it.
It will gives two exmaple of how to use it.
It generates C code that construct local char array and fill it.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char** args) {
char* name;
char* content;
FILE* out = stdout;
if (argc != 2) {
name = args[1];
content = args[2];
} else if (argc != 3){
name = args[1];
content = args[2];
out = fopen(args[3], "w");
} else {
fprintf(stderr, "%s <VarialbeName> <StringContent>\n", args[0]);
fprintf(stderr, "%s <VarialbeName> <StringContent> <OutputFile>\n", args[0]);
exit(1);
}
int len = strlen(content);
fprintf(out, "char %s[%u];\n", name, len + 1);
int i;
for (i = 0; i < len ; i++) {
fprintf(out, "%s[%d] = '%c';\n", name, i, content[i]);
}
fprintf(out, "%s[%d] = '\\0';\n", name, len);
fclose(out);
}