0
1.4kviews
What do you mean by FILE? Write a program to copy text from one file to other after converting lower case latters to upper case and vice versa. Keep other characters as it is.

Mumbai University> FE > Sem 2> STRUCTURED PROGRAMMING APPROACH

Marks: 10 M

Year: May 2016

1 Answer
0
21views

Files: A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage of data. It is a ready made structure.

There are five major operations that can be performed on a file are:

  1. Creation of file

  2. Opening an existing file

  3. Reading data from a file

  4. Writing data in a file

  5. Closing a file

Program:

#include <stdio.h>
#include <string.h> 
#define MAX 256

  int main() {
        int ch;
        FILE *fp1, *fp2;
        char file1[MAX], file2[MAX];

        /* get the file1 file name from the user */
        printf("Enter your source file name:");
        fgets(file1, MAX, stdin);
        file1[strlen(file1) - 1] = '\0';

        /* get the destination file name from the user */
        printf("Enter your destination file name:");
        fgets(file2, MAX, stdin);
        file2[strlen(file2) - 1] = '\0';

        /* open the first file in read mode */
        fp1 = fopen(file1, "r");

        /* open the destination file in write mode */
        fp2 = fopen(file2, "w");


        /* coverts Uppercase to lowercase or vice versa */
        while (!feof(fp1)) 
         {
                    ch = fgetc(fp1);
                if (ch == EOF) 
{
                            continue;
                  } 
else if (ch >= 'A' && ch <= 'Z') 
{
                            fputc((ch - 'A' + 'a'), fp2);
                    } 
else if (ch >= 'a' && ch <= 'z') 
{
                            fputc((ch - 'a' + 'A'), fp2);
                    } 
else 
{
                            fputc(ch, fp2);
                    }
            }

        /* close the opened files */
        fclose(fp1);
        fclose(fp2);

        return 0;
  }
Please log in to add an answer.