I have searched and only found this information for console, but I was wondering if it is possible to read text from a file on my local machine into the code, format it, and display in on screen? We have a text file with some legal jargon that can be updated periodically, and instead of having the user sift through code, we were wanting to just update the text file and have the changes apply online.
Thanks!
EDIT: Thanks to everyone's comments, here is an edit with the requirements. The program is in C# ASP.NET website. I have read many articles about this being done in a console, but I am not sure how to make it work for me. Thanks again for everybody's contribution.
Happy coding. You can copy paste the below C Program to read text from a file Using while fgetc () function, in c compiler to check how the source code work. After you compile and run the above c program to read text from a file and display, your C compiler asks you to enter the filename to read the text and display.
After you compile and run the above c program to read text from a file and display, your C compiler asks you to enter the filename to read the text and display. After you enter a .txt file name, the compiler will read the file and display its text.
This kind of data is typically stored in files on disk. Since reading from files involves interacting with your operating system, it amounts to a rather complex task. In this article, we’ll look at C++ streams, file handling, and three different methods for reading data from a file into a C++ program.
In C++, the workflow of reading a file is a bit more complex — there is the added step of reading to or from a stream object. To understand how our C++ programs interact with files, let us now take a look at the concept of streams in C++. What Are C++ Streams? C++ streams are interfaces for processing sequence-like input and output.
You have the complete program (ASP.net). You must have a file inside the App_Data
folder inside your ASP.net app, In this App your file name "Details.txt" which should be available inside your App_Data
folder.
You have Hidden-field and a paragraph available in your web page. When form loads, at that moment read the data from the text file and populate to the Hidden-field control. And in $(document).ready()
Jquery function populate data to paragraph from Hidden-field.
Your .aspx
page :
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ReadFromTextFileToTextBoxWebApp._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<style type="text/css" >
.details
{
background-color:Purple;color:yellow;top: 100px;
}
.txtDetails
{
left:150px;width:200px;height:100px;
}
</style>
<script src="Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
var data = $("#<%=HiddenField1.ClientID %>").val();
$('#pTextData').text(data);
});
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<p id="pTextData">
</p>
</div>
</asp:Content>
and here is your code behind page :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace ReadFromTextFileToTextBoxWebApp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var data = File.ReadAllText(Server.MapPath("~/App_Data/Details.txt"));
HiddenField1.Value = data.ToString();
}
}
}
Here are two methods to work in .Net
var legal = File.ReadAllText(@"C:\Legal\Legalease.txt");
// Or from the CWD of where the program is executing
var legal = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "Legalease.txt"));
Remember the Asp.Net is running as a user defined in IIS's application pool for that website. If the user does not have read access to where the file exists, it cannot be read. Make sure the user defined in the website's application pool has the right to read the file and verify the file has been published to the read location.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With