Hi there everyone.
I was hoping you guys could help me out again.
I would like to write a setting to a certain line in a textdocument, for example "4000" at line 5 of the document.
So far i have this:

void MainSettings::setSettingsToFile(String *numChannels, String *savePath, String *playlistPath)
// Here the settings will be written to the settings file
{
	// Declare variables
	strMixChannels = numChannels;
	strSettingsSavePath = ".\Settings.ini;
	strPlaylistPath = playlistPath;
	strSavePath = savePath;
	pStream = new FileStream(strSettingsSavePath, FileMode::Create);
	pWriter = new StreamWriter(pStream);

	// Write the number of mixchannels to the settings file
	pWriter->WriteLine(strMixChannels);
	pWriter->WriteLine(strSavePath);
	pWriter->WriteLine(strPlaylistPath);
	pWriter->Flush();
	pWriter->Close();
	pStream->Close();
}

But as you can see all atttributes now are being set in one method, and i can say what goes at what line.
I want to make different methods for each attribute, however i will need to know how to give it a specific location in the document for that.
So please, if you know how to do this, all help is appreciated.

Dani AI

Generated

As wanted a value placed at a specific text-file line, and as and noted, this is running on the .NET side (managed C++), so treat the file as variable-length text rather than something that can be safely "seeked" by line. A small, reusable helper that reads all lines, adjusts the desired index, and writes the file back is the most reliable approach; it lets each per-attribute setter simply call that helper and avoids accidental loss of other settings.

Recommended pattern (summary)

  • Read the entire file into a list/array.
  • If the requested line number is beyond EOF, append empty lines until the list is long enough.
  • Replace the element at index (lineNumber - 1) with the new value.
  • Write out to a temporary file and atomically replace the original (temp->move) to avoid data loss if the process crashes.

A concise C++/CLI example:

void SetLine(String^ path, int lineNo, String^ value)
{
    System::Collections::Generic::List<String^>^ lines =
        gcnew System::Collections::Generic::List<String^>();
    System::IO::StreamReader^ r = gcnew System::IO::StreamReader(path);
    while (!r->EndOfStream) lines->Add(r->ReadLine());
    r->Close();

    while (lines->Count < lineNo) lines->Add(String::Empty);
    lines[lineNo - 1] = value;

    String^ tmp = path + ".tmp";
    System::IO::StreamWriter^ w = gcnew System::IO::StreamWriter(tmp);
    for each (String^ s in lines) w->WriteLine(s);
    w->Close();

    System::IO::File::Delete(path);
    System::IO::File::Move(tmp, path);
}

Notes and cautions

  • Validate the path and line number; handle IO exceptions.
  • Preserve encoding and newline style if that matters (use StreamReader/Writer overloads).
  • For concurrent access, introduce file locking or a retry strategy.
  • Line-numbered settings are brittle — consider a key/value format (INI, XML, JSON, registry, or app config) for long-term maintainability. This helper makes per-attribute setters simple and safe compared with repeatedly opening the file with a create/overwrite mode.

Recommended Answers

All 2 Replies

Is this C#? (certainly not C or C++). You sure this is in the right forum?

>> (certainly not C or C++).
It looks like Managed C++. But I agree that this question would be better asked in a .NET forum.

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.