Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int is_string_numerical(char *str)
- {
- int len = strlen(str);
- int i;
- for (i = 0; i < len; ++i) {
- if (!isdigit(str[i])) {
- return 0;
- }
- }
- return 1;
- }
- void backupfile(char *filename)
- {
- FILE *file;
- FILE *backupfile;
- const int SIZE_backupfilename = strlen(filename) + 8; // TODO: we need less maybe
- char backupfilename[SIZE_backupfilename];
- strcpy(backupfilename, filename);
- strcat(backupfilename, "_backup");
- file = fopen(filename, "r");
- backupfile = fopen(backupfilename, "w");
- if (file == NULL) {
- printf("Error: could not open file %s\n", filename);
- }
- char c;
- while ((c = fgetc(file)) != EOF) {
- fputc(c, backupfile);
- }
- fclose(backupfile);
- fclose(file);
- }
- int main(int argc, char *argv[])
- {
- int howmanyspaces; // how many spaces will be converted into a tab
- if (argc < 2) { // 1 program name, 1 argument
- printf("Error: too few arguments. \"spacestotabs -h\" for help.\n");
- return 1;
- }
- if (argc > 3) { // 1 program name, 1 number, 1 filename
- printf("Warning: too many arguments. Arguments after the second one will be ignored. \"spacestotabs -h\" for help.\n");
- return 1;
- }
- // Handle first argument
- if (argv[1][0] == '-') {
- switch (argv[1][1])
- {
- case 'h': // Help content
- printf("\nUsage: spacestotabs <number> <file name>\n");
- printf("\n<number> : Number of spaces that will be converted into a tab. Number has to be positive.\n\n");
- return 0;
- default: // Parameter not found, wrong parameter
- printf("Error: wrong arguments. \"spacestotabs -h\" for help.\n");
- return 1;
- }
- } else {
- if (!is_string_numerical(argv[1])) {
- printf("Error: wrong parameter. \"spacestotabs -h\" for help.\n");
- return 1;
- }
- howmanyspaces = atoi(argv[1]);
- if (howmanyspaces <= 0) {
- printf("Error: number is too small. \"spacestotabs -h\" for help.\n");
- return 1;
- }
- }
- const int SIZE_filename = strlen(argv[2]) + 10; // TODO: we need only + 7 or 8
- char filename[SIZE_filename];
- strcpy(filename, argv[2]);
- // Backup the file first
- do {
- printf("Would you like to backup your file before the operation? [Y / N] : ");
- char c = getchar();
- if (c == 'n' || c == 'N') {
- break;
- } else if (c == 'y' || c == 'Y') {
- backupfile(filename);
- printf("Backup is created in the same directory with the name %s_backup\n", filename);
- break;
- } else {
- printf("Error: unknown input. Use letters y or n to answer.\n");
- }
- } while (1);
- // TODO : Do the operation on the original file, don't create a new one.
- const int SIZE_outfilename = SIZE_filename + 5; // TODO: less will be enough
- char outfilename[SIZE_outfilename];
- strcpy(outfilename, filename);
- strcat(outfilename, "_out");
- FILE *file;
- FILE *out;
- file = fopen(filename, "r");
- out = fopen(outfilename, "w");
- if (file == NULL) {
- printf("Error: could not open file %s\n", filename);
- }
- int spacesinarow = 0;
- char c;
- while ((c = fgetc(file)) != EOF) {
- if (spacesinarow == howmanyspaces) {
- fputc('\t', out);
- spacesinarow = 0;
- }
- if (c == ' ') {
- ++spacesinarow;
- } else {
- if (spacesinarow) {
- int i;
- for (i = 0; i < spacesinarow; ++i) {
- fputc(' ', out);
- }
- spacesinarow = 0;
- }
- fputc(c, out);
- }
- }
- fclose(file);
- fclose(out);
- printf("\nSpaces have been converted to tabs.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement