954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

strange behaviour of sys calls

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

int main( void )
{
        int fd,nob;
        char str[1024];
        fd = open("test.txt",O_RDWR|O_CREAT,S_IRWXU);
        if (fd < 0) {
                perror("open:");
                exit(0);
        }

        nob = write(fd,"hello first",1024);
        close(fd);

        printf(" %d nob written",nob);

        fd = open("test.txt",O_RDWR);
        nob = read(fd,str,1024);
        close(fd);

        printf(" %d nob read",nob);
        printf(" %s \n", str);
return 0;
}

The above code outputting 1024 nob written 1024 nob read hello first
on the terminal.

when i say cat test.txt the file contents are hello first %d nob written %d nob read %s

i heard about buffering , is it because of that , but i am closing the files immediately i finish the file operation.

any suggestions or help please...

Gaiety
Junior Poster
120 posts since Sep 2009
Reputation Points: 13
Solved Threads: 3
 

You are telling the write call (which is not looking for null terminated strings) to write up to 1024 bytes but only providing 11 bytes to be written. write will happily try to continue to read from wherever "hello first" is stored in memory for 1024 bytes. Since constant strings are usually stored together in memory in an executable file you end up writing all of the constant strings from the program into your file.

To see what I mean, try the following change:

char msg[] = "hello first";
/* ... */
nob = write (fd, msg, 1024);


and examine what changes.

When you call write you need to provide the amount of data you want writtenexactly. So something like nob = write (fd, msg, strlen (msg));

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

You're using printf correctly with the format specifiers (such as %d), but the data you're seeing in the file is a bit of gibberish.

In your write command, the buffer you specify to write is a string "hello first" (length of 11), but you give it a write size of 1024. This means that it writes 1024 bytes from the start of the "hello first" string. This includes the string and the following 1013 bytes in memory.

To fix this, you should make the write size the same size as the string:

nob = write(fd,"hello first",11);


EDIT: Whoops, I was writing while L7Sqr posted.

LaMouche
Posting Whiz in Training
269 posts since Oct 2006
Reputation Points: 83
Solved Threads: 39
 

Thank you L7Sqr and LaMouche for your explanation.

i dint know that "write" behaviour, its too dangerous.

Thank you very much..

Gaiety
Junior Poster
120 posts since Sep 2009
Reputation Points: 13
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You