In: Computer Science
A. Open/create the file bank.txt for writing "w". Enter a character from the keyboard and write it to the file. Close the file. In the same program open the file for reading "r" and read the character from the file and print it on screen.
Answer:-
******************** C Language Code ****************
Steps to develop below code:
->Open your Turbo C++ -> click on "file" menu -> select "New" option
Then write the below code(copy and paste)
-> click on "file" menu -> then select "save"
-> enter your file name as your wish but extension is ".c" -> click on "ok" button
->click on "compile" menu from top -> then select "compile" -> it will show errors and warnings
->at last click on "Run" menu -> then select "Run" -> it will run the program and it will ask the user for the enter input character from the keyboard. and -> display the character from the file.
********** FileReadAndWrite.c ************
#include<stdio.h>
#include<conio.h>
//header files
int main()
{
FILE *fp;//file declaration
char c; //character declaration
fp=fopen("C:\\bank.txt", "w");//it will create / open a file in
write "w" mode
if(fp==NULL) // checking null
{
printf("\nFile does not created!!");
exit(0);
}
printf("\nFile created successfully");
printf("\nEnter any character: "); //asking user for the
input
scanf("%c", &c); //reading input
fputc(c, fp); //put character into file
printf("\nData written successfully");
fclose(fp); //closing file
fp=fopen("C:\\bank.txt", "r"); //open / read "r" file
if(fp==NULL){
printf("\nCan't open file!!");
exit(0);
}
printf("\nContents of file is :\n");
printf("%c", getc(fp)); //display the contents of the file
fclose(fp); //closing a file
getch();
return 0;
}
********************FileReadAndWrite.c Output ***********
**************************** JAVA Language Code*******************
****ReadAndWriteFile.java****
package payrollsystem_phase2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ReadAndWriteFile {
public static void main(String[] args) throws IOException {
createFileAndStoreData();
openAndReadDataFile();
}
//file creation and write the data into a file
public static void createFileAndStoreData() throws
IOException{
// attach a file to
FileWriter
//you can specify your own
filepath(directory) like D:\app\myfiles\bank.txt
FileWriter fw=new FileWriter("bank.txt"); //create the file
bank.txt for writing "w".
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character to store it into a file:
");
// Character input
char c = scanner.next().charAt(0);
fw.write(c);
System.out.println("Writing successful");
File f = new File("bank.txt");
//Print absolute path
System.out.println("Location of file: "+f.getAbsolutePath());
//close the file
fw.close();
}
//open the file for reading "r" and read the character
from the file and print it on screen.
public static void openAndReadDataFile() throws
IOException{
// variable declaration
int ch;
// check if File exists or not
FileReader fr=null;
try
{
fr = new FileReader("bank.txt");
}
catch (FileNotFoundException fe)
{
System.out.println("File not found");
}
// read from FileReader till the end of file
System.out.println("Read Data:");
while ((ch=fr.read())!=-1)
System.out.print((char)ch);
// close the file
fr.close();
}
}
**************ReadAndWriteFile.java Output **************
**Please hit the like button, if you have any doubt please comment me.
Thank You.
X Problems @ Javadoc Declaration Console X <terminated> ReadAndWriteFile [Java Application] C:\Program Files Java\jdk1.8.0_181\bin\javaw.exe (Jul 16, 2020, 7:32:00 PM) Enter a character to store it into a file: H Writing successful Location of file: C:\Users\THINK\Documents\srikanth-eclipse-workspace\cg\bank.txt Read Data: H