C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Problem Statement
  • Algorithm
  • Explanation

Was this helpful?

  1. File Input and Output

Program: Converting Characters in a File to Uppercase

Problem Statement

Write a C program to read characters from a file, convert them to uppercase, and write the modified characters back to the file.

Algorithm

  1. Open File for Reading:

    • Use the fopen function to open the input file in read mode.

    FILE *inputFilePointer;
    inputFilePointer = fopen("input.txt", "r");
    • Check if the file is successfully opened.

    if (inputFilePointer == NULL) {
        // Handle file opening error
    }
  2. Open File for Writing:

    • Use the fopen function to open a temporary file in write mode.

    FILE *tempFilePointer;
    tempFilePointer = fopen("temp.txt", "w");
    • Check if the file is successfully opened.

    if (tempFilePointer == NULL) {
        // Handle file opening error
    }
  3. Read and Convert Characters:

    • Use a loop to read characters from the input file and convert them to uppercase.

    int character;
    while ((character = fgetc(inputFilePointer)) != EOF) {
        // Convert to uppercase
        character = toupper(character);
        // Write to the temporary file
        fputc(character, tempFilePointer);
    }
  4. Close Files:

    • Close both the input and temporary files.

    fclose(inputFilePointer);
    fclose(tempFilePointer);
  5. Remove Original File and Rename Temporary File:

    • Use the remove function to delete the original file.

    remove("input.txt");
    • Use the rename function to rename the temporary file to the original file name.

    rename("temp.txt", "input.txt");

Explanation

This program reads characters from the input file, converts them to uppercase using the toupper function, and writes the modified characters to a temporary file. After processing, it removes the original file and renames the temporary file to the original file name, effectively replacing the original file with the modified content.

Ensure that the input file exists and has the necessary permissions for reading and writing. This program provides a simple way to convert characters in a file to uppercase.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousFinding Your Position in a FileNextProgram: Printing the Contents of a File in Reverse Order

Was this helpful?