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

void *func(void *ptr);
int file;

int main()
{
  pthread_t thread1, thread2;
  int iret1, iret2;
  int  p;
  p=1;

  file=open("file1.txt", O_CREAT | O_TRUNC | O_RDWR , 0666);
  iret1 = pthread_create(&thread1, NULL, func, (void *)&p);
  pthread_join(thread1, NULL);

  close(file);
  exit(0);
}

void *func(void *ptr)
{
  int *num;
  int i;
  num = (int *)ptr;

  printf("%d ", *num);
  for (i=0; i<10; i++)
  {
    printf("%d", *num);
    write(file, *num, sizeof(*num));
  }
}

How to write integer var to file using write() function in c? This is my code. The problem is in the func(). If I use chars or const int it's working fine.

2
  • 4
    Please read the manual for write. Commented Jan 7, 2014 at 10:45
  • int or const int shouldn't show different results. Commented Jan 7, 2014 at 16:42

2 Answers 2

2

First, read the man page of write(). It writes bytes, not element types.

So, what you are trying to achieve, cannot be accomplished directly with write(). you need to use snprintf() to convert the int to char string, which you can use with write(). Please check the following code.

1. Define a char array, print the value of the int pointer to that array using snprintf().
2. Use the char array as the argument of write(). It'll work.

NOTE: It's always a best practice to add some error check to the system calls and library calls. It provides many useful information in the case they fail.

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

void *func(void *ptr);
int file;

int main()
{
        pthread_t thread1, thread2;
        int iret1, iret2;
        int  p;
        p=1;

        file=open("file1.txt", O_CREAT | O_TRUNC | O_RDWR , 0666);
        iret1 = pthread_create(&thread1, NULL, func, (void *)&p);
        pthread_join(thread1, NULL);

        close(file);
        exit(0);
}

void *func(void *ptr)
{
        int *num;
        int i, ret = -1;
        num = (int *)ptr;
        char buf[4] = {0};                              //to use with write()

        printf("%d\n", *num);
        for (i=0; i<10; i++)
        {
                printf("%d", *num);
                memset(buf, 0, sizeof (buf));
                snprintf(buf, sizeof *num, "%d", *num);               //print to buffer 
                ret = write(file, buf, strlen(buf));    //write the buf to file
                if (ret < 0)                            //check for erroneous condition
                        printf("ret is %d, errno %d\n", ret, errno);
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Shouldn't it be snprintf(buf, (sizeof *num + 1), "%d", *num);? snprintf writes atmost size bites including the null termination char. ofcourse then buf should be buf[5]
1

write(fd, &var, sizeof(i)); is a short fix for this question.

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person who asked. Consider editing your answer to add explanations and/or give an indication of what limitations and assumptions apply. See how to answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.