You are on page 1of 6

Page | 0

Problem No: 01.

Problem Name: Write a program that will read a C file and output its contents as a text file.
The "Hello World" message is printed using code in a C file.

Algorithm:
Here is the algorithm based on the code, step by step:
1. First, we create a FileReader object, "fr", and initialize it with the input file name
"input.c". This FileReader will be responsible for reading characters from the input file.
2. Next, we create a FileWriter object, "fw", and initialize it with the output file name
"output.txt". This FileWriter will be responsible for writing characters to the output file.
3. We create a character array, "buffer", of a suitable size to store the characters read from
the file. Using a buffer allows us to read and write characters in chunks, improving
performance when dealing with large files.
4. An integer variable, "numChars", is created to store the number of characters read into
the buffer.
5. We enter a loop that reads characters from the input file using the FileReader object "fr"
and stores them in the buffer. The loop continues until the FileReader returns -1 or the end
of the file is reached. This approach reads a chunk of characters at a time, rather than
reading them one by one, reducing the number of file access operations and improving
efficiency.
6. Inside the loop, we write the characters from the buffer to the output file using the
FileWriter object "fw" and its write () method. We write only the number of characters
specified by "numChars" to avoid writing any uninitialized or extra characters.
7. After the loop ends, we close the FileReader and FileWriter objects to release system
resources.
8. We print a message indicating that the file reading and writing process is complete,
providing feedback to the user about the successful execution of the program.
9. If any IOException occurs during the file reading or writing process, we catch the
exception and print an appropriate error message to handle exceptional scenarios.
10. The program execution ends.

Page | 1
Code:

Page | 2
Output:
input.c file:

Page | 3
Output.txt file:

Page | 4
Discussion:
In this lab, we replace the cFilePath variable with the actual path and also replace the
outputFilePath variable with the desired path for the output text file.When we run the
program and the content of the C file will be read and saved to the specified output file path
and have to handle any potential exceptions that may occur during file operations, such as
IOExceptions and adjust the file paths according to specific setup.
During the implementation of this code, some potential issues and enhancements should be
considered:
File rights: Verify that the input and output files for the application have the correct read and
write rights. If not, an IOException will happen.
Handling huge files: Memory problems may arise if a large file's full contents are read into
memory as a single string. For improved efficiency and memory management, think about
adopting buffering strategies or reading the file line by line.
File Not Found: A FileNotFoundException will be issued if the input file is missing or is in a
different directory. It is important to provide proper file existence checks and error handling.
Encoding Problems: When reading and writing files, the code uses the default encoding.
Use the appropriate constructors for FileReader and FileWriter, providing the desired
encoding, if the input file has a different encoding or if you want to explicitly indicate the
encoding.
Error Handling: To offer relevant error messages and gracefully manage exceptions,
adequate error handling should be performed in case of any IOException.
Here are some typical difficulties that might occur while using file input and output in Java.
When interacting with files in your code, it's crucial to handle errors correctly, verify file
existence and rights, and take performance and resource management considerations into
account.

Page | 5

You might also like