this program accepts user input and saved to a char array. Then creates a file and put those texts to the new file. Problem is, it can only copy the part before space.
Current Output : “how to read” –> “how”
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argv, char *argc[]){
int fd;
char buffer[100];
printf("Type your text : ");
scanf("%s",&buffer);
fd=open("menew.txt",O_CREAT|O_WRONLY|O_EXCL,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
if(fd<0){
printf("file already exist!\n");
}else printf("file created!\n");
fd=write(fd,buffer,20);
if(fd<0){
printf("error on writing...\n");
}else printf("successfully written!\n");
close(fd);
return 0;
}
Finally I found the solution!!!
Use
gets(buffer);instant ofscanf("%s",&buffer);