Question

In: Computer Science

In this lab, you will learn to read data from a sequential-access text file. To read...

In this lab, you will learn to read data from a sequential-access text file. To read data from a file, you need one FileStream object and one StreamReader object. The StreamReader object accepts the FileStream object as its argument.
To read data from a sequential-access text file, here's what you need to do:

  1. From the desktop, open Visual Studio 2017.
  2. From the menu bar, navigate to File > New > Project.
  3. In the New Project window, select Windows Forms App (.NET Framework).
  4. Type Name as StudentData and verify that the Solution name is automatically set as StudentData and click OK to create a new project with a textbox and button to open file information that contains student details.
  5. From the left side, navigate to Toolbox > All Windows Forms, drag Label and drop it on the blank area of the Form1 section.
  6. In the right pane, in the Properties side panel, under the Appearance section, remove the Text field's default value, i.e., label1.
  7. From the left side, navigate to Toolbox > All Windows Forms, drag Button, and drop it on the blank area of the Form1 section.
  8. In the right pane, in the Properties section, type Text as Open File.
  9. Double-click the Open File button.
  10. In the Form1.cs window, under using System.Windows.Forms, type the following code to include the System.IO library:Copy
    using System.IO;
  11. In the Form1.cs window, under private void button1_Click(object sender, EventArgs e), type the following code to declare and define an object, obtain a path for the text file, and display the file name encoded to UTF8 as output:Copy
     
    1. OpenFileDialog openFileDialog1 = new OpenFileDialog();
    2. openFileDialog1.ShowDialog();
    3. String file_name = openFileDialog1.FileName;
    4. string text = File.ReadAllText(@file_name, Encoding.UTF8);
    5. label1.Text = text;
  12. From the menu bar, navigate to Debug > Start Without Debugging and wait for the Form1 window to appear.
  13. In the Form1 window, click Open File.
  14. In the Open window, in the left pane, navigate to Local Disk (C:), select (say) the ip.txt file and click Open. You will see the file name encoded to UTF8.
  15. Close all the windows.

Solutions

Expert Solution

/* Please see the source code */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.ShowDialog();
String file_name = openFileDialog1.FileName;
string text = File.ReadAllText(@file_name, Encoding.UTF8);
label1.Text = text;
}
}
}

/* See the editor screen*/

/* Output */

/* Output */

/* That's all. Thanks */

:)


Related Solutions

In this lab, you open a file and read input from that file in a prewritten...
In this lab, you open a file and read input from that file in a prewritten C++ program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers.dat. Instructions Ensure the source code file named Flowers.cpp is open in the code editor. Declare the variables you will need. Write the C++ statements that will open the input file flowers.dat for reading....
C++ Question 2 You will read in data about the planets from a text file, and...
C++ Question 2 You will read in data about the planets from a text file, and then print out the data about that planet when the user requests the data. You will need to create a Planet Class (since this assignment does not cover templates, you are expected to split your class into .cpp and .h files), as well as a .cpp/.h library with the functions described below Planet Class For the first phase of the question, you will create...
You are required to read in a list of stocks from a text file “stocks.txt” and...
You are required to read in a list of stocks from a text file “stocks.txt” and write the sum and average of the stocks’ prices, the name of the stock that has the highest price, and the name of the stock that has the lowest price to an output file. The minimal number of stocks is 30 and maximal number of stocks in the input file is 50. You can download a input file “stocks.txt” from Canvas. When the program...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
Problem Statement You are required to read in a list of stocks from a text file...
Problem Statement You are required to read in a list of stocks from a text file “stocks.txt” and write the sum and average of the stocks’ prices, the name of the stock that has the highest price, and the name of the stock that has the lowest price to an output file. The minimal number of stocks is 30 and maximal number of stocks in the input file is 50. You can download a input file “stocks.txt” from Canvas. When...
Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
In this lab, you will read and write to a file, as well as sorting the...
In this lab, you will read and write to a file, as well as sorting the information. Copy and paste the following into a file named "inputs.txt": 7 199922007 C.J. Cregg Political_Science 200822013 Olivia Dunham Criminal_Justice 199822007 Josh Lyman Law 199922006 Toby Ziegler Communications 200922015 Leslie Knope Public_and_Environmental_Affairs 199922004 Sam Seaborn Law 200722013 Walter Bishop General_Sciences The input file provides details for a student database in the following format: Number_of_students ID_Number Student_First_Name Student_Last_Name Major …… ID_Number Student_First_Name Student_Last_Name Major Your...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT