Из C++ в C#. Замена числа в файле
Формулировка задачи:
Уважаемые, есть ли возможность портировать этот код из С++ на C#?
Код предназначен заменять в .dll файле число 1134 на введенное.
Заранее спасибо за советы.
Сам файл для наглядности http://rgho.st/6Vz9kxlRf .
Листинг программы
- #include <fstream>
- #include <iostream>
- #include <regex>
- #include <string>
- int main ()
- {
- std::fstream fs("C:\client.dll", std::ios::in | std::ios::out | std::ios::binary);
- if (!fs)
- return 1;
- std::cout << "Input your camera distance: ";
- std::string to;
- std::cin >> to;
- std::string text((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
- fs.clear();
- fs.seekp(0, std::ios::beg);
- std::regex_replace(std::ostreambuf_iterator<char>(fs), text.begin(), text.end(), std::regex("1134"), to);
- }
Решение задачи: «Из C++ в C#. Замена числа в файле»
textual
Листинг программы
- using System;
- using System.IO;
- using System.Text;
- class Program
- {
- static void Main()
- {
- const string PATH = @"C:\client.dll";
- const string find = "1134";
- Console.Write("Input your camera distance: ");
- string replaceBy = Console.ReadLine();
- Encoding encoding = Encoding.Default;
- string text;
- try { text = File.ReadAllText(PATH, encoding); }
- catch (FileNotFoundException) { return; }
- text = text.Replace(find, replaceBy);
- using (var fstream = new FileStream(PATH, FileMode.Open, FileAccess.Write))
- {
- byte[] buf = encoding.GetBytes(text);
- fstream.Write(buf, 0, buf.Length);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д