Hello,

Right, basically I'm trying to write some HTML to a file, now the problem is that I do not want to write the whole of the HTML in the C# program, incase I ever need to change the HTML file. All I want to do is change a few variables.

Now, I have this function:

public static void WebPage(int gradeA, int gradeB, int gradeC, int gradeD, int gradeE, int gradeF)
{
    // read the HTML file
    TextReader webpage = new StreamReader ("/Users//Desktop/Assignment/webpage.html");
    string LookUp;
    string str = ""; 
    while ((LookUp = webpage.ReadLine()) != null)
    {
	 string replace = "var grade1 = \"\"";
	 string replaceWITH =  "var grade1 = \"" + gradeA + "\"";
	 str = LookUp.Replace (replace, replaceWITH);
			
    }
}

Now, if I add this line:

Console.Write (str);

It will replace it on console, however, if I try to write to the HTML file, it will completely remove EVERYTHING in that file and give me nothing.

Please can you help me?

Edited 6 Years Ago by Phil++: n/a

You'll have to open a different file to write to, not the same one as the original. Once done you can delete the original and rename the 'new' file.

Hey,

Thanks for your reply. The problem is that if I open a different file, write to it, it'll only write that little section, not the whole HTML document won't it? For example, my HTML document contains a lot more lines, these are only the variables that make the HTML file work.

Thanks.

TextReader webpage = new StreamReader("/Users//Desktop/Assignment/webpage.html");
String S = webpage.ReadToEnd();
webpage.Close();

FileStream fstream = new FileStream("Same file or different", System.IO.FileMode.Create);
StreamWriter sWriter = new StreamWriter(fstream);

string replace = "var grade1 = \"\"";
string replaceWITH = "var grade1 = \"" + gradeA + "\"";
sWriter.Write(S.Replace(replace, replaceWITH));

This will rewrite everything including your changes

Hey,

Thanks for your reply. The problem is that if I open a different file, write to it, it'll only write that little section, not the whole HTML document won't it? For example, my HTML document contains a lot more lines, these are only the variables that make the HTML file work.

Thanks.

It depends on where you do the writing. Open the file outside the read loop and the write goes in the loop (after the Replace statement). Make sure you close all files.

The solution posted by Unhnd_Exception will work, just make sure the files aren't huge.

Just as an aside, I'd place a marker in the file to replace, you run less of a risk that you'll accidentally replace something you didn't want to replace, something like "#GRADEHERE#". It will also be more apparent to anyone who has to take over your code in the future.

Hey,

Thanks for all your replies, it still clears the whole document, does not even write to it.

Any ideas? :(

Shouldn't be possible.

Post your read / write code

My whole mockup file:

using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

using System;
using System.IO;


namespace Assignment1
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			int a = 0; int b = 0; int c = 0; int d = 0; int e = 0; int f = 0;
			Console.Clear();
			
			Console.WriteLine (">> Reading from file");
			
			TextReader tr = new StreamReader ("/Users//Desktop/Assignment/grades-2.dat");
			string line;
			
			if(!File.Exists("/Users//Desktop/Assignment/grades-2.dat"))
			{
				Console.WriteLine (">> File does not exist");
			}
			
			Console.Clear();
			
			while ((line = tr.ReadLine()) != null)
			{
			   if(line == "A")
			   {
				  a++;
			   }else if (line == "B")
			   {
				  b++;
			   }else if(line == "C")
			   {
				  c++;
			   }else if(line == "D")
			   {
				  d++;
			   }else if(line == "E")
			   {
				  e++;
			   }else if(line == "F")
			   {
				  f++;
			   }	
			}
			
			Graph(a, b, c, d, e, f);
			WebPage(a, b, c, d, e, f);
			
		}
		
		public static void Graph(int gradeA, int gradeB, int gradeC, int gradeD, int gradeE, int gradeF)
		{
			Console.WriteLine ("0   10   20   30   40   50   60   70   80   90   100 ");
			Console.WriteLine ("|    |   |    |    |    |    |    |    |    |     |");
			
			int resultA = gradeA * 100 / 50;
			int resultB = gradeB * 100 / 50;
			int resultC = gradeC * 100 / 50;
			int resultD = gradeD * 100 / 50;
			int resultE = gradeE * 100 / 50;
			int resultF = gradeF * 100 / 50;
		     	
        	for (int a=0; (a < resultA / 2); a++)
			{
				Console.Write("*");
			}
			
			Console.WriteLine ("(A){0}", resultA);
	
		
			for (int e=0; (e < resultB / 2); e++)
			{
				Console.Write("*");
			}
			Console.WriteLine ("(B){0}", resultB);
			
			for (int e=0; (e < resultC / 2); e++)
			{
				Console.Write("*");
			}
			
			Console.WriteLine ("(C){0}", resultC);
			
			for (int e=0; (e < resultD / 2); e++)
			{
				Console.Write("*");
			}
			
			Console.WriteLine ("(D){0}", resultD);
			
			for (int e=0; (e < resultE / 2); e++)
			{
				Console.Write("*");
			}
			
			Console.WriteLine ("(E){0}", resultE);
			
			for (int e=0; (e < resultF / 2); e++)
			{
				Console.Write("*");
			}
			
			Console.WriteLine ("(F){0}", resultF);
		
		}
		
		public static void WebPage(int gradeA, int gradeB, int gradeC, int gradeD, int gradeE, int gradeF)
		{
			// read the HTML file
			TextReader webpage = new StreamReader("/Users//Desktop/Assignment/webpage.html");
			string S = webpage.ReadToEnd();
			webpage.Close();
			
			FileStream fstream = new FileStream("/Users//Desktop/Assignment/webpage.html", System.IO.FileMode.Create);
			StreamWriter sWriter = new StreamWriter(fstream);

			string replace = "var grade1 = \"\""; 
			string replaceWITH = "var grade1 = \"" + gradeA + "\"";
			sWriter.Write(S.Replace(replace, replaceWITH));
		}
		
	}
}

Edited 6 Years Ago by Phil++: n/a

Add sWriter.Close() after your Write statement.

Edited 6 Years Ago by Momerath: n/a

Comments
Thanks for your help.

string replace = "var grade1 = ##";
string replaceWITH = "var grade1 = #" + gradeA + "#";

sWriter.Write(S.Replace(replace, replaceWITH));
sWriter.Flush();
fstream.Dispose();

Don't work with c# enough to deal with the \ but this will work

This question has already been answered. Start a new discussion instead.