Question

In: Computer Science

A. Open/create the file bank.txt for writing "w". Enter a character from the keyboard and write...

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.

Solutions

Expert Solution

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


Related Solutions

Write an LC-3 program that will repeatedly read a character from the keyboard. For each character...
Write an LC-3 program that will repeatedly read a character from the keyboard. For each character read in, your program will print a neat message that echoes the input value, and the ASCII character code of the input character in hexadecimal. It will run forever: no HALT or End of processing is required. For example: Please press a key: You pressed 'z' which is x7A Please press a key: You pressed '@' which is x40 Please press a key: You...
Create a c file to read from an existing file one character at a time and...
Create a c file to read from an existing file one character at a time and print that character to the console Create a c file to read a character from the standard input and write it to a file. please help me
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
In C++, The following program reads one character from the keyboard and will display the character...
In C++, The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit. Modify the program below such that if one of the whitespaces is entered, it displays a message and tells what the character was. // This program reads one character from the keyboard and will //...
Write an interactive C program that asks a user to enter a sentence from the keyboard,...
Write an interactive C program that asks a user to enter a sentence from the keyboard, and prints the sentence with all the words spelled backwards. Note: you may assume that each sentence will have at most 50 characters and each word is separated by a space. Sample code execution: bold information entered by user enter a sentence: hello ece 175 class olleh ece 571 ssalc continue (q to quit)? y enter a sentence: May the force be with you...
write a C++ program that uses function swap to enter three real value from the keyboard...
write a C++ program that uses function swap to enter three real value from the keyboard and outputs there in order from minimum to the maximum. For example 10,100,30 if were entered then the output would be 10,30,100. The program should use function get_data(a,b,c) and print_data(a,b,c)
Write a program that will write the contents of a text file backwards one character at...
Write a program that will write the contents of a text file backwards one character at a time. Your program should first ask for the text file name to be processed – and after verifying that the file exists, it should read the file into a StringBuilder buffer. Save the new file using the old filename appended with “Ver2-“ at the front. So for the file “MyStuff.txt” the file would be saved as Ver2-MyStuff.txt”. You can use any text file...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
Write a program that allows the user to enter two integers and a character If the...
Write a program that allows the user to enter two integers and a character If the character is A, add the two integers If it is S, subtract the second integer from the first else multiply the integers Display the results of the arithmetic
In java, write a program that asks the user to enter a character. Then pass the...
In java, write a program that asks the user to enter a character. Then pass the character to the following methods. isVowel() – returns true if the character is a vowel isConsonant() – returns true if the character is a consonant changeCase() – if the character is lower case then change it to upper case and if the character is in upper case then change it to lower case. (return type: char) Example output is given below: Enter a character...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT