Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
This is a small C program to test the client and server programs so that the client sends an integer to the client. The server multiplies the number by 10 and returns the integer*10 back to the client. When writing the integers to the FIFO.
This is my code so far:
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
main (void)
{
int fda;
int fdb;
int number;
int outputnumber;
if((fda=open("FIFO_to_server", O_WRONLY))<0)
printf("cant open fifo to write");
if((fdb=open("FIFO_to_client", O_RDONLY))<0)
printf("cant open fifo to read");
printf("Client: Please enter an integer: ");
scanf("%d", &number);
write(fda, number, sizeof(number));
printf("\nClient: Got the number sent, now waiting for response ");
read(fdb, outputnumber, sizeof(outputnumber));
printf("\nClient: received from server %s", outputnumber);
close(fda);
close(fdb);
printf ("\nall done!\n");
}
after compiling, I had some error:
-bash-3.2$ gcc clientHw.c -o client
clientHw.c: In function `main':
clientHw.c:36: warning: passing arg 2 of `write' makes pointer from integer
without a cast
clientHw.c:38: warning: passing arg 2 of `read' makes pointer from integer
without a cast
You must pass the address of the variables like this:
write(fda, &number, sizeof(number));
...
read(fdb, &outputnumber, sizeof(outputnumber));
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I am studying this youtube tutorial by ShellWave which learns you how to program in C on a Linux device and for some reason am getting stuck at lesson #024 : Youtube
My code is the following (I used the same as in the video) :
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd;
char buf[14];
//write
fd = open("myfile.txt", O_WRONLY | O_CREAT, 0600);
if(fd == -1)
{
printf("Failed to create and open the file. \n");
exit(1);
}
write(fd, "Hello World!\n", 13);
close(fd);
//read
fd = open("myfile.txt", O_RDONLY);
if(fd == -1)
{
printf("Failed to open and read the file. \n");
exit(1);
}
read(fd, buf, 13);
buf[13] = '\0';
close(fd);
printf("buf : %s\n", buf);
return 0;
}
The terminal shows output "Failed to create and open file". So I think I am using the open() wrong or maybe it has to do with my Ubuntu version?
Can somebody see what I am doing wrong?
I tried changing the order of the flags and tried to change the mode to 0777 and 0700 with no success.
There was a Permission Denied error. For some reason the "myfile.txt" was locked.
chmod u=rwx,g=r,o=r myfile.txt command worked for me. Thanks everyone for the quick help.
The only visible differences I can spot are from the this line:
fd = open("myfile", O_WRONLY | O_CREAT, 0600);
You didn’t include the ‘.txt’ on the end of the file name in this instance.
The video has this line listed as:
fd = open("myfile.txt”, O_CREAT | O_WRONLY, 0600);
The O_CREAT and O_WRONLY were the wrong way around, though I don’t know if this would change anything.
Edit: Thank you kind replier, been informed the order does not matter.
Hope this helps!
My terminal shows the different outut :
gcc t.c -Wall -Wextra
t.c: In function ‘main’:
t.c:11:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
11 | int main(int argc, char *argv[])
| ~~~~^~~~
t.c:11:26: warning: unused parameter ‘argv’ [-Wunused-parameter]
11 | int main(int argc, char *argv[])
| ~~~~~~^~~~~~
a#zalman:~/Dokumenty/t/t1$ ./a.out
buf : Hello World!
For me the program works. So maybe your compilation failed ?
This question already has answers here:
C - Executing Bash Commands with Execvp
(3 answers)
Closed 6 years ago.
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void main(){
char *cmd;
pid_t pid;
while (1) {
printf("$ ");
fgets(cmd,1000,stdin);
if (pid = fork() == -1) {
exit(1);
}
else if (pid == 0){
execvp(cmd,&cmd);
}
else{
int status;
wait(&status);
}
}
}
I am making a simple shell that executes commands but when I enter the command at the prompt I keep getting segmentation fault. This is the most simple version that only works for one-argument commands like "ls"
For your problem, in your code,
fgets(cmd,1000,stdin);
cmd is uninitialized. It does not point to a valid memory. Accessing invalid memory invokes undefined behavior.
You need to allocate memory to cmd before you ca use that. Alternatively, you can consider making cmd an array, like char cmd[1000] = {0}; to avoid the need to allocate memory yourself.
Then, execvp(cmd,&cmd); is not quite right, it's not what you think it is. Read the man page for a better understanding.
That said, for a hosted environment, void main() should at least be int main(void) to have standard conformance.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am running the follwong c program related with fork() system call using the cygwin terminal in the windows OS! and i get the following error! how can i rectify this?
./fork.cpp: line 3: syntax error near unexpected token `('
'/fork.cpp: line 3: `int main(int argcc,char *argv[])
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
# include<conio.h>
int main(int argcc,char *argv[])
{
printf("I m %d\n",(int) getpid());
pid_t pid = fork();
printf("Fork returned: %d\n", (int) pid);
printf("I am %d\n, (int) getpid());
getch();
}
You should state the line at which it gives you the error but I am assuming it is this statement : printf("I am %d\n, (int) getpid()); since you have not put the closing quotes in the printf statement ". The unexpected token `(' is the ( just before the word int since the compiler is looking for the closing quotes and can not find them.
This question already has answers here:
printf anomaly after "fork()"
(3 answers)
Closed 7 years ago.
I was writing a multi-process program using fork() and i bumped into a problem.
Below is a sample code reproducing the problem (without any sort of error checking):
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
printf("hello world");
fork();
}
This code prints 2 "hello world" statements (one from parent and the other from child). However this should not be the case since the printffunction call is prior to the fork() system call.
After testing, the problem appears to be solved by the following:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
printf("hello world\n"); \\ addition of the new line character
// or by using fflush(stdout);
fork();
}
My guessing is that the printf buffer is being copied while its not flushed, so the child process is emptying its copy of this buffer before exiting. Hence the other printf shows.
Can anyone provide a better explanation of this issue? Or even better, correct me if i am wrong or missing something.
The file handle stdout (which is used by printf) is by default line buffered, which means output using printf will be flushed (and shown in the console) either when there's a newline or when the buffer is full.
As fork creates an exact duplicate of the parent process, both processes have the same contents in the (un-flushed) output buffer, and both will be flushed when the two processes exits.
So yes you're correct in your guessing.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to write code that opens an external file, and prints out the lines in that file, but keep getting
Line 7: warning: incompatible implicit declaration of built-in function 'exit'
Line 15: error: expected expression before '%' token
Line 15: error: stray '\' in program
When trying to compile this code:
#include <stdio.h>
#include <assert.h>
main(int argc, char *argv[]){
if (argc != 2){
fprintf(stderr, "usage: strgen <file>\n");
exit(1);
}
char *infile = argv[1];
FILE *fp = fopen(infile, "r");
assert(fp != NULL);
char buffer[50];
while( fgets( buffer, 50, fp) != NULL){
printf("%d\n",buffer);
printf(%s\n, buffer);
}
fclose(fp);
return(0);
}
Two problems:
You need to #include <stdlib.h> to get the declaration of exit()
You need quotes around the %s\n in the second printf() statement
you need to include header file
#include <stdlib.h>
It would help if you formatted your code correctly. There's a stray printf statement in there without any quotes around the %s\n, hence the error.