system call to write and read from a file












-1















I want to test the system call of read and write



#include <unistd.h>
#include <fcntl.h>

int main(void)
{
fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
write(fd, "Test the first line",20);
}


The cc reports:



In [29]: !cc write_test.c                                                                                         
write_test.c:6:5: error: use of undeclared identifier 'fd'
fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
^
write_test.c:7:11: error: use of undeclared identifier 'fd'
write(fd, "Test the first line",20)
^
2 errors generated.


I have some python basics, but have no idea how to get the code done.










share|improve this question




















  • 3





    fd is not declared as said so it produces 2 errors since it has 2 usages

    – TerribleDog
    Nov 13 '18 at 8:55








  • 3





    When attempting to use a new language for the first time, getting to know it first is usually good (instead of just guessing and hoping for the best). Just like when learning a new spoken and written language. So please get a couple of beginners book, read some tutorials, or take a class or two, before continuing.

    – Some programmer dude
    Nov 13 '18 at 8:57













  • yes, I am taking APUE. @Someprogrammerdude

    – JawSaw
    Nov 13 '18 at 8:58






  • 1





    APUE is a book about programming in a Unix-like environment, but it's not a language book. It requires you to already know C.

    – Some programmer dude
    Nov 13 '18 at 8:59











  • Also, if you're reading APUE then it should have taught you to check for errors, and clean up your resources (IIRC, long time since I read it). You should check that open and write succeed. And you should close the file when you're done with it.

    – Some programmer dude
    Nov 13 '18 at 9:11
















-1















I want to test the system call of read and write



#include <unistd.h>
#include <fcntl.h>

int main(void)
{
fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
write(fd, "Test the first line",20);
}


The cc reports:



In [29]: !cc write_test.c                                                                                         
write_test.c:6:5: error: use of undeclared identifier 'fd'
fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
^
write_test.c:7:11: error: use of undeclared identifier 'fd'
write(fd, "Test the first line",20)
^
2 errors generated.


I have some python basics, but have no idea how to get the code done.










share|improve this question




















  • 3





    fd is not declared as said so it produces 2 errors since it has 2 usages

    – TerribleDog
    Nov 13 '18 at 8:55








  • 3





    When attempting to use a new language for the first time, getting to know it first is usually good (instead of just guessing and hoping for the best). Just like when learning a new spoken and written language. So please get a couple of beginners book, read some tutorials, or take a class or two, before continuing.

    – Some programmer dude
    Nov 13 '18 at 8:57













  • yes, I am taking APUE. @Someprogrammerdude

    – JawSaw
    Nov 13 '18 at 8:58






  • 1





    APUE is a book about programming in a Unix-like environment, but it's not a language book. It requires you to already know C.

    – Some programmer dude
    Nov 13 '18 at 8:59











  • Also, if you're reading APUE then it should have taught you to check for errors, and clean up your resources (IIRC, long time since I read it). You should check that open and write succeed. And you should close the file when you're done with it.

    – Some programmer dude
    Nov 13 '18 at 9:11














-1












-1








-1








I want to test the system call of read and write



#include <unistd.h>
#include <fcntl.h>

int main(void)
{
fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
write(fd, "Test the first line",20);
}


The cc reports:



In [29]: !cc write_test.c                                                                                         
write_test.c:6:5: error: use of undeclared identifier 'fd'
fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
^
write_test.c:7:11: error: use of undeclared identifier 'fd'
write(fd, "Test the first line",20)
^
2 errors generated.


I have some python basics, but have no idea how to get the code done.










share|improve this question
















I want to test the system call of read and write



#include <unistd.h>
#include <fcntl.h>

int main(void)
{
fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
write(fd, "Test the first line",20);
}


The cc reports:



In [29]: !cc write_test.c                                                                                         
write_test.c:6:5: error: use of undeclared identifier 'fd'
fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
^
write_test.c:7:11: error: use of undeclared identifier 'fd'
write(fd, "Test the first line",20)
^
2 errors generated.


I have some python basics, but have no idea how to get the code done.







c undeclared-identifier






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:03









gsamaras

51.2k24100186




51.2k24100186










asked Nov 13 '18 at 8:53









JawSawJawSaw

4,33811735




4,33811735








  • 3





    fd is not declared as said so it produces 2 errors since it has 2 usages

    – TerribleDog
    Nov 13 '18 at 8:55








  • 3





    When attempting to use a new language for the first time, getting to know it first is usually good (instead of just guessing and hoping for the best). Just like when learning a new spoken and written language. So please get a couple of beginners book, read some tutorials, or take a class or two, before continuing.

    – Some programmer dude
    Nov 13 '18 at 8:57













  • yes, I am taking APUE. @Someprogrammerdude

    – JawSaw
    Nov 13 '18 at 8:58






  • 1





    APUE is a book about programming in a Unix-like environment, but it's not a language book. It requires you to already know C.

    – Some programmer dude
    Nov 13 '18 at 8:59











  • Also, if you're reading APUE then it should have taught you to check for errors, and clean up your resources (IIRC, long time since I read it). You should check that open and write succeed. And you should close the file when you're done with it.

    – Some programmer dude
    Nov 13 '18 at 9:11














  • 3





    fd is not declared as said so it produces 2 errors since it has 2 usages

    – TerribleDog
    Nov 13 '18 at 8:55








  • 3





    When attempting to use a new language for the first time, getting to know it first is usually good (instead of just guessing and hoping for the best). Just like when learning a new spoken and written language. So please get a couple of beginners book, read some tutorials, or take a class or two, before continuing.

    – Some programmer dude
    Nov 13 '18 at 8:57













  • yes, I am taking APUE. @Someprogrammerdude

    – JawSaw
    Nov 13 '18 at 8:58






  • 1





    APUE is a book about programming in a Unix-like environment, but it's not a language book. It requires you to already know C.

    – Some programmer dude
    Nov 13 '18 at 8:59











  • Also, if you're reading APUE then it should have taught you to check for errors, and clean up your resources (IIRC, long time since I read it). You should check that open and write succeed. And you should close the file when you're done with it.

    – Some programmer dude
    Nov 13 '18 at 9:11








3




3





fd is not declared as said so it produces 2 errors since it has 2 usages

– TerribleDog
Nov 13 '18 at 8:55







fd is not declared as said so it produces 2 errors since it has 2 usages

– TerribleDog
Nov 13 '18 at 8:55






3




3





When attempting to use a new language for the first time, getting to know it first is usually good (instead of just guessing and hoping for the best). Just like when learning a new spoken and written language. So please get a couple of beginners book, read some tutorials, or take a class or two, before continuing.

– Some programmer dude
Nov 13 '18 at 8:57







When attempting to use a new language for the first time, getting to know it first is usually good (instead of just guessing and hoping for the best). Just like when learning a new spoken and written language. So please get a couple of beginners book, read some tutorials, or take a class or two, before continuing.

– Some programmer dude
Nov 13 '18 at 8:57















yes, I am taking APUE. @Someprogrammerdude

– JawSaw
Nov 13 '18 at 8:58





yes, I am taking APUE. @Someprogrammerdude

– JawSaw
Nov 13 '18 at 8:58




1




1





APUE is a book about programming in a Unix-like environment, but it's not a language book. It requires you to already know C.

– Some programmer dude
Nov 13 '18 at 8:59





APUE is a book about programming in a Unix-like environment, but it's not a language book. It requires you to already know C.

– Some programmer dude
Nov 13 '18 at 8:59













Also, if you're reading APUE then it should have taught you to check for errors, and clean up your resources (IIRC, long time since I read it). You should check that open and write succeed. And you should close the file when you're done with it.

– Some programmer dude
Nov 13 '18 at 9:11





Also, if you're reading APUE then it should have taught you to check for errors, and clean up your resources (IIRC, long time since I read it). You should check that open and write succeed. And you should close the file when you're done with it.

– Some programmer dude
Nov 13 '18 at 9:11












1 Answer
1






active

oldest

votes


















3














You need to declare what type fd is. What type is that? Check the reference of open(), which mentions:




int open(const char *path, int oflag, ... );




You can see that the return type is int. As a result, the variable that should be assigned the return value of that function should also be of the same type.



So change:



fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);


to this:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);




PS: If the file doesn't exist, then you will need to do:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR | O_CREAT, 0600);


Read more in Using open() to create a file in C.






share|improve this answer





















  • 2





    @avirate Have you checked if the file could be opened? open() won't create a new file when you don't pass O_CREAT.

    – Swordfish
    Nov 13 '18 at 9:07








  • 1





    @avirate if the file doesn't exist in the first place, you need to use the O_CREAT as well, and a creation mode as the 3rd parameter, as @Ctx mentioned. Then if you sudo cat <yourFile> it will work. But maybe then you would like to append, but that's up to you. Read more in Using open to create files. However, I strongly suggest you to take a step back and learn some basic C first, before diving into APUE. Edit: Swordfish is right and faster than me! :)

    – gsamaras
    Nov 13 '18 at 9:11








  • 1





    If you use O_CREAT, you additionally should provide a creation mode as third parameter, i.e. 0600

    – Ctx
    Nov 13 '18 at 9:12






  • 1





    thank you all for the kindness and patience, I feel and cannot say anything.

    – JawSaw
    Nov 13 '18 at 9:14






  • 1





    Why the downvotes?

    – Ctx
    Nov 13 '18 at 9:16











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%2f53277128%2fsystem-call-to-write-and-read-from-a-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You need to declare what type fd is. What type is that? Check the reference of open(), which mentions:




int open(const char *path, int oflag, ... );




You can see that the return type is int. As a result, the variable that should be assigned the return value of that function should also be of the same type.



So change:



fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);


to this:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);




PS: If the file doesn't exist, then you will need to do:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR | O_CREAT, 0600);


Read more in Using open() to create a file in C.






share|improve this answer





















  • 2





    @avirate Have you checked if the file could be opened? open() won't create a new file when you don't pass O_CREAT.

    – Swordfish
    Nov 13 '18 at 9:07








  • 1





    @avirate if the file doesn't exist in the first place, you need to use the O_CREAT as well, and a creation mode as the 3rd parameter, as @Ctx mentioned. Then if you sudo cat <yourFile> it will work. But maybe then you would like to append, but that's up to you. Read more in Using open to create files. However, I strongly suggest you to take a step back and learn some basic C first, before diving into APUE. Edit: Swordfish is right and faster than me! :)

    – gsamaras
    Nov 13 '18 at 9:11








  • 1





    If you use O_CREAT, you additionally should provide a creation mode as third parameter, i.e. 0600

    – Ctx
    Nov 13 '18 at 9:12






  • 1





    thank you all for the kindness and patience, I feel and cannot say anything.

    – JawSaw
    Nov 13 '18 at 9:14






  • 1





    Why the downvotes?

    – Ctx
    Nov 13 '18 at 9:16
















3














You need to declare what type fd is. What type is that? Check the reference of open(), which mentions:




int open(const char *path, int oflag, ... );




You can see that the return type is int. As a result, the variable that should be assigned the return value of that function should also be of the same type.



So change:



fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);


to this:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);




PS: If the file doesn't exist, then you will need to do:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR | O_CREAT, 0600);


Read more in Using open() to create a file in C.






share|improve this answer





















  • 2





    @avirate Have you checked if the file could be opened? open() won't create a new file when you don't pass O_CREAT.

    – Swordfish
    Nov 13 '18 at 9:07








  • 1





    @avirate if the file doesn't exist in the first place, you need to use the O_CREAT as well, and a creation mode as the 3rd parameter, as @Ctx mentioned. Then if you sudo cat <yourFile> it will work. But maybe then you would like to append, but that's up to you. Read more in Using open to create files. However, I strongly suggest you to take a step back and learn some basic C first, before diving into APUE. Edit: Swordfish is right and faster than me! :)

    – gsamaras
    Nov 13 '18 at 9:11








  • 1





    If you use O_CREAT, you additionally should provide a creation mode as third parameter, i.e. 0600

    – Ctx
    Nov 13 '18 at 9:12






  • 1





    thank you all for the kindness and patience, I feel and cannot say anything.

    – JawSaw
    Nov 13 '18 at 9:14






  • 1





    Why the downvotes?

    – Ctx
    Nov 13 '18 at 9:16














3












3








3







You need to declare what type fd is. What type is that? Check the reference of open(), which mentions:




int open(const char *path, int oflag, ... );




You can see that the return type is int. As a result, the variable that should be assigned the return value of that function should also be of the same type.



So change:



fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);


to this:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);




PS: If the file doesn't exist, then you will need to do:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR | O_CREAT, 0600);


Read more in Using open() to create a file in C.






share|improve this answer















You need to declare what type fd is. What type is that? Check the reference of open(), which mentions:




int open(const char *path, int oflag, ... );




You can see that the return type is int. As a result, the variable that should be assigned the return value of that function should also be of the same type.



So change:



fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);


to this:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);




PS: If the file doesn't exist, then you will need to do:



int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR | O_CREAT, 0600);


Read more in Using open() to create a file in C.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 9:15

























answered Nov 13 '18 at 8:57









gsamarasgsamaras

51.2k24100186




51.2k24100186








  • 2





    @avirate Have you checked if the file could be opened? open() won't create a new file when you don't pass O_CREAT.

    – Swordfish
    Nov 13 '18 at 9:07








  • 1





    @avirate if the file doesn't exist in the first place, you need to use the O_CREAT as well, and a creation mode as the 3rd parameter, as @Ctx mentioned. Then if you sudo cat <yourFile> it will work. But maybe then you would like to append, but that's up to you. Read more in Using open to create files. However, I strongly suggest you to take a step back and learn some basic C first, before diving into APUE. Edit: Swordfish is right and faster than me! :)

    – gsamaras
    Nov 13 '18 at 9:11








  • 1





    If you use O_CREAT, you additionally should provide a creation mode as third parameter, i.e. 0600

    – Ctx
    Nov 13 '18 at 9:12






  • 1





    thank you all for the kindness and patience, I feel and cannot say anything.

    – JawSaw
    Nov 13 '18 at 9:14






  • 1





    Why the downvotes?

    – Ctx
    Nov 13 '18 at 9:16














  • 2





    @avirate Have you checked if the file could be opened? open() won't create a new file when you don't pass O_CREAT.

    – Swordfish
    Nov 13 '18 at 9:07








  • 1





    @avirate if the file doesn't exist in the first place, you need to use the O_CREAT as well, and a creation mode as the 3rd parameter, as @Ctx mentioned. Then if you sudo cat <yourFile> it will work. But maybe then you would like to append, but that's up to you. Read more in Using open to create files. However, I strongly suggest you to take a step back and learn some basic C first, before diving into APUE. Edit: Swordfish is right and faster than me! :)

    – gsamaras
    Nov 13 '18 at 9:11








  • 1





    If you use O_CREAT, you additionally should provide a creation mode as third parameter, i.e. 0600

    – Ctx
    Nov 13 '18 at 9:12






  • 1





    thank you all for the kindness and patience, I feel and cannot say anything.

    – JawSaw
    Nov 13 '18 at 9:14






  • 1





    Why the downvotes?

    – Ctx
    Nov 13 '18 at 9:16








2




2





@avirate Have you checked if the file could be opened? open() won't create a new file when you don't pass O_CREAT.

– Swordfish
Nov 13 '18 at 9:07







@avirate Have you checked if the file could be opened? open() won't create a new file when you don't pass O_CREAT.

– Swordfish
Nov 13 '18 at 9:07






1




1





@avirate if the file doesn't exist in the first place, you need to use the O_CREAT as well, and a creation mode as the 3rd parameter, as @Ctx mentioned. Then if you sudo cat <yourFile> it will work. But maybe then you would like to append, but that's up to you. Read more in Using open to create files. However, I strongly suggest you to take a step back and learn some basic C first, before diving into APUE. Edit: Swordfish is right and faster than me! :)

– gsamaras
Nov 13 '18 at 9:11







@avirate if the file doesn't exist in the first place, you need to use the O_CREAT as well, and a creation mode as the 3rd parameter, as @Ctx mentioned. Then if you sudo cat <yourFile> it will work. But maybe then you would like to append, but that's up to you. Read more in Using open to create files. However, I strongly suggest you to take a step back and learn some basic C first, before diving into APUE. Edit: Swordfish is right and faster than me! :)

– gsamaras
Nov 13 '18 at 9:11






1




1





If you use O_CREAT, you additionally should provide a creation mode as third parameter, i.e. 0600

– Ctx
Nov 13 '18 at 9:12





If you use O_CREAT, you additionally should provide a creation mode as third parameter, i.e. 0600

– Ctx
Nov 13 '18 at 9:12




1




1





thank you all for the kindness and patience, I feel and cannot say anything.

– JawSaw
Nov 13 '18 at 9:14





thank you all for the kindness and patience, I feel and cannot say anything.

– JawSaw
Nov 13 '18 at 9:14




1




1





Why the downvotes?

– Ctx
Nov 13 '18 at 9:16





Why the downvotes?

– Ctx
Nov 13 '18 at 9:16


















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%2f53277128%2fsystem-call-to-write-and-read-from-a-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







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ