I have no idea how to use system calls but after some search on internet I came up with this code. Can you please correct my code ?
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main()
{
char ch;
int file;
open(file, "input.txt", O_RDONLY);
read(file, ch, 1);
while (ch != '\0')
{
printf("%c", ch);
fseek(file, 1, SEEK_CUR);
read(file, ch, 1);
}
return 0;
}
you are almost right in doing, dont need to use fseek inside ,it will be automatically moved.
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int retVal = 0;
unsigned char ch;
int fdr = open(argv[1],O_RDONLY);
if( fdr )
{
while(read(fdr,&ch,1) == 1)
printf("%c", ch);
retVal = 0;
}
else
{
printf("File open failed\n");
retVal = -1;
}
return retVal;
}
Related
I have 2 programs.
One is server one is client. The Client sending string to the server, the server is swapping the string and return to the client.
the problems I have is when I pressing ctr+c the program(client) closed.(I'm using threads because I have been asking for.)
So how can I keep the program unclosed after ctr+c?
when clicking enter this is the right result and waiting for the server.
when entering ctl+c the program closed and cannot running server.c
The client.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#define MAX_BUF 1024
// client
int main()
{
int fd;
char *myfifo = "myfifo";
char str[MAX_BUF];
printf("Input string: ");
fgets(str, MAX_BUF, stdin);
str[strlen(str) - 1] = '\0';
if (mkfifo("myfifo", 0777) == -1)
{
if (errno != EEXIST)
{
printf("Could not create fifo file\n");
return 1;
}
}
/* create the FIFO (named pipe) */
fd = open(myfifo, O_WRONLY);
if (fd == -1)
return 2;
if (write(fd, str, MAX_BUF) == -1)
return 3;
close(fd);
fd = open(myfifo, O_RDONLY);
read(fd, str, MAX_BUF);
printf("%s\n", str);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
The server
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#define MAX_BUF 1024
// server
// creatring struct for saving data
typedef struct thread_data
{
char str[MAX_BUF];
int result;
} thread_data;
// the
void *routine(void *arg)
{
int fd[2];
char *myfifo = "myfifo";
thread_data *tdata = (thread_data *)arg;
if (!(strcmp(tdata->str, "exit")))
tdata->result = 1; // is exit
else
tdata->result = 0; // not exit
if (tdata->result == 1)
{
fd[1] = open(myfifo, O_WRONLY);
write(fd[1], "Done", sizeof(MAX_BUF));
close(fd[1]);
}
else
{
char string[MAX_BUF] = {0};
char c = 0;
int length = 0, i = 0;
length = strlen(tdata->str);
printf("\nBefore Swap : %s\n", tdata->str);
for (i = 0; i < length / 2; i++)
{
c = tdata->str[i];
tdata->str[i] = tdata->str[length - 1 - i];
tdata->str[length - 1 - i] = c;
}
printf("\nAfter Swap String : %s\n", tdata->str);
fd[1] = open(myfifo, O_WRONLY);
write(fd[1], tdata->str, sizeof(MAX_BUF));
close(fd[1]);
}
pthread_exit(NULL);
}
int Calculation()
{
int fd[2];
// fd[0] read
// fd[1] write
char *myfifo = "myfifo";
char buf[MAX_BUF];
/* open, read, and display the message from the FIFO */
fd[0] = open(myfifo, O_RDONLY);
if(fd[0]==-1)
return 4;
if(read(fd[0], buf, MAX_BUF)==-1)
return 5;
close(fd[0]);
// res
int result;
thread_data tdata;
strcpy(tdata.str, buf);
pthread_t t1;
if (pthread_create(&t1, NULL, &routine, (void *)&tdata) != 0)
{
return 1;
}
if (pthread_join(t1, NULL) != 0)
{
return 2;
}
}
int main()
{
int res = Calculation();
return res;
}
To avoid closing a program you should capture and
manage the signal sent by CTRL + C, that is SIGINT, I have modified the code so that it captures the signal CTRL + C
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#define MAX_BUF 1024
// client
int main()
{
signal(SIGINT,SIG_IGN); // Register signal handler for ignoring the signal
int fd;
char *myfifo = "myfifo";
char str[MAX_BUF];
printf("Input string: ");
fgets(str, MAX_BUF, stdin);
str[strlen(str) - 1] = '\0';
if (mkfifo("myfifo", 0777) == -1)
{
if (errno != EEXIST)
{
printf("Could not create fifo file\n");
return 1;
}
}
/* create the FIFO (named pipe) */
fd = open(myfifo, O_WRONLY);
if (fd == -1)
return 2;
if (write(fd, str, MAX_BUF) == -1)
return 3;
close(fd);
fd = open(myfifo, O_RDONLY);
read(fd, str, MAX_BUF);
printf("%s\n", str);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
I am trying to make a C program that concatenates the contents of two files at a target file. To do this I must have at least 2 files open at the same time but I haven't figured out why I can't. The problem can be sufficiently described by the two pieces of code below:
Why does this work:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
size_t fdes = open(argv[1], O_RDONLY);
void * buf;
int bytes;
while ((bytes = (int)read(fdes, buf, 3)) > 0) {
printf("%s", (char*)buf);
}
close(fdes);
return 0;
}
and this doesn't?
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
size_t fdes = open(argv[1], O_RDONLY);
size_t fdes2 = open(argv[2], O_RDONLY);
void * buf;
int bytes;
while ((bytes = (int)read(fdes, buf, 3)) > 0) {
printf("%s", (char*)buf);
}
close(fdes);
close(fdes2);
return 0;
}
can't i have multiple files open?
I make some changes, and this works:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
size_t fdes = open(argv[1], O_RDONLY);
size_t fdes2 = open(argv[2], O_RDONLY);
size_t length = 0;
char * buf = (char *)malloc(sizeof(char) * length);
int bytes;
while ((bytes = (int)read(fdes, buf, sizeof(buf) - 1)) > 0) {
printf("%s", buf);
}
while ((bytes = (int)read(fdes2, buf, sizeof(buf) - 1)) > 0) {
printf("%s", buf);
}
close(fdes);
close(fdes2);
return 0;
}
First, buf should be char *
Second, you need to point buf to somewhere (malloc or a char array)
Reference: https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/rtrea.htm
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <poll.h>
#include <string.h>
#include <errno.h>
#define BUFSIZE 256
char buffer[BUFSIZE];
int readBytes;
struct termios saved;
struct termios modif;
void onExit(){
if(tcsetattr(STDIN_FILENO, TCSANOW, &saved) == -1)
readBytes = readBytes; //placeholder
}
void nonCanonMode(){
if(tcgetattr(STDIN_FILENO, &saved) == -1)
readBytes = readBytes;
if(tcgetattr(STDIN_FILENO, &modif) == -1)
readBytes = readBytes;
modif.c_iflag = ISTRIP;
modif.c_oflag = 0;
modif.c_lflag = 0;
if(tcsetattr(STDIN_FILENO, TCSANOW, &modif) == -1)
readBytes = readBytes;
atexit(onExit);
}
void readChar(){
int counter = 0;
readBytes = read(STDIN_FILENO, buffer, BUFSIZE);
if(readBytes == -1)
readBytes = readBytes;
while(counter < readBytes){
if(buffer[counter] == '\004'){
exit(0);
}
else{
if(write(STDOUT_FILENO, &buffer[counter], sizeof(char)) == -1)
readBytes = readBytes;
write(STDOUT_FILENO, &buffer[counter], sizeof(char));
}
counter++;
}
}
int main(){
nonCanonMode();
while(1)
readChar();
exit(0);
}
The readChar() function doesn't print out characters as they are typed in. I put my terminal into noncanonical mode and loop this function indefinitely to read a character at a time. However the function only writes to stdout once. If I comment out the second write then there's no output.
I tried reading from the file "hello.txt" but it doesn't enter the while loop at all. The read function returns 0 on EOF and -1 on error. I'm trying to search for the word in w if it exists in the file or not. I'm reading characters from the file and comparing them to w[].
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
char c;
int i=0;
int bytesread;
int flag=1;
char w[]={'h','e','l','l','o'};
if((fd=open("hello.txt",O_RDONLY,0))!=-1){ //if 1
bytesread = read(fd,&c,0);
if(bytesread!=-1){ //if 2
while(bytesread!=0)
{ //while
if(c==w[i])
{ //if 3
i++;
flag=0;
} //end of f3
else if(flag==0&&i!=0)
{ // else 3
i=0;
flag=1;
} // end of else 3
bytesread = read(fd,&c,0);
} //end of while
}else //end of if 2
printf("couldn't read file.\n");
}else //end of if 1
printf("Couldn't open file for read.\n");
} //end of main
ssize_t read(int fd, void *buf, size_t count);
read reads count bytes from the file. You're asking it to read zero bytes when doing bytesread = read(fd,&c,0);. Change it to bytesread = read(fd,&c,1);
Try this:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
int fd;
char c;
int bytesread;
if ((fd = open("hello.txt", O_RDONLY, 0)) != -1) {
while ((bytesread = read(fd, &c, 1)) == 1)
printf("read %d bytes [%c]\n", bytesread, c);
} else
printf("Couldn't open file for read.\n");
return 0;
}
read(fd,&c,0) asks the system to read zero bytes, and it shouldn't be what you want to do.
You should ask the system to read one byte by read(fd,&c,1).
Suppose I had a file that I wanted to read.
./a.out file // where file is a argument
The Program being:
//program.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char ch, file_name[25] = argv[1]; //??? Is the issue here?
FILE *fp;
fp = fopen(file_name,"r");
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
return 0;
}
EDITED:
When compiling with gcc
gcc program.c
program.c: file not recognized: File format not recognized
Is the error Im getting.
//program.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SIZEBUF 1024
int main(int argc, char *argv[])
{
int fd;
char buffer[SIZEBUF];
if (argc < 2)
{
printf("Missing file\n");
return (EXIT_FAILURE);
}
fd = open(argv[1], O_RDONLY);
if (fd == -1)
{
printf("Some error occured\n");
return (EXIT_FAILURE);
}
printf("The contents of %s file are :\n", argv[1]);
while(read(fd, buffer, SIZEBUF) > 0 )
printf("%s", buffer);
close(fd);
return (EXIT_SUCCESS);
}
Try this.
Yes.
But if you're in doubt, you could try printing the things in argv.