Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to read from a fifo and i am not understanding it's behaviour.
This is the write side, write.c:
for(int i = 1;i<argc;i++){
if(write(fifoFd,argv[i],strlen(argv[i])) <= 0)
perror("Error writing");
}
And this is the read side, read.c:
char buf[1024];
while(1){
int b = read(fifoFd,buf,1024);
if(b<=0) break;
}
printf("%s\n",buf);
First i start read.c ./read then i execute ./write
If i execute write like this ./write backup *.txt sometimes i get what i expected, i.e, backupexample1.txtexample2.txt.
But sometimes i only get example1.txtexample2.txt and i am not understanding this, where is "backup"?
Your code:
while(1){
int b = read(fifoFd,buf,1024);
if(b<=0) break;
}
printf("%s\n",buf);
You loop, each time through the loop you overwrite the buffer, and then you print the buffer. So, sometimes, you read "backup" followed by "example1.txtexample2.txt" (which overwrites "backup"), other times you read the whole lot at once in a single read.
If you change the loop to read into the unpopulated portion of the buffer, it will behave consistently:
int read = 0;
while(read != 1024){
int b = read(fifoFd,buf+read,1024-read);
if(b<=0) break;
read += b;
}
printf("%s\n",buf);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I don't know why, but my code is not taking input.... where did i do mistake???
After running it just prints this:
Type your input (press enter to save and exit).🙂️
Done, your file is saved successfully🤩️
My code is:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fpp;
char Entry;
char sid;
fpp = fopen("sid","w");
if (fpp == NULL)
{
printf("Sorry🙁️ file not created\n");
exit(0);
}
printf("Type your input (press enter to save and exit).🙂️\n");
while (1)
{
putc(Entry,fpp);
if(Entry =='\n')
break;
}
printf("Done, your file is saved succesfully🤩️\n");
fclose(fpp);
return 0;
}
yes, guys I used scanf() instead of putc(). My online tutor said me to write this...
while((ch=getchar())!='\n')
{
putc(Entry,fpp);
}
and I used that.... but now I used this code and it worked.
while (1)
{
scanf("%c",&Entry);
if(Entry =='\n')
break;
}
In order to get this out of the list of unanswered questions I compile an answer from comments.
Credits to the commenters: MikeCAT, 0x5453, Vlad, Daniel Farrell.
Other commenters provided wise input, which I however do not see as immeditate parts of the solution.
Your question is not very specific about what puzzles you about the shown output so here is my guess, along with explanation:
Why does any input you provide not have any influence on program behavior?
Because you do never take any input. It seems that you intend to read input with putc(Entry,fpp);, but as 0x5453 comments
putc is used for output, not effective input.
I.e. with no input reading functions called there is no input.
Why does Done, your file is saved successfully🤩️ occur at all? It should only occur after '\n' input.
Because you have an unconditional break; in your endless loop.
That might be non-obvious, but as Daniel Farrell commetns:
... the semicolon between if(...) and break essentially makes the if a no-op... Thus break is executed the first time the loop runs.
I.e. unconditional immediate break, end of loop, output. End of program.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would like to merge two files using open and file descriptor. Moreover I would like to separate the content of the first file with - before writing the the content of the second file.
I did the following :
void merge (char* fileName, char *fileName1) {
int fd = open(fileName, O_RDWR);
char c;
while (read(fd, c, 1) > 0) {//going at the end of the first file
}
char next[] = "\n";
char charc[] = "-";
write (fd, next, strlen(next));
for (int i = 0; i < 80; i++) {
if (write (fd, charc, strlen(charc)) == -1) {
perror("error : ");
}
}
write (fd, next, strlen(next));
int fd1 = open(fileName1, O_RDWR);
while(read(fd1, &c, 1) > 0) {
write(fd, &c, sizeof(c));
}
close(fd1);
close(fd);
}
Is there a better way to write this code ? Moreover I have a little problem even if it works it seems like I don't have the right to read the new file. For example if I do cat newFile I have a permission denied.
Is there a better way to write this code ?
You are not handling errors of all calls. All of syscalls open, write, read and close return -1 on error and set errno and may do that at any time. EINTR could be handled.
going at the end of the first file open has O_APPEND flag mode that is used for appending data.
Copying one character at a time is very not optimal. With glibc standard library you could use BUFSIZ bytes at a time that is chosen for fast I/O output. You could make a copy of a big chunk size at a time that is a power of 2, like 2048 or 4096.
There is little reason to use file descriptors here - prefer to use standard FILE * handling, which would make your code portable and also buffer the data for faster I/O.
If you wish to create the file use O_CREAT and add the third argument to open that is the mask of permissions of new file.
On linux there is splice(2) system call that can be used to append data on kernel side for maximum efficiency.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am doing a project in my class about file and i/o system calls. I have dumbed down the code below for the sake of my question. I have read the manual multiple times. I thought I understood read(), but clearly I'm missing something.
int a, bytesRead;
char buffer[150];
a = open("/home/JohnDoe/Dowloads/TestFile.dat", 0);
if (a < 0) {
printf("Error opening file TestFile.dat\n");
return 0;
}
bytesRead = read(a, buffer, 150);
printf("Bytes read: %d\n", bytesRead);
I'm lost to as why this read() call returns -1. If I understand the manual correctly, read() takes arguments file id, buffer and number of bytes to read. The integer, a (file id), was returned without error, I created my buffer and reading 150 bytes should be no problem because the file contains well over 150 bytes. Any help would be greatly appreciated.
Check errno. If read returns -1, the errno will be set and you can see, where the error happened.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
here is the code given to me and a have also a text file where i need to get the Text from and compile the program.
You need to use open a file with fopen() first. But the current user needs to have perms to read/write the file.
We will use r to only read from a file. If the file is not read it will return NULL. You can fscanf() function to get the value of a file. The second parameter represents the type of the variable as in this case it's a string(char), third param is the mem address of the variable itself. Kind of like file version of scanf().
int main()
{
char a[1000];
FILE *myFile;
if ((myFile = fopen("C:\\myUSER\\newprogram.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(myFile ,"%s", &a);
printf("Value of a=%s", a);
fclose(myFile);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can we read datatype char* from the user in a pipe between two processes?
Here is my code:
#include<stdio.h>
#include<unistd.h>
#define MSGSIZE 120
int main (
{
char *msg ;
char inbuff[MSGSIZE];
int p[2];
int ret;
pipe(p);
ret = fork();
if(ret != 0){
close(p[0]);
printf("Please Enter the msg\n");
scanf("%s",msg);
printf("I am the parent sending a msg %s\n",msg);
write(p[1],msg,MSGSIZE);
}
else
{
sleep(10);
close(p[1]);
read(p[0],inbuff,MSGSIZE);
printf("\nI am the child with msg %s",inbuff);
}
return 0;
}
My problem is that whenever the compiler gets into scanf() I am getting an error segmentation fault
You need to define msg as follow:
char msg[MSGSIZE];
You are calling scanf(). You want to tell it to put the entered data somewhere. But you don't give it such a space.
Instead, you give it a pointer, which is uninitialized and therefore points to an unknown location. That leads to segfault.
You could use the inbuf instead.
But you should refrain from using scanf() in this way. If the user enters more data than your buffer can hold, weird things happen.
Instead, use fscanf().