Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read from text file?

Status
Not open for further replies.

lynque

IS-IT--Management
Sep 30, 2004
124
CA
Hello all,

I am trying to set up a "tip of the day" feature for one of our sites which reads the term from a text file, I am relatively new to C# so I am kind of struggling a little.

Code:
public class textFromFile 
{
    private const string FILE_NAME = "termOfTheDay.txt";
    public static void Main(String[] args)
    {
        if (!File.Exists(FILE_NAME)) 
        {
            ("{0} does not exist.", FILE_NAME);
            return;
        }
        StreamReader todaysTerm = File.OpenText(FILE_NAME);
        String input;
        while ((input=todaysTerm.ReadLine())!=null) 
        {
            (input);
        }
        ("The end of the stream has been reached.");
        todaysTerm.Close();
    }
}

Here is the call from the body of the page

Code:
<%=todaysTerm%>

Any help is much appreciated
 
<%=todaysTerm%> Attempts to call a function called todaysTerm.

In your class there is no such function. todaysTerm is a local variable, which means it has scope, and so is only active for as long as the Main[] function is operating.

How are you creating an instance of the class?
 
Thanks for the quick response Kalisto,

After googling it I discovered I hadn't created an instance of the class, as I said I'm green with C#.

If I understand correctly, I set the class and define some properties within it.

I have this class with todaysTerm as aproperty of it

Code:
public class textFromFile 
{

public string todaysTerm;

}
 
For starters, I would create a label running server side and set the value from the code behind in your routine.

Code:
<asp:Label ID="lblMyLabel" Runat="Server"/>

Then in your code do something like:

Code:
public class textFromFile
{
    private const string FILE_NAME = "termOfTheDay.txt";
    public static void Main(String[] args)
    {
        if (!File.Exists(FILE_NAME))
        {
            ("{0} does not exist.", FILE_NAME);
            return;
        }
        StreamReader todaysTerm = File.OpenText(FILE_NAME);
        String returnString;
        while ((string input=todaysTerm.ReadLine())!=null)
        {
            returnString = input;
        }
        ("The end of the stream has been reached.");
        todaysTerm.Close();
        lblMyLabel.Text = returnString;
    }
}

I only modified the code the way I did for simplicity's sake. Personally, I wouldn't do it like this as you never know what's in that file you are reading AND it will only give you the last line of text in your file. Just something to think about. Does that help at all?

&quot;It's easier to ask forgiveness than it is to get permission.&quot; - Rear Admiral Dr. Grace Hopper
 
Thanks Brainiac,

I ended up doing something different after reading about this at 4guysfromrolla, tried to find the url but couldn't so here is the code.

Code:
string line = "";
string mynewline = "";
int days;
int tiptoshow;

void Page_Load(){
	// get days since 1970
	DateTime dtCurTime = DateTime.Now; 
	DateTime dtEpochStartTime = Convert.ToDateTime("1/1/1970 8:00:00 AM"); 
	TimeSpan ts = dtCurTime.Subtract(dtEpochStartTime); 
	long epochtime; 
	days = ts.Days; 
	
	// do the mod, choose your tip
	int tipcount=10; // this is the number of tips in the txt file. hard-coded.
	tiptoshow = days % tipcount;

	StreamReader sr = new StreamReader("Server.Path/.txt");
	int i=0;
	string line = sr.ReadLine();
	while (line != null && i<tiptoshow){
		line = sr.ReadLine();
		i++;
	}
	mynewline=line;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top