Karthik Chunduru
Karthik Chunduru

Reputation: 23

garbage values in file when using system call

I am Getting garbage values By Running this code

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
    int fd,k;
    fd=open("hello.txt",O_WRONLY|O_CREAT|O_TRUNC);
    char a[1000];
    scanf(" %s",a);
    k=write(fd,a,sizeof(a));
    close(fd);      
}

why I am getting garbage values in this hello.txt file when I run this program and enter the string as input?

Upvotes: 1

Views: 337

Answers (2)

Luis Colorado
Luis Colorado

Reputation: 12668

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
    int fd,k;
    fd=open("hello.txt",O_WRONLY|O_CREAT|O_TRUNC);
    char a[1000];

Upto here you create a variable to hold upto 1000 chars.

    scanf(" %s",a);

Here you read some characters (skip all spaces before a word, then a single word ---you read in a only until you get a space char or a new line---) and this means you only initialize part of the array)

Assume you input the string "Se esta quemando la serreria", the scanf() function only puts the three bytes 'S', 'e', and '\0' in the first three positions of a...

    k=write(fd,a,sizeof(a));

now, you write to fd the full size of a, this is the 1000 character positions that describe its sizeof value.

    close(fd);      
}

If, instead, you had calculated how many characters you entered in the string a (you can use strlen() library function for this purpose), then only the two characters before the '\0' char had been written.

    k=write(fd, a, strlen(a));

Final note: while sizeof and strlen() appear both as functions, that is not true:

  • sizeof is an operator, that computes the size of the type of the expression being introduced, or the size of the type name (in this case, you need the parenthesis, but not in the previous) So you can write just sizeof a (without the parenthesis, to remember you it is an operator) It's returned value is produced by the compiler at compilation time, as the types in C are of static nature (the types cannot change during the execution of a program)
  • strlen() is a library function, it allows you to find a variable string length, by searching for the '\0' byte in the string. Normally, functions that produce strings, take care of terminating them with such a character, and their length can be variable at runtime. This function will allow you to determine the amount of chars to write from a, and not the full set, as you did in your sample code.

Upvotes: 0

meaning-matters
meaning-matters

Reputation: 22946

Because sizeof() returns the memory size of a which is 1000.

a is created on the stack and its contents is undefined. After the scanf() only the first part of this stack garbage is overwritten.

You need to do write(fd, a, strlen(a)), or with strlen(a) + 1 if you want to also write the C string delimiter \0, which is added to the end of a.

Please check the return values op open(), scanf(), and write(), to make sure all steps are executed without error. See the manual pages for their return values.

Upvotes: 4

Related Questions