bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

assignment-s-21.c (1955B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 21 Jan 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a program to copy the contents of a text file to another file, after removing all
      9 white spaces (spaces, tabs, newlines). */
     10 
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 #include <string.h>
     14 
     15 #define NEW_FILE_PREFIX "cleaned_"
     16 
     17 int cleaning(FILE *);
     18 
     19 int main()
     20 {
     21     char filename[50];
     22     char *newFilename = NULL;
     23     FILE *givenFile = NULL;
     24     FILE *cleanedFile = NULL;
     25     int len, temp;
     26 
     27     printf("Enter the filename (Max: 50 character): ");
     28     fgets(filename, sizeof(filename), stdin);
     29     len = strlen(filename);
     30     if (filename[len - 1] == '\n')
     31     {
     32         filename[len - 1] = '\0';
     33     }
     34 
     35     givenFile = fopen(filename, "r");
     36     if (givenFile == NULL)
     37     {
     38         printf("\nCouldn't opened the file \"%s\""
     39                "\nPlease ensure that \"%s\" is in the same folder.",
     40                filename, filename);
     41         return 1;
     42     }
     43 
     44     len += strlen(NEW_FILE_PREFIX);
     45     newFilename = (char *)malloc((len + 1) * sizeof(char));
     46     if (newFilename == NULL)
     47     {
     48         printf("\nMemory allocation failed.");
     49         fclose(givenFile);
     50         return 1;
     51     }
     52     newFilename[0] = '\0';
     53     strcat(newFilename, NEW_FILE_PREFIX);
     54     strcat(newFilename, filename);
     55 
     56     cleanedFile = fopen(newFilename, "w");
     57     if (cleanedFile == NULL)
     58     {
     59         printf("\nCUnable to create the file \"%s\"", newFilename);
     60         free(newFilename);
     61         return 1;
     62     }
     63 
     64     while ((temp = fgetc(givenFile)) != EOF)
     65     {
     66         if (temp != ' ' && temp != '\n' && temp != '\t')
     67         {
     68             fputc(temp, cleanedFile);
     69         }
     70     }
     71 
     72     printf("\nSuccessfully cleaned: \"%s\"", filename);
     73     printf("\nGenerated file: \"%s\"", newFilename);
     74 
     75     fclose(givenFile);
     76     fclose(cleanedFile);
     77     free(newFilename);
     78 
     79     return 0;
     80 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror