Ok so i want my program to read a text file to a certain point. Lets say to the line line that says fn: i then want it to read the one line only and only the the text after fn: and i want it to shoew that text in a text box. i no it can be done. but i dont know how. if any body knows would you min sharing

-T

Dani AI

Generated

asked for only the text after the marker fn:. 's idea (scan lines until you find the marker) and 's suggestion (remove the prefix) are exactly the right directions. Below is a compact, robust Python way to do that and a few gotchas to watch for years later.

import re

pattern = re.compile(r'^\s*fn\s*:\s*(.*)', re.IGNORECASE)

value = None
with open('path/to/file.txt', encoding='utf-8', errors='replace') as fh:
    for line in fh:
        m = pattern.match(line)
        if m:
            value = m.group(1).rstrip()
            break

# use `value` in your UI (replace print with textbox insert)
if value is not None:
    print(value)

Notes and troubleshooting:

  • The regex tolerates leading whitespace, spaces around the colon, and different letter case (so Fn : something still works).
  • If the file uses a different encoding, set the correct encoding= or handle errors; errors='replace' prevents crashes on bad bytes.
  • If the marker is on its own line and the wanted text is on the next line, read the next line with next(fh, '') after matching the marker.
  • For very large files, iterating line-by-line (as above) is memory efficient. If you need the last occurrence of fn:, keep assigning value and finish the loop instead of breaking.
  • Add a try/except around file open to handle missing files or permissions.

This ties the original C# suggestions to a Python-ready solution while covering common edge cases you might hit when revisiting the thread later.

Recommended Answers

All 5 Replies

StreamReader sr = new StreamReader("yourFileNameGoesHere", System.Text.Encoding.Default);
			string s = null;

			while ((s = sr.ReadLine()) != null)
			{
				if (s.IndexOf("fn:") != -1)
				{
					// ok, you found it
					// now you can do like 
					// textBox1.text = s;
					break;
				}
			}

			sr.Close();

Thank you so much, man you have helped me learn C# so much. I really thank you for all of your help.


-T

Thank you so much, man you have helped me learn C# so much. I really thank you for all of your help.


-T

hmmm but how can i make it so it only shos what comes after fn: not inl;uding fn:?

Grretings:
Just remove that part from the string:

s.Remove(0,3);

the remove method takes two parameter, the index where you want to start removing and length you wish to remove

Hi

Complete code to reading last line in text file

StreamReader streamReader = new StreamReader("HereWillBeYourTextFilePath");
    ArrayList lines = new ArrayList();

    string line;

    while ((line = streamReader.ReadLine()) != null)
    {
        lines.Add(line);
    }
    streamReader.Close();

    if (lines.Count > 0)
    {
        Response.Write(lines[lines.Count - 1].ToString());
    }

===============

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.