Write() to file descriptor disappearing when read()
up vote
0
down vote
favorite
So I'm working on the server side of my program right now, and I want to do the following:
1) open a file in read/write mode
2) append a word (WORD) to the end of the file
3) [I believe I have all of this part down already] open a pipe, create a child process, have it read directly from the file (file descriptor), execute a command, and send the result into the write/output of the pipe. The parent process reads from the read/input of the pipe and puts the info into a buffer to send back to the client.
What I'm having trouble with is the appending part. I'm pretty sure it appends to the file (with a newline in between the existing text and my WORD) because when I directly open the text file it's there. But when I try to print it from my buffer, it's not there. I have tried closing the file descriptor after writing and reopening and it's not there. I've tried strcat instead of writing to the file descriptor and it's not there.
#define WORD "WORD"
#define BUFFERLENGTH 512
char buffer[BUFFERLENGTH];
int fileDesc = open (filePath, O_RDWR|O_APPEND, 0660);
if (fileDesc <= 0)
write(clientDesc, ERRORMSG, BUFFERLENGTH);
exit(EXIT_FAILURE);
read(fileDesc,buffer,BUFFERLENGTH);
long length = lseek(fileDesc,0,SEEK_END);
int status = write(fileDesc,WORD,sizeof(WORD)-1);
read(fileDesc, buffer, BUFFER_LEN+1);
printf("new text: %sn", buffer); //WORD does not show up at the end of file as intended
Is there something I'm really misunderstanding?
Perhaps I don't fully understand how open(), read(), write(), and lseek() work, but if anyone could help explain to me why this isn't working as intended that'd be greatly appreciated. I've been struggling with this for the past week and the number of tabs I currently have open to searching for a solution is tragic.
c file-descriptor read-write lseek
add a comment |
up vote
0
down vote
favorite
So I'm working on the server side of my program right now, and I want to do the following:
1) open a file in read/write mode
2) append a word (WORD) to the end of the file
3) [I believe I have all of this part down already] open a pipe, create a child process, have it read directly from the file (file descriptor), execute a command, and send the result into the write/output of the pipe. The parent process reads from the read/input of the pipe and puts the info into a buffer to send back to the client.
What I'm having trouble with is the appending part. I'm pretty sure it appends to the file (with a newline in between the existing text and my WORD) because when I directly open the text file it's there. But when I try to print it from my buffer, it's not there. I have tried closing the file descriptor after writing and reopening and it's not there. I've tried strcat instead of writing to the file descriptor and it's not there.
#define WORD "WORD"
#define BUFFERLENGTH 512
char buffer[BUFFERLENGTH];
int fileDesc = open (filePath, O_RDWR|O_APPEND, 0660);
if (fileDesc <= 0)
write(clientDesc, ERRORMSG, BUFFERLENGTH);
exit(EXIT_FAILURE);
read(fileDesc,buffer,BUFFERLENGTH);
long length = lseek(fileDesc,0,SEEK_END);
int status = write(fileDesc,WORD,sizeof(WORD)-1);
read(fileDesc, buffer, BUFFER_LEN+1);
printf("new text: %sn", buffer); //WORD does not show up at the end of file as intended
Is there something I'm really misunderstanding?
Perhaps I don't fully understand how open(), read(), write(), and lseek() work, but if anyone could help explain to me why this isn't working as intended that'd be greatly appreciated. I've been struggling with this for the past week and the number of tabs I currently have open to searching for a solution is tragic.
c file-descriptor read-write lseek
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So I'm working on the server side of my program right now, and I want to do the following:
1) open a file in read/write mode
2) append a word (WORD) to the end of the file
3) [I believe I have all of this part down already] open a pipe, create a child process, have it read directly from the file (file descriptor), execute a command, and send the result into the write/output of the pipe. The parent process reads from the read/input of the pipe and puts the info into a buffer to send back to the client.
What I'm having trouble with is the appending part. I'm pretty sure it appends to the file (with a newline in between the existing text and my WORD) because when I directly open the text file it's there. But when I try to print it from my buffer, it's not there. I have tried closing the file descriptor after writing and reopening and it's not there. I've tried strcat instead of writing to the file descriptor and it's not there.
#define WORD "WORD"
#define BUFFERLENGTH 512
char buffer[BUFFERLENGTH];
int fileDesc = open (filePath, O_RDWR|O_APPEND, 0660);
if (fileDesc <= 0)
write(clientDesc, ERRORMSG, BUFFERLENGTH);
exit(EXIT_FAILURE);
read(fileDesc,buffer,BUFFERLENGTH);
long length = lseek(fileDesc,0,SEEK_END);
int status = write(fileDesc,WORD,sizeof(WORD)-1);
read(fileDesc, buffer, BUFFER_LEN+1);
printf("new text: %sn", buffer); //WORD does not show up at the end of file as intended
Is there something I'm really misunderstanding?
Perhaps I don't fully understand how open(), read(), write(), and lseek() work, but if anyone could help explain to me why this isn't working as intended that'd be greatly appreciated. I've been struggling with this for the past week and the number of tabs I currently have open to searching for a solution is tragic.
c file-descriptor read-write lseek
So I'm working on the server side of my program right now, and I want to do the following:
1) open a file in read/write mode
2) append a word (WORD) to the end of the file
3) [I believe I have all of this part down already] open a pipe, create a child process, have it read directly from the file (file descriptor), execute a command, and send the result into the write/output of the pipe. The parent process reads from the read/input of the pipe and puts the info into a buffer to send back to the client.
What I'm having trouble with is the appending part. I'm pretty sure it appends to the file (with a newline in between the existing text and my WORD) because when I directly open the text file it's there. But when I try to print it from my buffer, it's not there. I have tried closing the file descriptor after writing and reopening and it's not there. I've tried strcat instead of writing to the file descriptor and it's not there.
#define WORD "WORD"
#define BUFFERLENGTH 512
char buffer[BUFFERLENGTH];
int fileDesc = open (filePath, O_RDWR|O_APPEND, 0660);
if (fileDesc <= 0)
write(clientDesc, ERRORMSG, BUFFERLENGTH);
exit(EXIT_FAILURE);
read(fileDesc,buffer,BUFFERLENGTH);
long length = lseek(fileDesc,0,SEEK_END);
int status = write(fileDesc,WORD,sizeof(WORD)-1);
read(fileDesc, buffer, BUFFER_LEN+1);
printf("new text: %sn", buffer); //WORD does not show up at the end of file as intended
Is there something I'm really misunderstanding?
Perhaps I don't fully understand how open(), read(), write(), and lseek() work, but if anyone could help explain to me why this isn't working as intended that'd be greatly appreciated. I've been struggling with this for the past week and the number of tabs I currently have open to searching for a solution is tragic.
c file-descriptor read-write lseek
c file-descriptor read-write lseek
asked Nov 10 at 2:16
purpledots
2216
2216
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
After your write() call you're going to be at the end of the file, so read() isn't going to be able to read anything. You'll need to lseek() to a point earlier in the file if you want to be able to read anything from it.
You should be checking the return from read() (and almost all other system calls, for that matter) and use perror() or similar in the case of error, and this will do wonders for helping you to understand what's going on when you see behavior you don't expect.
Modifying your program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define WORD "WORD"
#define BUFFERLENGTH 512
int main(void)
O_APPEND, 0660);
if (fd < 0)
perror("couldn't open file");
return EXIT_FAILURE;
// Write word to end.
int status = write(fd, WORD, strlen(WORD));
if ( status < 0 )
perror("couldn't write");
return EXIT_FAILURE;
// Seek to start of file.
long length = lseek(fd, 0, SEEK_SET);
if ( length < 0 )
perror("couldn't lseek");
return EXIT_FAILURE;
// Read contents of file.
status = read(fd, buffer, BUFFERLENGTH - 1);
if ( status < 0 )
perror("couldn't read");
return EXIT_FAILURE;
// Print buffer.
printf("file contents: %sn", buffer);
return 0;
yields:
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file
file contents: WORD
paul@mac:scratch$ ./file
file contents: WORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORDWORD
paul@mac:scratch$
If you want to actually see only the new contents, then you'll need to lseek() to some point other than the start of the file. Since a successful write() will return the number of bytes written, you can use this value to offset back from the end of the file:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define BUFFERLENGTH 512
int main(int argc, char * argv)
if ( argc < 2 )
fprintf(stderr, "you need to enter a word argumentn");
return EXIT_FAILURE;
char * filePath = "testfile.txt";
char buffer[BUFFERLENGTH] = 0;
// Open file.
int fd = open(filePath, O_RDWR
yielding:
paul@mac:scratch$ rm testfile.txt
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file2 these
new text: these
paul@mac:scratch$ ./file2 are
new text: are
paul@mac:scratch$ ./file2 some
new text: some
paul@mac:scratch$ ./file2 words
new text: words
paul@mac:scratch$ cat testfile.txt
thesearesomewordspaul@mac:scratch$
Thank you for your advice! I guess what I'm confused about then is how come after writing to the file the read/print will print from the beginning of the file and stop before the WORD. If what you suggest is that I'm reading from the end shouldn't it have printed nothing? I've also tried closing the file and opening it again to read, which I'm also assuming read from the beginning but the ending text didn't print : Maybe I'm not understanding
– purpledots
Nov 10 at 2:45
@purpledots: You'll need to show a full compilable program along with the input you're using and the output you're getting in order for me to be able to explain that, I can't reproduce what you describe based on what you've shown.
– Paul Griffiths
Nov 10 at 2:49
@purpledots: Do you need toflush()those 4 characters out of the buffer? Did you change the read length to accommodate the extra characters? I notice you have two different constants for the buffer length in your code:BUFFERLENGTHandBUFFER_LEN.
– Omegaman
Nov 10 at 2:59
@Omegaman: Unix file i/o withread(),write()and friends is unbuffered (the disk block buffering at the OS level is transparent to the programmer) so there'll be nothing that needs flushing, here.
– Paul Griffiths
Nov 10 at 3:05
@Omegaman I'm not too familiar with flush() but is that to clear out the buffer? I'm not trying to clear it out, just display what's in there. And oops yes I found that in my code as well thanks :)
– purpledots
Nov 10 at 3:09
|
show 4 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
After your write() call you're going to be at the end of the file, so read() isn't going to be able to read anything. You'll need to lseek() to a point earlier in the file if you want to be able to read anything from it.
You should be checking the return from read() (and almost all other system calls, for that matter) and use perror() or similar in the case of error, and this will do wonders for helping you to understand what's going on when you see behavior you don't expect.
Modifying your program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define WORD "WORD"
#define BUFFERLENGTH 512
int main(void)
O_APPEND, 0660);
if (fd < 0)
perror("couldn't open file");
return EXIT_FAILURE;
// Write word to end.
int status = write(fd, WORD, strlen(WORD));
if ( status < 0 )
perror("couldn't write");
return EXIT_FAILURE;
// Seek to start of file.
long length = lseek(fd, 0, SEEK_SET);
if ( length < 0 )
perror("couldn't lseek");
return EXIT_FAILURE;
// Read contents of file.
status = read(fd, buffer, BUFFERLENGTH - 1);
if ( status < 0 )
perror("couldn't read");
return EXIT_FAILURE;
// Print buffer.
printf("file contents: %sn", buffer);
return 0;
yields:
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file
file contents: WORD
paul@mac:scratch$ ./file
file contents: WORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORDWORD
paul@mac:scratch$
If you want to actually see only the new contents, then you'll need to lseek() to some point other than the start of the file. Since a successful write() will return the number of bytes written, you can use this value to offset back from the end of the file:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define BUFFERLENGTH 512
int main(int argc, char * argv)
if ( argc < 2 )
fprintf(stderr, "you need to enter a word argumentn");
return EXIT_FAILURE;
char * filePath = "testfile.txt";
char buffer[BUFFERLENGTH] = 0;
// Open file.
int fd = open(filePath, O_RDWR
yielding:
paul@mac:scratch$ rm testfile.txt
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file2 these
new text: these
paul@mac:scratch$ ./file2 are
new text: are
paul@mac:scratch$ ./file2 some
new text: some
paul@mac:scratch$ ./file2 words
new text: words
paul@mac:scratch$ cat testfile.txt
thesearesomewordspaul@mac:scratch$
Thank you for your advice! I guess what I'm confused about then is how come after writing to the file the read/print will print from the beginning of the file and stop before the WORD. If what you suggest is that I'm reading from the end shouldn't it have printed nothing? I've also tried closing the file and opening it again to read, which I'm also assuming read from the beginning but the ending text didn't print : Maybe I'm not understanding
– purpledots
Nov 10 at 2:45
@purpledots: You'll need to show a full compilable program along with the input you're using and the output you're getting in order for me to be able to explain that, I can't reproduce what you describe based on what you've shown.
– Paul Griffiths
Nov 10 at 2:49
@purpledots: Do you need toflush()those 4 characters out of the buffer? Did you change the read length to accommodate the extra characters? I notice you have two different constants for the buffer length in your code:BUFFERLENGTHandBUFFER_LEN.
– Omegaman
Nov 10 at 2:59
@Omegaman: Unix file i/o withread(),write()and friends is unbuffered (the disk block buffering at the OS level is transparent to the programmer) so there'll be nothing that needs flushing, here.
– Paul Griffiths
Nov 10 at 3:05
@Omegaman I'm not too familiar with flush() but is that to clear out the buffer? I'm not trying to clear it out, just display what's in there. And oops yes I found that in my code as well thanks :)
– purpledots
Nov 10 at 3:09
|
show 4 more comments
up vote
2
down vote
After your write() call you're going to be at the end of the file, so read() isn't going to be able to read anything. You'll need to lseek() to a point earlier in the file if you want to be able to read anything from it.
You should be checking the return from read() (and almost all other system calls, for that matter) and use perror() or similar in the case of error, and this will do wonders for helping you to understand what's going on when you see behavior you don't expect.
Modifying your program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define WORD "WORD"
#define BUFFERLENGTH 512
int main(void)
O_APPEND, 0660);
if (fd < 0)
perror("couldn't open file");
return EXIT_FAILURE;
// Write word to end.
int status = write(fd, WORD, strlen(WORD));
if ( status < 0 )
perror("couldn't write");
return EXIT_FAILURE;
// Seek to start of file.
long length = lseek(fd, 0, SEEK_SET);
if ( length < 0 )
perror("couldn't lseek");
return EXIT_FAILURE;
// Read contents of file.
status = read(fd, buffer, BUFFERLENGTH - 1);
if ( status < 0 )
perror("couldn't read");
return EXIT_FAILURE;
// Print buffer.
printf("file contents: %sn", buffer);
return 0;
yields:
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file
file contents: WORD
paul@mac:scratch$ ./file
file contents: WORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORDWORD
paul@mac:scratch$
If you want to actually see only the new contents, then you'll need to lseek() to some point other than the start of the file. Since a successful write() will return the number of bytes written, you can use this value to offset back from the end of the file:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define BUFFERLENGTH 512
int main(int argc, char * argv)
if ( argc < 2 )
fprintf(stderr, "you need to enter a word argumentn");
return EXIT_FAILURE;
char * filePath = "testfile.txt";
char buffer[BUFFERLENGTH] = 0;
// Open file.
int fd = open(filePath, O_RDWR
yielding:
paul@mac:scratch$ rm testfile.txt
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file2 these
new text: these
paul@mac:scratch$ ./file2 are
new text: are
paul@mac:scratch$ ./file2 some
new text: some
paul@mac:scratch$ ./file2 words
new text: words
paul@mac:scratch$ cat testfile.txt
thesearesomewordspaul@mac:scratch$
Thank you for your advice! I guess what I'm confused about then is how come after writing to the file the read/print will print from the beginning of the file and stop before the WORD. If what you suggest is that I'm reading from the end shouldn't it have printed nothing? I've also tried closing the file and opening it again to read, which I'm also assuming read from the beginning but the ending text didn't print : Maybe I'm not understanding
– purpledots
Nov 10 at 2:45
@purpledots: You'll need to show a full compilable program along with the input you're using and the output you're getting in order for me to be able to explain that, I can't reproduce what you describe based on what you've shown.
– Paul Griffiths
Nov 10 at 2:49
@purpledots: Do you need toflush()those 4 characters out of the buffer? Did you change the read length to accommodate the extra characters? I notice you have two different constants for the buffer length in your code:BUFFERLENGTHandBUFFER_LEN.
– Omegaman
Nov 10 at 2:59
@Omegaman: Unix file i/o withread(),write()and friends is unbuffered (the disk block buffering at the OS level is transparent to the programmer) so there'll be nothing that needs flushing, here.
– Paul Griffiths
Nov 10 at 3:05
@Omegaman I'm not too familiar with flush() but is that to clear out the buffer? I'm not trying to clear it out, just display what's in there. And oops yes I found that in my code as well thanks :)
– purpledots
Nov 10 at 3:09
|
show 4 more comments
up vote
2
down vote
up vote
2
down vote
After your write() call you're going to be at the end of the file, so read() isn't going to be able to read anything. You'll need to lseek() to a point earlier in the file if you want to be able to read anything from it.
You should be checking the return from read() (and almost all other system calls, for that matter) and use perror() or similar in the case of error, and this will do wonders for helping you to understand what's going on when you see behavior you don't expect.
Modifying your program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define WORD "WORD"
#define BUFFERLENGTH 512
int main(void)
O_APPEND, 0660);
if (fd < 0)
perror("couldn't open file");
return EXIT_FAILURE;
// Write word to end.
int status = write(fd, WORD, strlen(WORD));
if ( status < 0 )
perror("couldn't write");
return EXIT_FAILURE;
// Seek to start of file.
long length = lseek(fd, 0, SEEK_SET);
if ( length < 0 )
perror("couldn't lseek");
return EXIT_FAILURE;
// Read contents of file.
status = read(fd, buffer, BUFFERLENGTH - 1);
if ( status < 0 )
perror("couldn't read");
return EXIT_FAILURE;
// Print buffer.
printf("file contents: %sn", buffer);
return 0;
yields:
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file
file contents: WORD
paul@mac:scratch$ ./file
file contents: WORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORDWORD
paul@mac:scratch$
If you want to actually see only the new contents, then you'll need to lseek() to some point other than the start of the file. Since a successful write() will return the number of bytes written, you can use this value to offset back from the end of the file:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define BUFFERLENGTH 512
int main(int argc, char * argv)
if ( argc < 2 )
fprintf(stderr, "you need to enter a word argumentn");
return EXIT_FAILURE;
char * filePath = "testfile.txt";
char buffer[BUFFERLENGTH] = 0;
// Open file.
int fd = open(filePath, O_RDWR
yielding:
paul@mac:scratch$ rm testfile.txt
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file2 these
new text: these
paul@mac:scratch$ ./file2 are
new text: are
paul@mac:scratch$ ./file2 some
new text: some
paul@mac:scratch$ ./file2 words
new text: words
paul@mac:scratch$ cat testfile.txt
thesearesomewordspaul@mac:scratch$
After your write() call you're going to be at the end of the file, so read() isn't going to be able to read anything. You'll need to lseek() to a point earlier in the file if you want to be able to read anything from it.
You should be checking the return from read() (and almost all other system calls, for that matter) and use perror() or similar in the case of error, and this will do wonders for helping you to understand what's going on when you see behavior you don't expect.
Modifying your program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define WORD "WORD"
#define BUFFERLENGTH 512
int main(void)
O_APPEND, 0660);
if (fd < 0)
perror("couldn't open file");
return EXIT_FAILURE;
// Write word to end.
int status = write(fd, WORD, strlen(WORD));
if ( status < 0 )
perror("couldn't write");
return EXIT_FAILURE;
// Seek to start of file.
long length = lseek(fd, 0, SEEK_SET);
if ( length < 0 )
perror("couldn't lseek");
return EXIT_FAILURE;
// Read contents of file.
status = read(fd, buffer, BUFFERLENGTH - 1);
if ( status < 0 )
perror("couldn't read");
return EXIT_FAILURE;
// Print buffer.
printf("file contents: %sn", buffer);
return 0;
yields:
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file
file contents: WORD
paul@mac:scratch$ ./file
file contents: WORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORD
paul@mac:scratch$ ./file
file contents: WORDWORDWORDWORD
paul@mac:scratch$
If you want to actually see only the new contents, then you'll need to lseek() to some point other than the start of the file. Since a successful write() will return the number of bytes written, you can use this value to offset back from the end of the file:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define BUFFERLENGTH 512
int main(int argc, char * argv)
if ( argc < 2 )
fprintf(stderr, "you need to enter a word argumentn");
return EXIT_FAILURE;
char * filePath = "testfile.txt";
char buffer[BUFFERLENGTH] = 0;
// Open file.
int fd = open(filePath, O_RDWR
yielding:
paul@mac:scratch$ rm testfile.txt
paul@mac:scratch$ touch testfile.txt
paul@mac:scratch$ ./file2 these
new text: these
paul@mac:scratch$ ./file2 are
new text: are
paul@mac:scratch$ ./file2 some
new text: some
paul@mac:scratch$ ./file2 words
new text: words
paul@mac:scratch$ cat testfile.txt
thesearesomewordspaul@mac:scratch$
edited Nov 10 at 2:48
answered Nov 10 at 2:26
Paul Griffiths
18.2k22648
18.2k22648
Thank you for your advice! I guess what I'm confused about then is how come after writing to the file the read/print will print from the beginning of the file and stop before the WORD. If what you suggest is that I'm reading from the end shouldn't it have printed nothing? I've also tried closing the file and opening it again to read, which I'm also assuming read from the beginning but the ending text didn't print : Maybe I'm not understanding
– purpledots
Nov 10 at 2:45
@purpledots: You'll need to show a full compilable program along with the input you're using and the output you're getting in order for me to be able to explain that, I can't reproduce what you describe based on what you've shown.
– Paul Griffiths
Nov 10 at 2:49
@purpledots: Do you need toflush()those 4 characters out of the buffer? Did you change the read length to accommodate the extra characters? I notice you have two different constants for the buffer length in your code:BUFFERLENGTHandBUFFER_LEN.
– Omegaman
Nov 10 at 2:59
@Omegaman: Unix file i/o withread(),write()and friends is unbuffered (the disk block buffering at the OS level is transparent to the programmer) so there'll be nothing that needs flushing, here.
– Paul Griffiths
Nov 10 at 3:05
@Omegaman I'm not too familiar with flush() but is that to clear out the buffer? I'm not trying to clear it out, just display what's in there. And oops yes I found that in my code as well thanks :)
– purpledots
Nov 10 at 3:09
|
show 4 more comments
Thank you for your advice! I guess what I'm confused about then is how come after writing to the file the read/print will print from the beginning of the file and stop before the WORD. If what you suggest is that I'm reading from the end shouldn't it have printed nothing? I've also tried closing the file and opening it again to read, which I'm also assuming read from the beginning but the ending text didn't print : Maybe I'm not understanding
– purpledots
Nov 10 at 2:45
@purpledots: You'll need to show a full compilable program along with the input you're using and the output you're getting in order for me to be able to explain that, I can't reproduce what you describe based on what you've shown.
– Paul Griffiths
Nov 10 at 2:49
@purpledots: Do you need toflush()those 4 characters out of the buffer? Did you change the read length to accommodate the extra characters? I notice you have two different constants for the buffer length in your code:BUFFERLENGTHandBUFFER_LEN.
– Omegaman
Nov 10 at 2:59
@Omegaman: Unix file i/o withread(),write()and friends is unbuffered (the disk block buffering at the OS level is transparent to the programmer) so there'll be nothing that needs flushing, here.
– Paul Griffiths
Nov 10 at 3:05
@Omegaman I'm not too familiar with flush() but is that to clear out the buffer? I'm not trying to clear it out, just display what's in there. And oops yes I found that in my code as well thanks :)
– purpledots
Nov 10 at 3:09
Thank you for your advice! I guess what I'm confused about then is how come after writing to the file the read/print will print from the beginning of the file and stop before the WORD. If what you suggest is that I'm reading from the end shouldn't it have printed nothing? I've also tried closing the file and opening it again to read, which I'm also assuming read from the beginning but the ending text didn't print : Maybe I'm not understanding
– purpledots
Nov 10 at 2:45
Thank you for your advice! I guess what I'm confused about then is how come after writing to the file the read/print will print from the beginning of the file and stop before the WORD. If what you suggest is that I'm reading from the end shouldn't it have printed nothing? I've also tried closing the file and opening it again to read, which I'm also assuming read from the beginning but the ending text didn't print : Maybe I'm not understanding
– purpledots
Nov 10 at 2:45
@purpledots: You'll need to show a full compilable program along with the input you're using and the output you're getting in order for me to be able to explain that, I can't reproduce what you describe based on what you've shown.
– Paul Griffiths
Nov 10 at 2:49
@purpledots: You'll need to show a full compilable program along with the input you're using and the output you're getting in order for me to be able to explain that, I can't reproduce what you describe based on what you've shown.
– Paul Griffiths
Nov 10 at 2:49
@purpledots: Do you need to
flush() those 4 characters out of the buffer? Did you change the read length to accommodate the extra characters? I notice you have two different constants for the buffer length in your code: BUFFERLENGTH and BUFFER_LEN.– Omegaman
Nov 10 at 2:59
@purpledots: Do you need to
flush() those 4 characters out of the buffer? Did you change the read length to accommodate the extra characters? I notice you have two different constants for the buffer length in your code: BUFFERLENGTH and BUFFER_LEN.– Omegaman
Nov 10 at 2:59
@Omegaman: Unix file i/o with
read(), write() and friends is unbuffered (the disk block buffering at the OS level is transparent to the programmer) so there'll be nothing that needs flushing, here.– Paul Griffiths
Nov 10 at 3:05
@Omegaman: Unix file i/o with
read(), write() and friends is unbuffered (the disk block buffering at the OS level is transparent to the programmer) so there'll be nothing that needs flushing, here.– Paul Griffiths
Nov 10 at 3:05
@Omegaman I'm not too familiar with flush() but is that to clear out the buffer? I'm not trying to clear it out, just display what's in there. And oops yes I found that in my code as well thanks :)
– purpledots
Nov 10 at 3:09
@Omegaman I'm not too familiar with flush() but is that to clear out the buffer? I'm not trying to clear it out, just display what's in there. And oops yes I found that in my code as well thanks :)
– purpledots
Nov 10 at 3:09
|
show 4 more comments
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53235473%2fwrite-to-file-descriptor-disappearing-when-read%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