fopen is unable to open windows hosts file

Multi tool use
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
|
show 5 more comments
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
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 withgcc
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 withr
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
|
show 5 more comments
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
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
c windows network-programming fopen
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 withgcc
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 withr
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
|
show 5 more comments
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 withgcc
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 withr
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
|
show 5 more comments
2 Answers
2
active
oldest
votes
you need admin priv to open hosts file on windows, try running your script as admin.
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
you need admin priv to open hosts file on windows, try running your script as admin.
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
add a comment |
you need admin priv to open hosts file on windows, try running your script as admin.
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
add a comment |
you need admin priv to open hosts file on windows, try running your script as admin.
you need admin priv to open hosts file on windows, try running your script as admin.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Dec 30 '18 at 21:09
dorondoron
18.6k64882
18.6k64882
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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