Redirect the standard output [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Hello and thank you for attention. I am writing my own shell in c and i have problem with redirect the standard output. For example, I get command: ls -l >> file and I need to display output to file. Can you give me some ideas to resolve that problem? Thanks.

You may want to use dup() & dup2(), here are two functions I have ready:
void stdout_redirect(int fd, int *stdout_bak)
{
if (fd <= 0)
return;
*stdout_bak = dup(fileno(stdout));
dup2(fd, fileno(stdout));
close(fd);
}
void stdout_restore(int stdout_bak)
{
if (stdout_bak <= 0)
return;
dup2(stdout_bak, fileno(stdout));
close(stdout_bak);
}
Here is how to use it:
int main(void)
{
int stdout_bak;
FILE *fd;
fd = fopen("tmp", "w");
stdout_redirect(fileno(fd), &stdout_bak);
/* Your Stuff Here */
stdout_restore(&stdout_bak);
return 0;
}

Related

my true or false if else statement isn't working [closed]

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 2 years ago.
Improve this question
I am coding something and this should print "Good Job" but instead it is just saying "invalid lol,".
Here is my code:
#include <stdio.h>
#include <string.h>
fgets(line, 69, stdin);
if (strcmp(line, "stats") == 0)
{
printf("Good Job\n");
}
else
{
printf("Invalidlol\n");
{
I suspect the newline is getting added to your string. Try
if (strcmp(line, "stats\n") == 0)
For a quick fix

How to read file in C programming using codeanywhere? [closed]

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;
}

Include OCR Api in a C project [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
Hi all i'm at computer science (bd),for my exam project i want to make a c ocr program (no gui),i search in the internet for tesseract but i don't find any api for c but only for c++,anyone knows a ocr api for c language?
Thanks in advance
This is an example of using Tesseract C API, taken from the official documentation:
#include <stdio.h>
#include <allheaders.h>
#include <capi.h>
void die(const char *errstr) {
fputs(errstr, stderr);
exit(1);
}
int main(int argc, char *argv[]) {
TessBaseAPI *handle;
PIX *img;
char *text;
if((img = pixRead("img.png")) == NULL)
die("Error reading image\n");
handle = TessBaseAPICreate();
if(TessBaseAPIInit3(handle, NULL, "eng") != 0)
die("Error initialising tesseract\n");
TessBaseAPISetImage2(handle, img);
if(TessBaseAPIRecognize(handle, NULL) != 0)
die("Error in Tesseract recognition\n");
if((text = TessBaseAPIGetUTF8Text(handle)) == NULL)
die("Error getting text\n");
fputs(text, stdout);
TessDeleteText(text);
TessBaseAPIEnd(handle);
TessBaseAPIDelete(handle);
pixDestroy(&img);
return 0;
}
If you are using Linux, you can compile it as you would compile a program using C++ API.

How to write from all the file from child to parent using time? [closed]

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 9 years ago.
Improve this question
I have written the following code in c, the parent read what he get from child using pipe, and write it to a new file, and the child should wait between each 50 characters he read but the problem is that parent only write one line to the file, I know there is something missing but unable to recognize it?
if(fork())
{
//Parent
read(fd[0],str,sizeof(str));
fprintf(fpnew,str,"w+");
}
else
{
//Child 1
while(fgets(str,n,fp)!=NULL)
{
write(fd[1],str,sizeof(str));
sleep(1+rand()%10);
}
}
You need to put a loop in the parent to read the child output until it is finished. fprintf is the wrong thing to use here. If you want to stick to the std C lib then you probably want fwrite here. But rather than mix and match the stdlib functions with the lower level read I tend to like to be consistent. It's just one less thing to think about. You want something more like this. (Also, read up on the parameters of the functions you are using and check return code.)
int fpnew = open("whatever", O_WRONLY);
if (pfnew < 0)
error....
int bytesread;
while ((bytesread = read(fd[0], str, sizeof(str))) != 0)
write(fpnew, str, bytesread);
close(pfnew);

Save data to file before program will close not after [closed]

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
if(RS_ile==1)
{
if(RS_buf == 1) break;
if(RS_buf==49)
printf("1\n");
else
{
printf("%d\n", RS_buf);
fprintf (fp, "%d\n", RS_buf);
fclose (fp);
}
}
Everything work fine but all data was saved after I close my program. All I need is to put date to file while program is running not after closed.
Operating system Windows 8.1
Put a fflush(fp) ; after your fprintf.

Resources