Simple create - lseek - read program in C












0















I'm trying to create a simple program which creates a file, writes on it, then moves back the pointer and finally reads it.



#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

int main(){

int fd = creat("/home/alum/Class/ej",S_IRWXU);
if(fd==-1){
printf("Error %dn",errno);
return 1;
}

ssize_t size = write(fd, "Halo",4);
if(size==-1){
printf("Error %dn",errno);
return 1;
}

char string[50];
lseek(fd,0,SEEK_SET);
while((size = read(fd, string, 49)) >0){
printf("Read[%d]: %sn",size,string);
}

printf("Size: %dn",size);

if(size==-1){
printf("Error %dn",errno);
return 1;
}

int c=close(fd);
if(c==-1){
printf("Error %dn",errno);
return 1;
}

return 0;
}


My problem is that "lseek" seems not to be working. I always get size "-1" when I try to read, so I assume I'm not going back to the beginning of the file... Any suggestions?



I've seen some questions related to lseek but I haven't found a solution to my problem.



EDIT:



I changed



  int fd = creat("/home/alum/Class/ej",S_IRWXU);


to



  int fd = open("/home/alum/Class/ej",O_CREAT|O_RDONLY|O_WRONLY|O_TRUNC);


and also tried



  int fd = open("/home/alum/Class/ej",O_CREAT|O_RDONLY|O_WRONLY|O_TRUNC,0700);


As suggested but I keep getting the same error.










share|improve this question




















  • 5





    According to the manpage, "creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC", so it seems you can't read from a file opened with creat (flag O_WRONLY)

    – Karsten Koop
    Nov 27 '18 at 9:12











  • I tried using open and I get the same error (changes in the post)

    – Dgrm
    Nov 27 '18 at 9:21











  • Fixed it using O_RDWR, Thanks!

    – Dgrm
    Nov 27 '18 at 9:27






  • 1





    use perror instead of printf. It will print a human-readable error message.

    – Antti Haapala
    Nov 27 '18 at 10:11






  • 1





    Note: string is not a string when printf("Read[%d]: %sn",size,string) is called as string lacks a certain null character. (UB)

    – chux
    Nov 27 '18 at 11:08


















0















I'm trying to create a simple program which creates a file, writes on it, then moves back the pointer and finally reads it.



#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

int main(){

int fd = creat("/home/alum/Class/ej",S_IRWXU);
if(fd==-1){
printf("Error %dn",errno);
return 1;
}

ssize_t size = write(fd, "Halo",4);
if(size==-1){
printf("Error %dn",errno);
return 1;
}

char string[50];
lseek(fd,0,SEEK_SET);
while((size = read(fd, string, 49)) >0){
printf("Read[%d]: %sn",size,string);
}

printf("Size: %dn",size);

if(size==-1){
printf("Error %dn",errno);
return 1;
}

int c=close(fd);
if(c==-1){
printf("Error %dn",errno);
return 1;
}

return 0;
}


My problem is that "lseek" seems not to be working. I always get size "-1" when I try to read, so I assume I'm not going back to the beginning of the file... Any suggestions?



I've seen some questions related to lseek but I haven't found a solution to my problem.



EDIT:



I changed



  int fd = creat("/home/alum/Class/ej",S_IRWXU);


to



  int fd = open("/home/alum/Class/ej",O_CREAT|O_RDONLY|O_WRONLY|O_TRUNC);


and also tried



  int fd = open("/home/alum/Class/ej",O_CREAT|O_RDONLY|O_WRONLY|O_TRUNC,0700);


As suggested but I keep getting the same error.










share|improve this question




















  • 5





    According to the manpage, "creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC", so it seems you can't read from a file opened with creat (flag O_WRONLY)

    – Karsten Koop
    Nov 27 '18 at 9:12











  • I tried using open and I get the same error (changes in the post)

    – Dgrm
    Nov 27 '18 at 9:21











  • Fixed it using O_RDWR, Thanks!

    – Dgrm
    Nov 27 '18 at 9:27






  • 1





    use perror instead of printf. It will print a human-readable error message.

    – Antti Haapala
    Nov 27 '18 at 10:11






  • 1





    Note: string is not a string when printf("Read[%d]: %sn",size,string) is called as string lacks a certain null character. (UB)

    – chux
    Nov 27 '18 at 11:08
















0












0








0








I'm trying to create a simple program which creates a file, writes on it, then moves back the pointer and finally reads it.



#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

int main(){

int fd = creat("/home/alum/Class/ej",S_IRWXU);
if(fd==-1){
printf("Error %dn",errno);
return 1;
}

ssize_t size = write(fd, "Halo",4);
if(size==-1){
printf("Error %dn",errno);
return 1;
}

char string[50];
lseek(fd,0,SEEK_SET);
while((size = read(fd, string, 49)) >0){
printf("Read[%d]: %sn",size,string);
}

printf("Size: %dn",size);

if(size==-1){
printf("Error %dn",errno);
return 1;
}

int c=close(fd);
if(c==-1){
printf("Error %dn",errno);
return 1;
}

return 0;
}


My problem is that "lseek" seems not to be working. I always get size "-1" when I try to read, so I assume I'm not going back to the beginning of the file... Any suggestions?



I've seen some questions related to lseek but I haven't found a solution to my problem.



EDIT:



I changed



  int fd = creat("/home/alum/Class/ej",S_IRWXU);


to



  int fd = open("/home/alum/Class/ej",O_CREAT|O_RDONLY|O_WRONLY|O_TRUNC);


and also tried



  int fd = open("/home/alum/Class/ej",O_CREAT|O_RDONLY|O_WRONLY|O_TRUNC,0700);


As suggested but I keep getting the same error.










share|improve this question
















I'm trying to create a simple program which creates a file, writes on it, then moves back the pointer and finally reads it.



#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

int main(){

int fd = creat("/home/alum/Class/ej",S_IRWXU);
if(fd==-1){
printf("Error %dn",errno);
return 1;
}

ssize_t size = write(fd, "Halo",4);
if(size==-1){
printf("Error %dn",errno);
return 1;
}

char string[50];
lseek(fd,0,SEEK_SET);
while((size = read(fd, string, 49)) >0){
printf("Read[%d]: %sn",size,string);
}

printf("Size: %dn",size);

if(size==-1){
printf("Error %dn",errno);
return 1;
}

int c=close(fd);
if(c==-1){
printf("Error %dn",errno);
return 1;
}

return 0;
}


My problem is that "lseek" seems not to be working. I always get size "-1" when I try to read, so I assume I'm not going back to the beginning of the file... Any suggestions?



I've seen some questions related to lseek but I haven't found a solution to my problem.



EDIT:



I changed



  int fd = creat("/home/alum/Class/ej",S_IRWXU);


to



  int fd = open("/home/alum/Class/ej",O_CREAT|O_RDONLY|O_WRONLY|O_TRUNC);


and also tried



  int fd = open("/home/alum/Class/ej",O_CREAT|O_RDONLY|O_WRONLY|O_TRUNC,0700);


As suggested but I keep getting the same error.







c file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 9:20







Dgrm

















asked Nov 27 '18 at 9:07









DgrmDgrm

589




589








  • 5





    According to the manpage, "creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC", so it seems you can't read from a file opened with creat (flag O_WRONLY)

    – Karsten Koop
    Nov 27 '18 at 9:12











  • I tried using open and I get the same error (changes in the post)

    – Dgrm
    Nov 27 '18 at 9:21











  • Fixed it using O_RDWR, Thanks!

    – Dgrm
    Nov 27 '18 at 9:27






  • 1





    use perror instead of printf. It will print a human-readable error message.

    – Antti Haapala
    Nov 27 '18 at 10:11






  • 1





    Note: string is not a string when printf("Read[%d]: %sn",size,string) is called as string lacks a certain null character. (UB)

    – chux
    Nov 27 '18 at 11:08
















  • 5





    According to the manpage, "creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC", so it seems you can't read from a file opened with creat (flag O_WRONLY)

    – Karsten Koop
    Nov 27 '18 at 9:12











  • I tried using open and I get the same error (changes in the post)

    – Dgrm
    Nov 27 '18 at 9:21











  • Fixed it using O_RDWR, Thanks!

    – Dgrm
    Nov 27 '18 at 9:27






  • 1





    use perror instead of printf. It will print a human-readable error message.

    – Antti Haapala
    Nov 27 '18 at 10:11






  • 1





    Note: string is not a string when printf("Read[%d]: %sn",size,string) is called as string lacks a certain null character. (UB)

    – chux
    Nov 27 '18 at 11:08










5




5





According to the manpage, "creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC", so it seems you can't read from a file opened with creat (flag O_WRONLY)

– Karsten Koop
Nov 27 '18 at 9:12





According to the manpage, "creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC", so it seems you can't read from a file opened with creat (flag O_WRONLY)

– Karsten Koop
Nov 27 '18 at 9:12













I tried using open and I get the same error (changes in the post)

– Dgrm
Nov 27 '18 at 9:21





I tried using open and I get the same error (changes in the post)

– Dgrm
Nov 27 '18 at 9:21













Fixed it using O_RDWR, Thanks!

– Dgrm
Nov 27 '18 at 9:27





Fixed it using O_RDWR, Thanks!

– Dgrm
Nov 27 '18 at 9:27




1




1





use perror instead of printf. It will print a human-readable error message.

– Antti Haapala
Nov 27 '18 at 10:11





use perror instead of printf. It will print a human-readable error message.

– Antti Haapala
Nov 27 '18 at 10:11




1




1





Note: string is not a string when printf("Read[%d]: %sn",size,string) is called as string lacks a certain null character. (UB)

– chux
Nov 27 '18 at 11:08







Note: string is not a string when printf("Read[%d]: %sn",size,string) is called as string lacks a certain null character. (UB)

– chux
Nov 27 '18 at 11:08














1 Answer
1






active

oldest

votes


















1














I had to use



int fd = open("/home/alum/Class/ej",O_CREAT|O_RDWR|O_TRUNC,0700);


to create the file since 'creat' does not work for reading (manpage) and 'open' didn't work as O_RDWR for me if I used O_RDONLY|O_WRONLY.






share|improve this answer



















  • 3





    I used O_RDONLY|O_WRONLY. And it is completely wrong to do that: "Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag: O_EXEC ... O_RDONLY ... O_RDWR ... O_SEARCH ... O_WRONLY"

    – Andrew Henle
    Nov 27 '18 at 10:24













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%2f53496085%2fsimple-create-lseek-read-program-in-c%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









1














I had to use



int fd = open("/home/alum/Class/ej",O_CREAT|O_RDWR|O_TRUNC,0700);


to create the file since 'creat' does not work for reading (manpage) and 'open' didn't work as O_RDWR for me if I used O_RDONLY|O_WRONLY.






share|improve this answer



















  • 3





    I used O_RDONLY|O_WRONLY. And it is completely wrong to do that: "Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag: O_EXEC ... O_RDONLY ... O_RDWR ... O_SEARCH ... O_WRONLY"

    – Andrew Henle
    Nov 27 '18 at 10:24


















1














I had to use



int fd = open("/home/alum/Class/ej",O_CREAT|O_RDWR|O_TRUNC,0700);


to create the file since 'creat' does not work for reading (manpage) and 'open' didn't work as O_RDWR for me if I used O_RDONLY|O_WRONLY.






share|improve this answer



















  • 3





    I used O_RDONLY|O_WRONLY. And it is completely wrong to do that: "Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag: O_EXEC ... O_RDONLY ... O_RDWR ... O_SEARCH ... O_WRONLY"

    – Andrew Henle
    Nov 27 '18 at 10:24
















1












1








1







I had to use



int fd = open("/home/alum/Class/ej",O_CREAT|O_RDWR|O_TRUNC,0700);


to create the file since 'creat' does not work for reading (manpage) and 'open' didn't work as O_RDWR for me if I used O_RDONLY|O_WRONLY.






share|improve this answer













I had to use



int fd = open("/home/alum/Class/ej",O_CREAT|O_RDWR|O_TRUNC,0700);


to create the file since 'creat' does not work for reading (manpage) and 'open' didn't work as O_RDWR for me if I used O_RDONLY|O_WRONLY.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '18 at 9:29









DgrmDgrm

589




589








  • 3





    I used O_RDONLY|O_WRONLY. And it is completely wrong to do that: "Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag: O_EXEC ... O_RDONLY ... O_RDWR ... O_SEARCH ... O_WRONLY"

    – Andrew Henle
    Nov 27 '18 at 10:24
















  • 3





    I used O_RDONLY|O_WRONLY. And it is completely wrong to do that: "Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag: O_EXEC ... O_RDONLY ... O_RDWR ... O_SEARCH ... O_WRONLY"

    – Andrew Henle
    Nov 27 '18 at 10:24










3




3





I used O_RDONLY|O_WRONLY. And it is completely wrong to do that: "Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag: O_EXEC ... O_RDONLY ... O_RDWR ... O_SEARCH ... O_WRONLY"

– Andrew Henle
Nov 27 '18 at 10:24







I used O_RDONLY|O_WRONLY. And it is completely wrong to do that: "Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag: O_EXEC ... O_RDONLY ... O_RDWR ... O_SEARCH ... O_WRONLY"

– Andrew Henle
Nov 27 '18 at 10:24






















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%2f53496085%2fsimple-create-lseek-read-program-in-c%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)