Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read unicode (utf-8) / binary file line by line
    primarykey
    data
    text
    <p><strong>Hi programmers,</strong></p> <p>I want read line by line a Unicode (UTF-8) text file created by Notepad, i don't want display the Unicode string in the screen, i want just read and compare the strings!.</p> <p>This code read ANSI file line by line, and compare the strings</p> <h2>What i want</h2> <p>Read test_ansi.txt line by line</p> <p>if the line = "b" print "YES!"</p> <p>else print "NO!"</p> <h2>read_ansi_line_by_line.c</h2> <pre><code>#include &lt;stdio.h&gt; int main() { char *inname = "test_ansi.txt"; FILE *infile; char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */ char line_number; infile = fopen(inname, "r"); if (!infile) { printf("\nfile '%s' not found\n", inname); return 0; } printf("\n%s\n\n", inname); line_number = 0; while (fgets(line_buffer, sizeof(line_buffer), infile)) { ++line_number; /* note that the newline is in the buffer */ if (strcmp("b\n", line_buffer) == 0 ){ printf("%d: YES!\n", line_number); }else{ printf("%d: NO!\n", line_number,line_buffer); } } printf("\n\nTotal: %d\n", line_number); return 0; } </code></pre> <h2>test_ansi.txt</h2> <pre><code>a b c </code></pre> <h2>Compiling</h2> <pre><code>gcc -o read_ansi_line_by_line read_ansi_line_by_line.c </code></pre> <h2>Output</h2> <pre><code>test_ansi.txt 1: NO! 2: YES! 3: NO! Total: 3 </code></pre> <p>Now i need read Unicode (UTF-8) file created by Notepad, after more than 6 months i don't found any good code/library in C can read file coded in UTF-8!, i don't know exactly why but i think the standard C don't support Unicode!</p> <p>Reading Unicode binary file its OK!, but the probleme is the binary file most be already created in binary mode!, that mean if we want read a Unicode (UTF-8) file created by Notepad we need to translate it from UTF-8 file to BINARY file!</p> <p>This code write Unicode string to a binary file, NOTE the C file is coded in UTF-8 and compiled by GCC</p> <h2>What i want</h2> <p>Write the Unicode char "ب" to test_bin.dat</p> <h2>create_bin.c</h2> <pre><code>#define UNICODE #ifdef UNICODE #define _UNICODE #else #define _MBCS #endif #include &lt;stdio.h&gt; #include &lt;wchar.h&gt; int main() { /*Data to be stored in file*/ wchar_t line_buffer[BUFSIZ]=L"ب"; /*Opening file for writing in binary mode*/ FILE *infile=fopen("test_bin.dat","wb"); /*Writing data to file*/ fwrite(line_buffer, 1, 13, infile); /*Closing File*/ fclose(infile); return 0; } </code></pre> <h2>Compiling</h2> <pre><code>gcc -o create_bin create_bin.c </code></pre> <h2>Output</h2> <pre><code>create test_bin.dat </code></pre> <p>Now i want read the binary file line by line and compare!</p> <h2>What i want</h2> <p>Read test_bin.dat line by line if the line = "ب" print "YES!" else print "NO!"</p> <h2>read_bin_line_by_line.c</h2> <pre><code>#define UNICODE #ifdef UNICODE #define _UNICODE #else #define _MBCS #endif #include &lt;stdio.h&gt; #include &lt;wchar.h&gt; int main() { wchar_t *inname = L"test_bin.dat"; FILE *infile; wchar_t line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */ infile = _wfopen(inname,L"rb"); if (!infile) { wprintf(L"\nfile '%s' not found\n", inname); return 0; } wprintf(L"\n%s\n\n", inname); /*Reading data from file into temporary buffer*/ while (fread(line_buffer,1,13,infile)) { /* note that the newline is in the buffer */ if ( wcscmp ( L"ب" , line_buffer ) == 0 ){ wprintf(L"YES!\n"); }else{ wprintf(L"NO!\n", line_buffer); } } /*Closing File*/ fclose(infile); return 0; } </code></pre> <h2>Output</h2> <pre><code>test_bin.dat YES! </code></pre> <hr> <h2>THE PROBLEM</h2> <p>This method is VERY LONG! and NOT POWERFUL (i m beginner in software engineering)</p> <p>Please any one know how to read Unicode file ? (i know its not easy!) Please any one know how to convert Unicode file to Binary file ? (simple method) Please any one know how to read Unicode file in binary mode ? (i m not sure)</p> <p>Thank You. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload