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.
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 4 months ago.
Improve this question
I want to create a ext4 filesystem in C
For example, my C code currently runs
system("mkfs.ext4 /dev/sdc1")
The problem is using system is not recommended and we don't want to have mkfs.ext4 utility in the root file system.
I saw mke2fs.c file in e2fsprog package, Do we need to copy directly the code understanding the implementation or is there any better way of using some library
That command mkfs.ext4 formats a filesystem on a partition, it does not create a partition. If you need to format a filesystem, the best way to do it is to run the mkfs.ext4 tool. It's not a good idea to copy code from e2fsprogs into your own program.
Why don't you want to have the utility there in the filesystem? If you need to use a certain utility, it should be there.
The system call is potentially unsafe because it passes a string to the shell, and it's difficult to safely escape dynamic arguments to avoid the possibility of shell attacks. If you are using a fixed device or a limited set of devices, and not accepting user input for the device, it should be okay to use system.
If you want to avoid using system, you can do something similar with fork and exec. This does not use the shell, so it is safer. But the code is more complex. I included a safer reusable "systemv" function.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
char *mkfs_ext4_prog = "/sbin/mkfs.ext4";
int systemv(const char *pathname, char *const argv[])
{
int wstatus;
pid_t pid;
pid = fork();
if (pid == -1) {
perror("fork failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
execv(pathname, argv);
exit(EXIT_FAILURE);
}
if (waitpid(pid, &wstatus, 0) == -1) {
perror("waitpid");
}
if (WIFEXITED(wstatus)) {
return WEXITSTATUS(wstatus);
}
if (WIFSIGNALED(wstatus)) {
fprintf(stderr, "%s killed by signal %d\n", mkfs_ext4_prog, WTERMSIG(wstatus));
}
exit(EXIT_FAILURE);
}
int mkfs_ext4(char *device)
{
char *const argv[] = { mkfs_ext4_prog, "--", device, NULL };
return systemv(mkfs_ext4_prog, argv);
}
int main(void)
{
int status;
char *device;
device = "/dev/sdc1";
status = mkfs_ext4(device);
exit(status);
}
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
Ok, so i've been having a probelm with using int and char for my function. And is getting an error message enter image description here want to know how i should fix this with the code that i have:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char name[100];
int roll_no, chars;
float marks;
fp = fopen("records.txt", "r");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fscanf() function: \n\n");
printf("Name:\t\tRoll\t\tMarks\n");
while( fscanf(fp, "Name: %s\t\tRoll no: %d\t\tMarks: %f\n"
, name, &roll_no, &marks) != EOF )
{
printf("%s\t\t%d\t\t%.2f\n", name, roll_no ,marks);
}
fclose(fp);
return 0;
}
The intedend output was this... enter image description here
Advice/help on how to use the char function at line 8 would be appericated
NOTE: This answer refers to revision 1 of the question. Meanwhile, OP has clarified the question and is asking for additional help concerning a related issue. That is why this answer does not fully address the question in its current state.
That's not an error message, that is simply a warning because you declare the variable 'chars' on line 8 and never use it in your program. Your program should be able to run even if there are compiler warnings, which are different from compiler errors.
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;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to print Malayalam (a south Indian Language) as c/c++ program output but it shows some unfamiliar characters both in terminal and in user interface using WINAPI.
(The file "malayalam.txt" contain some Malayalam words.)
#include <stdio.h>
#include <windows.h>
main() {
char s[100];
FILE *fp;
fp = fopen("malayalam.txt", "r");
if (fp == NULL) {
puts("Cannot open file");
}
while (fgets(s, 100, fp) != NULL) {
printf("%s", s);
MessageBox(NULL, s, "Malayalam", MB_OK);
}
fclose(fp);
}
The example from the following link may help you fix this issue for WINAPI.
You need to find the unicode equivalent of your Malayalam word in the .txt file you can convert it from here http://www.aksharangal.com
An example from the following page http://harikrishnanvs.blogspot.in/2011/12/printing-malayalam-as-c-program-output.html
WIN32 program to print my name in Malayalam -MessageBox
This works for windows 7, but not working in XP
Create new project in visual studio 2010.
File-->New-->Project-->Win32 Project
Name the project
click OK
Finish
include header files stdafx.h, tchar.h.
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCommandline,int iCmdshow)
{
TCHAR c[4];
c[0]=3385;
c[1]=3376;
c[2]=3391;
c[3]='\0';
TCHAR szbuffer[100];
_stprintf(szbuffer,_T("%ls"),c);
MessageBox(NULL,szbuffer,TEXT("HELLO ALL"),0);
return 0;
}
Please ensure that , Configuration Properties--->Character set---> Use Unicode Character Set option is selected.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Doing regex in C# or PHP is very easy for me now. However currently I have a need to use regex in C. And, I don't seem to understand the usage of regcomp or regexec fully. It's definitely because of my lack of experience in C.
Use the PCRE library. Examples are included in the source, in the demo/ directory. Here's a direct link to pcredemo.c.
This may get you started, as you indicate regex(3) functions. Following is a trivial program matching its arguments. However, if you're relatively new to C, you'll want to go slowly with regex(3), as you'll be working with pointers and arrays and regmatch_t-supplied offsets and lions and tigers and bears. ;)
$ ./regexec '[[:digit:]]' 56789 alpha " " foo12bar
matched: 56789
matched: foo12bar
$ ./regexec '[[:digit:]](foo'
error: Unmatched ( or \(
$ ./regexec '['
error: Invalid regular expression
... and the source:
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
int main(int argc, char **argv) {
int r;
regex_t reg;
++argv; /* Danger! */
if (r = regcomp(®, *argv, REG_NOSUB|REG_EXTENDED)) {
char errbuf[1024];
regerror(r, ®, errbuf, sizeof(errbuf));
printf("error: %s\n", errbuf);
return 1;
}
for (++argv; *argv; ++argv) {
if (regexec(®, *argv, 0, NULL, 0) == REG_NOMATCH)
continue;
printf("matched: %s\n", *argv);
}
return 0;
}
You need a library that provides it, and there are several to choose from. PCRE is one.
There's also libslack(str) - string module:
http://libslack.org/manpages/str.3.html
The gnu C library has a regex library