Question

In: Computer Science

Create C# code that can search a text file and output the data at the line...

Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed.

Example of call in command window:

Search16s filename.txt 273   10

Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output.

The number of sequences entered on call should always be a odd number or give an error in console.

The output should also display in the console.

Solutions

Expert Solution

using System;

using System.IO;

class MainClass

{

public static void Main(String[]args)

{

Console.Write("\nEnter the name of the file: ");

string fileName = Console.ReadLine();

if(!File.Exists(fileName))

{

Console.WriteLine("Could not locate file: " + fileName);

}

else

{

string[] allLines = File.ReadAllLines(fileName);

int counter = allLines.Length;

Console.WriteLine("\nFile read complete: Total " + counter + " lines read");

Console.Write("Enter the line number: ");

int lineNum = Int32.Parse(Console.ReadLine());

while(lineNum < 0 || lineNum > counter)

{

Console.WriteLine("Input for line number exceeded the file content!");

Console.Write("\nEnter the line number: ");

lineNum = Int32.Parse(Console.ReadLine());

}

string line = allLines[lineNum - 1];

Console.Write("Enter a sequence number for the line (must be odd): ");

int sequenceNum = Int32.Parse(Console.ReadLine());

while(sequenceNum < 0 || sequenceNum > line.Length)

{

Console.WriteLine("Input for sequence number exceeded the line length");

Console.Write("\nEnter a sequence number for the line (must be odd): ");

sequenceNum = Int32.Parse(Console.ReadLine());

}

while(sequenceNum % 2 == 0)

{

Console.WriteLine("Input for sequence number must be odd");

Console.Write("\nEnter a sequence number for the line (must be odd): ");

sequenceNum = Int32.Parse(Console.ReadLine());

}

string result = line.Substring(0, sequenceNum);

Console.WriteLine("\nResult: " + result);

}

}

}

***************************************************************** SCREENSHOT **********************************************************

INPUT FILE:


Related Solutions

Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
Given some data in a text file, the task is to scramble the text and output...
Given some data in a text file, the task is to scramble the text and output in a separate text file. So, we need to write a Python program that reads a text file, scrambles the words in the file and writes the output to a new text file. Rules to be followed: Words less than or equal to 3 characters need not be scrambled. Don’t scramble first and last char, so Scrambling can become Srbmnacilg or Srbmnailcg or Snmbracilg,...
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
C Programming How do you read in a text file line by line into a an...
C Programming How do you read in a text file line by line into a an array. example, i have the text file containing: line 1: "qwertyuiop" line 2: "asdfghjkl" line 3: "zxcvbnm" Then in the resulting array i get this: array:"qwertyuiopasdfghjklzxcvbnm"
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an error message if the input cannot be converted I can give an thumbs up! :)
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT