Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- void printHelp()
- {
- printf("Usage: fstring [ char ] [ amount ] Optional:[ outfile ]n"
- "Example: fstring A 100 out.txtn");
- exit(1);
- }
- char *f_string(const char *s, int t)
- {
- int i; char *dst = malloc(t * strlen(s) + 1);
- for(i = 0; i < t; i++) {
- strcat(dst, s);
- }
- return dst;
- }
- int main(int argc, char *argv[])
- {
- char c; char *file;
- while((c = getopt(argc, argv, "f:")) != -1)
- switch(c) {
- case 'f':
- file = optarg;
- break;
- default:
- printHelp();
- return 1;
- }
- if(argc < 3) {
- printf("You need at least two arguments!n");
- return 1;
- }
- char *res = f_string(argv[1], atoi(argv[2]));
- FILE *f = fopen(file, "w+");
- if(!f) {
- puts(res);
- exit(0);
- } else {
- fprintf(f, "%s", res);
- }
- fclose(f);
- free(res);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- char line[80];
- if(argc != 6) {
- fprintf(stderr, "You need to give 5 arguments!n");
- return 1;
- }
- FILE *in;
- if(!(in = fopen("spooky.csv", "r"))) {
- fprintf(stderr, "File %s doesn't exist!", in);
- return 1;
- }
- FILE *file1 = fopen(argv[2], "w");
- FILE *file2 = fopen(argv[4], "w");
- FILE *file3 = fopen(argv[5], "w");
- while(fscanf(in, "%79s", line) == 1) {
- if(strstr(line, argv[1]))
- fprintf(file1, "%sn", line);
- else if (strstr(line, argv[3]))
- fprintf(file2, "%sn", line);
- else
- fprintf(file3, "%sn", line);
- }
- fclose(file1);
- fclose(file2);
- fclose(file3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement