system calls - Cannot write to a .txt file using write() function in C -


i wrote following code in order write random characters text file:

#include <stdio.h> #include <stdlib.h> #include <fcntl.h>  int main() {     int input_f = open("./input.txt", o_creat | o_append |  o_rdwr ,0666);     int i;     for(i=0;i<50;i++)     {         int r = rand()%252;         printf("%d size of r: %d\n",i,sizeof(r));         write(input_f,r,sizeof(r));         printf("%d wrote %d %c\n",i,r,r);     }      close(input_f); } 

i looked solutions

maybe here knows how can fix this?

write(input_f,r,sizeof(r)); 

should be

write(input_f, &r, sizeof(r)); 

the second parameter address of buffer want send according man page.

also should check return value of function equal sizeof r.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -