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.
Related
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 6 years ago.
Improve this question
How,and what libs I need to create and manipulate a FILE in the Desktop? using only C, not C# or C++.
I'm using Codeblocks, I using allegro, what means I can´t use windows.h lib, The program need to work in windows.
In Windows Vista or higher, use SHGetKnownFolderPath to find he path for desktop. You have to use Unicode functions to get the file path, use _wfopen_s. You can write ANSI to the file, but it is recommended to write UTF16 or to convert text to UTF8.
#include <stdio.h>
#include <Windows.h>
#include <Shlobj.h>
int main()
{
wchar_t *desktop;
if(S_OK == SHGetKnownFolderPath(&FOLDERID_Desktop, 0, NULL, &desktop))
{
wprintf(L"Desktop path: %s\n", desktop);
wchar_t filename[MAX_PATH];
swprintf_s(filename, MAX_PATH, L"%s\\%s", desktop, L"file.txt");
wprintf(L"Filename path: %s\n", filename);
FILE *fp;
_wfopen_s(&fp, filename, L"w");
if(fp)
{
fprintf(fp, "Hello world\n");
fclose(fp);
}
else
{
wprintf(L"can't create file\n");
}
CoTaskMemFree(desktop);
}
return 0;
}
In Windows XP use SHGetSpecialFolderPath (deprecated)
#include <stdio.h>
#include <Windows.h>
#include <Shlobj.h>
int main()
{
wchar_t desktop[MAX_PATH];
if(SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_DESKTOP | CSIDL_FLAG_CREATE, NULL, 0, desktop)))
{
wprintf(L"desktop: %s\n", desktop);
wchar_t path[MAX_PATH];
swprintf(path, L"%s\\%s", desktop, L"filename.txt");
HANDLE handle = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(handle != INVALID_HANDLE_VALUE)
{
DWORD temp;
const char *buf = "hello world";
WriteFile(handle, buf, strlen(buf), &temp, NULL);
CloseHandle(handle);
}
else
{
printf("can't create file\n");
}
}
return 0;
}
Given that you are on Windows, you'd want to look at CreateFile and its associated functions.
The lifecycle of a file is as follows: You can create it (or, if it exists, open it) using CreateFile, and once it is created/opened, you can use the resulting file handle to manipulate it with ReadFile, WriteFile, and potentially DeleteFile. Once you're done, you need to close the handle with CloseHandle.
1 If you are using Unix or Unix-like system, you can call
system("touch ~/Desktop/FILE");
in your code, and you need to #include <stdlib.h>.
This calls the command "touch ~/Desktop/FILE". If you want to handle (read/write) the file after this, you have to call fopen with correct mode. But the 2nd method is much better if you want to write something into the file.
2 You can also use
For Unix or Unix-like operating systems:
FILE *file = fopen("~/Desktop/FILE", "w+")
For Windows:
FILE *file = fopen("C:\\Users\\YOUR_USER_NAME\\Desktop\\FILE", "w+")
(*Assumes you are using the default user folder location.)
This creates the file, and then you can read/write to the file after this. Be sure to used the correct mode. Here is a reference for fopen and modes. Remember to #include <stdlib.h>.
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 7 years ago.
Improve this question
I was asked to solve a programming challenge, and there is this line which I don't understand can some one explain to me how can I pass the test cases to the program using this command, I think I have to store it in some file but I am not sure
size_t getline(char **lineptr,size_t *n, FILE *stream);
here is the full code
#include <stdio.h>
#include <stdlib.h>
size_t getline(char **lineptr,size_t *n, FILE *stream);
int main()
{
size_t maxLineLen=1024;
char *line = (char*)malloc(maxLineLen);
while(getline(&line, &maxLineLen,stdin)!= -1){
printf("Hello, World!\n");
printf("%s\n",line);
}
}
Seems you are asking how to run the given code and get input into.
getline(&line, &maxLineLen,stdin)
That reads a line from stdin. stdin is a standard file stream and is opened by the startup code for you. Without redirection, reading from stdin will get the input typed into the terminal
So to get input into the program you can do one of the following:
Run the program and then type each input line into the terminal.
Run the program and then redirect a file into the program. Example:
./my_program < my_input.txt
Are you given a file name?
Then the FILE * parameter would have to be opened via fopen.
See manual for fopen
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
For example, I run my program like:
program.exe < text.txt
I want the program to read from file text.txt. How do I begin doing this?
Ok, as this is a textfile, you probably want to read it line by line, so
char buf[1024];
while (fgets(buf, 1024, stdin))
{
/* do whatever you need with the line that's in buf here */
}
Note your code doesn't know about the file, it just reads from standard input. With <, you tell your environment (CMD on windows, a shell like e.g. bash on *nix) to open that file for you and provide it to your program as the standard input instead of the default, the controlling terminal, which would normally just read from the keyboard.
Hint: 1024 is kind of a random pick, most text files don't have lines exceeding 1kb. You might want to modify it to better suit your expected input.
another way to do what you are looking for is
#include <stdio.h>
int main (void) {
int c;
while ((c = fgetc(stdin)) != EOF) fputc(c, stdout);
return 0;
}
some more help here
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.
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
So I have this bit of C code below. When I place printf statements to test the text from the input file, I see that I'm getting a bunch of junk values, to be more specific they are not even alphabetic or numerical, I think they are diamonds with question marks in them. I assume this means it is not processing these values the way it should be. The input file a bit of MIPS assembly code, but in this context it is only a text file. I have commented out all other parts of my program and am left with this small piece and yet I still receive the bad values. What could I possibly be doing wrong here?
The command I use to run the program on the console is:
./assembler -symbols adder.asm
Where ./assembler is the driver (argv[0])
-symbols is a tag used (argv[1])
adder.asm is the input file (argv[2])
So once opened I should be able to grab text out of this file, and it's not a problem with the file as far as I believe, it was working earlier.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
FILE *fp;
FILE *fp_out;
void main(int argc, char* argv[])
{
int mode;
if (strcmp(argv[1], "-symbols") == 0)
{
fp = fopen(argv[2], "r");
mode = 1;
}
else
{
fp = fopen(argv[1], "r");
fp_out = fopen(argv[2], "w");
mode = 2;
}
}
Try to add the following line right after the open section and add #include <errno.h> to the beginning.
printf("%p, %p, %d\n", fp, ftp_out, errno);
If the fp is null then there is some problem opening the file. If you do not check the return value, you can read from a wrong buffer. Maybe there is some permission problems (or whatever). Also if errno != 0 you have a problem. Check with perror <num> the errno value in command line (or see perror(3) function).