fopen is unable to open windows hosts file

Multi tool use
Multi tool use












0















I am having a FILE* target here which should open the windows hosts file and write to it:



FILE* target;
target = fopen("C:\windows\sysnative\drivers\etc\hosts", "r+");
if (target != NULL) {
printf("truen");
} else {
printf("falsen");
}


However, upon opening the windows hosts file, it fails to open it. Specifically, fopen() returns NULL and false is printed to the screen. I checked the directory. It is good. Removing the extra s, I was able to open it with Notepad. However fopen() cannot open that file. It is able to open any file in the current working directory, or in a nested directory inside it, but it can't open the hosts file. Perhaps I have an issue with my path? Am I missing something?










share|improve this question




















  • 4





    Are you sure about the open-mode "r+"? That means you want to open for both reading and writing. And it's not unlikely that you can't open for writing.

    – Some programmer dude
    Dec 30 '18 at 20:32











  • @Someprogrammerdude If I open it for both reading and writing, then that implies I can also write to it?

    – Galaxy
    Dec 30 '18 at 20:33






  • 2





    it's a system file, you need admin rights to write to it

    – Jean-François Fabre
    Dec 30 '18 at 20:36











  • @Jean-FrançoisFabre And how would you suggest I fix it? Should I compile the source code with gcc as admin, or run the executable itself as admin? I have tried running the executable as admin, but it still does not solve the issue.

    – Galaxy
    Dec 30 '18 at 20:39











  • first try with r mode to see if you can read it. Then try to write to it, using elevated privileges when running the exe

    – Jean-François Fabre
    Dec 30 '18 at 20:40
















0















I am having a FILE* target here which should open the windows hosts file and write to it:



FILE* target;
target = fopen("C:\windows\sysnative\drivers\etc\hosts", "r+");
if (target != NULL) {
printf("truen");
} else {
printf("falsen");
}


However, upon opening the windows hosts file, it fails to open it. Specifically, fopen() returns NULL and false is printed to the screen. I checked the directory. It is good. Removing the extra s, I was able to open it with Notepad. However fopen() cannot open that file. It is able to open any file in the current working directory, or in a nested directory inside it, but it can't open the hosts file. Perhaps I have an issue with my path? Am I missing something?










share|improve this question




















  • 4





    Are you sure about the open-mode "r+"? That means you want to open for both reading and writing. And it's not unlikely that you can't open for writing.

    – Some programmer dude
    Dec 30 '18 at 20:32











  • @Someprogrammerdude If I open it for both reading and writing, then that implies I can also write to it?

    – Galaxy
    Dec 30 '18 at 20:33






  • 2





    it's a system file, you need admin rights to write to it

    – Jean-François Fabre
    Dec 30 '18 at 20:36











  • @Jean-FrançoisFabre And how would you suggest I fix it? Should I compile the source code with gcc as admin, or run the executable itself as admin? I have tried running the executable as admin, but it still does not solve the issue.

    – Galaxy
    Dec 30 '18 at 20:39











  • first try with r mode to see if you can read it. Then try to write to it, using elevated privileges when running the exe

    – Jean-François Fabre
    Dec 30 '18 at 20:40














0












0








0








I am having a FILE* target here which should open the windows hosts file and write to it:



FILE* target;
target = fopen("C:\windows\sysnative\drivers\etc\hosts", "r+");
if (target != NULL) {
printf("truen");
} else {
printf("falsen");
}


However, upon opening the windows hosts file, it fails to open it. Specifically, fopen() returns NULL and false is printed to the screen. I checked the directory. It is good. Removing the extra s, I was able to open it with Notepad. However fopen() cannot open that file. It is able to open any file in the current working directory, or in a nested directory inside it, but it can't open the hosts file. Perhaps I have an issue with my path? Am I missing something?










share|improve this question
















I am having a FILE* target here which should open the windows hosts file and write to it:



FILE* target;
target = fopen("C:\windows\sysnative\drivers\etc\hosts", "r+");
if (target != NULL) {
printf("truen");
} else {
printf("falsen");
}


However, upon opening the windows hosts file, it fails to open it. Specifically, fopen() returns NULL and false is printed to the screen. I checked the directory. It is good. Removing the extra s, I was able to open it with Notepad. However fopen() cannot open that file. It is able to open any file in the current working directory, or in a nested directory inside it, but it can't open the hosts file. Perhaps I have an issue with my path? Am I missing something?







c windows network-programming fopen






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 20:40







Galaxy

















asked Dec 30 '18 at 20:30









GalaxyGalaxy

608419




608419








  • 4





    Are you sure about the open-mode "r+"? That means you want to open for both reading and writing. And it's not unlikely that you can't open for writing.

    – Some programmer dude
    Dec 30 '18 at 20:32











  • @Someprogrammerdude If I open it for both reading and writing, then that implies I can also write to it?

    – Galaxy
    Dec 30 '18 at 20:33






  • 2





    it's a system file, you need admin rights to write to it

    – Jean-François Fabre
    Dec 30 '18 at 20:36











  • @Jean-FrançoisFabre And how would you suggest I fix it? Should I compile the source code with gcc as admin, or run the executable itself as admin? I have tried running the executable as admin, but it still does not solve the issue.

    – Galaxy
    Dec 30 '18 at 20:39











  • first try with r mode to see if you can read it. Then try to write to it, using elevated privileges when running the exe

    – Jean-François Fabre
    Dec 30 '18 at 20:40














  • 4





    Are you sure about the open-mode "r+"? That means you want to open for both reading and writing. And it's not unlikely that you can't open for writing.

    – Some programmer dude
    Dec 30 '18 at 20:32











  • @Someprogrammerdude If I open it for both reading and writing, then that implies I can also write to it?

    – Galaxy
    Dec 30 '18 at 20:33






  • 2





    it's a system file, you need admin rights to write to it

    – Jean-François Fabre
    Dec 30 '18 at 20:36











  • @Jean-FrançoisFabre And how would you suggest I fix it? Should I compile the source code with gcc as admin, or run the executable itself as admin? I have tried running the executable as admin, but it still does not solve the issue.

    – Galaxy
    Dec 30 '18 at 20:39











  • first try with r mode to see if you can read it. Then try to write to it, using elevated privileges when running the exe

    – Jean-François Fabre
    Dec 30 '18 at 20:40








4




4





Are you sure about the open-mode "r+"? That means you want to open for both reading and writing. And it's not unlikely that you can't open for writing.

– Some programmer dude
Dec 30 '18 at 20:32





Are you sure about the open-mode "r+"? That means you want to open for both reading and writing. And it's not unlikely that you can't open for writing.

– Some programmer dude
Dec 30 '18 at 20:32













@Someprogrammerdude If I open it for both reading and writing, then that implies I can also write to it?

– Galaxy
Dec 30 '18 at 20:33





@Someprogrammerdude If I open it for both reading and writing, then that implies I can also write to it?

– Galaxy
Dec 30 '18 at 20:33




2




2





it's a system file, you need admin rights to write to it

– Jean-François Fabre
Dec 30 '18 at 20:36





it's a system file, you need admin rights to write to it

– Jean-François Fabre
Dec 30 '18 at 20:36













@Jean-FrançoisFabre And how would you suggest I fix it? Should I compile the source code with gcc as admin, or run the executable itself as admin? I have tried running the executable as admin, but it still does not solve the issue.

– Galaxy
Dec 30 '18 at 20:39





@Jean-FrançoisFabre And how would you suggest I fix it? Should I compile the source code with gcc as admin, or run the executable itself as admin? I have tried running the executable as admin, but it still does not solve the issue.

– Galaxy
Dec 30 '18 at 20:39













first try with r mode to see if you can read it. Then try to write to it, using elevated privileges when running the exe

– Jean-François Fabre
Dec 30 '18 at 20:40





first try with r mode to see if you can read it. Then try to write to it, using elevated privileges when running the exe

– Jean-François Fabre
Dec 30 '18 at 20:40












2 Answers
2






active

oldest

votes


















1














you need admin priv to open hosts file on windows, try running your script as admin.






share|improve this answer
























  • So apparently running the Windows Command Prompt as Administrator, then compiling the source code, and running the executable from within it, worked! fopen() was able to successfully write to the file.

    – Galaxy
    Dec 30 '18 at 21:55











  • Specifically, the hosts file grants standard users generic-read access, so "r" mode would work fine, but "r+" mode requires being an administrator.

    – eryksun
    Dec 31 '18 at 1:34



















0














Whenever a file operation fails on Windows, you can call GetLastError() (errno` on Posix systems) to find out why the operation fails. This will return an error code you can look up to find out why it failed






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981187%2ffopen-is-unable-to-open-windows-hosts-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    you need admin priv to open hosts file on windows, try running your script as admin.






    share|improve this answer
























    • So apparently running the Windows Command Prompt as Administrator, then compiling the source code, and running the executable from within it, worked! fopen() was able to successfully write to the file.

      – Galaxy
      Dec 30 '18 at 21:55











    • Specifically, the hosts file grants standard users generic-read access, so "r" mode would work fine, but "r+" mode requires being an administrator.

      – eryksun
      Dec 31 '18 at 1:34
















    1














    you need admin priv to open hosts file on windows, try running your script as admin.






    share|improve this answer
























    • So apparently running the Windows Command Prompt as Administrator, then compiling the source code, and running the executable from within it, worked! fopen() was able to successfully write to the file.

      – Galaxy
      Dec 30 '18 at 21:55











    • Specifically, the hosts file grants standard users generic-read access, so "r" mode would work fine, but "r+" mode requires being an administrator.

      – eryksun
      Dec 31 '18 at 1:34














    1












    1








    1







    you need admin priv to open hosts file on windows, try running your script as admin.






    share|improve this answer













    you need admin priv to open hosts file on windows, try running your script as admin.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 30 '18 at 20:55









    ablil98ablil98

    435




    435













    • So apparently running the Windows Command Prompt as Administrator, then compiling the source code, and running the executable from within it, worked! fopen() was able to successfully write to the file.

      – Galaxy
      Dec 30 '18 at 21:55











    • Specifically, the hosts file grants standard users generic-read access, so "r" mode would work fine, but "r+" mode requires being an administrator.

      – eryksun
      Dec 31 '18 at 1:34



















    • So apparently running the Windows Command Prompt as Administrator, then compiling the source code, and running the executable from within it, worked! fopen() was able to successfully write to the file.

      – Galaxy
      Dec 30 '18 at 21:55











    • Specifically, the hosts file grants standard users generic-read access, so "r" mode would work fine, but "r+" mode requires being an administrator.

      – eryksun
      Dec 31 '18 at 1:34

















    So apparently running the Windows Command Prompt as Administrator, then compiling the source code, and running the executable from within it, worked! fopen() was able to successfully write to the file.

    – Galaxy
    Dec 30 '18 at 21:55





    So apparently running the Windows Command Prompt as Administrator, then compiling the source code, and running the executable from within it, worked! fopen() was able to successfully write to the file.

    – Galaxy
    Dec 30 '18 at 21:55













    Specifically, the hosts file grants standard users generic-read access, so "r" mode would work fine, but "r+" mode requires being an administrator.

    – eryksun
    Dec 31 '18 at 1:34





    Specifically, the hosts file grants standard users generic-read access, so "r" mode would work fine, but "r+" mode requires being an administrator.

    – eryksun
    Dec 31 '18 at 1:34













    0














    Whenever a file operation fails on Windows, you can call GetLastError() (errno` on Posix systems) to find out why the operation fails. This will return an error code you can look up to find out why it failed






    share|improve this answer




























      0














      Whenever a file operation fails on Windows, you can call GetLastError() (errno` on Posix systems) to find out why the operation fails. This will return an error code you can look up to find out why it failed






      share|improve this answer


























        0












        0








        0







        Whenever a file operation fails on Windows, you can call GetLastError() (errno` on Posix systems) to find out why the operation fails. This will return an error code you can look up to find out why it failed






        share|improve this answer













        Whenever a file operation fails on Windows, you can call GetLastError() (errno` on Posix systems) to find out why the operation fails. This will return an error code you can look up to find out why it failed







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 21:09









        dorondoron

        18.6k64882




        18.6k64882






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981187%2ffopen-is-unable-to-open-windows-hosts-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            j6,FKu7ruVC n,EE wYiDkI8Hbijq,dBsX9qhr7GO oIZ
            D7,89twPded,GTBIjZxB23qIGDc0cnSvOL8ZhWfhV4OQjVVie 1q8iARSQrA

            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas