Write a program that reads a file from a standard input and rewrites the file to
ID: 3836656 • Letter: W
Question
Write a program that reads a file from a standard input and rewrites the file to standard output, replacing all tab characters ‘ ’ with the appropriate number of spaces. Make the distance between tab columns a constant and set it to 3. Then expand tabs to the number of spaces necessary to move to the next tab column. That may be less than three spaces. For example, consider the line containing “ | | |”. The first tab is changed to three spaces, the second to two spaces, and the third to one space. Write the program using Java. Also, take a screenshot of the output. Write a program that reads a file from a standard input and rewrites the file to standard output, replacing all tab characters ‘ ’ with the appropriate number of spaces. Make the distance between tab columns a constant and set it to 3. Then expand tabs to the number of spaces necessary to move to the next tab column. That may be less than three spaces. For example, consider the line containing “ | | |”. The first tab is changed to three spaces, the second to two spaces, and the third to one space. Write the program using Java. Also, take a screenshot of the output. Write the program using Java. Also, take a screenshot of the output.Explanation / Answer
Here I am explain the sample code for the given problem.
In C programming, file is a place on your physical disk where information is stored.
-Create a variable of type "FILE*".
-Open the file using the "fopen" function and assign the "file" to the data variable.
-Check to make sure the file was successfully opened by checking to see if the data == NULL. Otherwise error occurred.
- fprintf or fscanf functions to write/read from the file.
Here I am providing sample code for file operation for reading and writing
Normally file operations include
-Creating a new file
-Opening an existing file
-Closing a file
-Reading from and writing information to a file
-For reading and writing to a text file, the functions arefprintf() and fscanf().
-They are just the file versions of printf() and scanf(). The only difference is that, fprint and fscanf expects a pointer to the structure FILE.
#include <stdio.h>
#include <string.h>
FILE * test= fopen("file name", "r"); // it specifies file for read only operation
FILE *outputFile = fopen("file name", "w"); // it specifies file for write only operation
// to check whether is file is present or not
if (inputFile == NULL || outputFile == NULL)
{
printf("error present ");
exit(-1);
}
// code for writing to file
fprintf(file, " %d ", integer); // writing to file operation
fprintf(stdout, " %d ", integer);
printf( " %d ", integer);
// read from keyboard
fscanf(file, "%d %d", &dat1, &dat2);
fscanf(stdin, "%d %d", & dat1, & dat2);
scanf( "%d %d", & dat1, & dat2);
//Here I am giving the sample code for the given problem
//Taking input as file
//Read the file
//Do the pattern matching with file
//Generate the output
import java.util.regex.*;
public class patternMtachingFile {
public static void main(String[] args) {
String input = "data.*";
try {
FileInputStream fstream = new FileInputStream("thomas.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String dataLine;
while ((strLine = br.readLine()) != null) {
if (Pattern.matches(input, dataLine)) {
Pattern p = Pattern.compile("'(.*?)'");
Matcher mt = Pattern.compile("(]+)\([^\) (?m)^\s*([^\]*\|<([^>]*)>[^\)]*\)").matcher(dataLine);
while (mt.search()) {
String x = mt.group(1);
String y = x.toString() + ".*";
System.out.println(b);
if (Pattern.matches(c, dataLine)) {
Pattern ptrn = Pattern.compile("<(.*?)>");
Matcher match = ptrn.matcher(dataLine);
while (match.find()) {
System.out.println(mt.group(1));
}
} else {
System.out.println("There is no matching with specified string file input");
}
}
}
}
} catch (Exception e) {
System.err.println("e: " + e.getMessage());
}
}
}