Good day, dear experts. Without further ado I will describe what is needed. But I need to edit the hosts (drivers / etc) file with C ++ through the console program. That is, I write the specified text, which it will replace (before that I backup the file), run the file, and it changes the text. I am interested in any methods, but preferably simpler ones (first-year student, just started to learn programming. But I want to step forward a bit, as the speed of work on pairs does not satisfy at all). I hope for your help. I looked in Google, I found several commands, but I didn’t quite understand how to use them. If possible, give a small example.

UPD: Additional question. Android (as I read on the wiki) also has this file. Tell me, does it affect all applications or just the browser?

  • Well, the students went, some kind of mess in my head. They cannot even formulate a question - just ad. Especially touches:> Tell me, does it affect all applications or only the browser? - Barmaley

1 answer 1

fopen fread ... fwrite fflush fclose? but there is another question - now antiviruses have begun to fashionably deal with making changes to this file. block write, restore default settings, etc. those. This fact should be taken into account when implementing software.

upd . simple example:

#include "stdafx.h" #include <stdio.h> #include <Windows.h> int main(int argc, _TCHAR* argv[]) { FILE *fh; char *path; char *data = "\t127.0.0.1 localhost\n\t127.0.0.1 test.com \n"; //то что пишем в файл hosts path = (char *)LocalAlloc(LPTR, MAX_PATH + 1); // выделяем память из локальной кучи, под строку содержащую путь с именем файла GetWindowsDirectoryA(path, MAX_PATH); // c:\Windows lstrcatA(path, "\\System32\\drivers\\etc\\hosts"); // добавляем путь с именем файла. в результате в строке: c:\Windows\System32\drivers\etc\hosts fh = fopen(path, "a+"); //открываем файл на чтение и добавление данных if(fh == NULL ){ printf( "The file %s was not opened\n", path ); LocalFree(path); return 0; } fwrite(data, sizeof(char), lstrlenA(data), fh); // записываем данные в файл. /* 1й параметр - указатель на записываемые данные 2й параметр - размер элемента в байтах 3й параметр - максимальное число записываемых элементов 4й параметр - указатель на структуру типа FILE */ fclose(fh); //закрываем файл LocalFree(path); //освобождаем ранее выделенную память. return 0; } 
  • so you need a team? hold on from the command line: echo 127.0.0.1 localhost >>% SYSTEMROOT% \ System32 \ drivers \ etc \ hosts - vv2cc
  • No, I'm sorry, I put it wrong. fopen fread is it a function? In general, I wanted to know how to use them? Well, or how to use the command line using C ++? - Yegor Eremin
  • I found that using C ++ you can use console commands. But for some reason it does not work for me. Here is the code: int main () {return system ("echo 127.0.0.1 localhost >>% SYSTEMROOT% \ System32 \ drivers \ etc \ hosts"); } - Yegor Eremin
  • I tried, at first it did not work out, I also connected <tchar.h>. It compiles normally, but when you start it produces the following: piccy.info/view3/3462136/47d802fb15a78f0aab9e4468693a7305 - Egor Eremin
  • one
    @ Vendetta8247 you have definitely not included any antivirus? Error due to the fact that the file did not open, but there were no checks. mk You said you solved the problem with access to the hosts file. if there is something like a Kaspersky, it blocks access to the file for writing. - vv2cc