Hi there!
I am new to daniweb.com and C++ CLI. I am trying to make a program that will tell the user the text for the ListItem number they type in. The ListItem information is stored in a text file with my custom file formatting, which is:

<~ListItem 1> text.... <~EndOf 1>
<~ListItem 2> text.... <~EndOf 2>
etc.

I have been thinking of a strategy for days, but I can't find out anything.
Thank you very much in advanced!

Dani AI

Generated

Short, practical summary for future readers (and ): because your items can span multiple lines, reading the whole file as one string and replacing the substring between a start tag and the matching end tag is the simplest, most robust approach. 's line-by-line idea works for single-line items, but it gets awkward once the body contains newlines.

A straightforward C++/CLI implementation (reads file, finds the tags for item number no, replaces the inner text, writes atomically via a temp file):

static void ReplaceListItem(System::String^ path, int no, System::String^ newText)
{
    System::String^ content = System::IO::File::ReadAllText(path);
    System::String^ startTag = System::String::Format("<~ListItem {0}>", no);
    System::String^ endTag   = System::String::Format("<~EndOf {0}>", no);

    int s = content->IndexOf(startTag, System::StringComparison::Ordinal);
    if (s == -1) return;            // start not found

    s += startTag->Length;
    int e = content->IndexOf(endTag, s, System::StringComparison::Ordinal);
    if (e == -1) return;            // end not found / malformed

    System::String^ result = content->Substring(0, s) + newText + content->Substring(e);
    System::String^ tmp = System::String::Concat(path, ".tmp");
    System::IO::File::WriteAllText(tmp, result);
    System::IO::File::Delete(path);
    System::IO::File::Move(tmp, path);
}

Notes and gotchas:

  • If the replacement text may contain $ or \, avoid regex replacement unless you escape or use a MatchEvaluator. The IndexOf/Substring method above treats newText literally.
  • Back up the file (or use the temp-file pattern above) so a crash won't corrupt data.
  • For very large files, stream copy: copy up to start tag into a temp file, write new text, skip the old body until end tag, then copy the remainder. That avoids loading the entire file into memory.
  • Validate tags exist and handle whitespace variations (use StringComparison or Regex with Singleline if you need flexible matching).

This approach keeps the logic simple, handles multi-line bodies, and is easy to test.

Recommended Answers

All 3 Replies

Well you can read in each line from the file and see if you find a match

Thanks for the reply, but I am not sure how to check if the line ends with <~EndOf (No)>
The psuedo code for what I want to achieve:
File find <~ListItem + " " + no + > + string + <~EndOf + no + >
And replace it with <~ListItem + " " + no + > + newstring + <~EndOf + no + >
Thanks.

Never mind I figured out the solution myself.

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.