Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:32:08+00:00 2026-06-10T07:32:08+00:00

Currently I am trying to read a UTF-16 encoded CSV file char by char,

  • 0

Currently I am trying to read a UTF-16 encoded CSV file char by char, and convert each char into ascii so I can process it. I later plan to change my processed data back to UTF-16 but that is besides the point right now.

I know right off the bat I am doing this completely wrong, as I have never attempted anything like this before:

int main(void)
{
    FILE *fp;
    int ch;
    if(!(fp = fopen("x.csv", "r"))) return 1;
    while(ch != EOF)
    {
        ch = fgetc(fp);
                ch = (wchar_t) ch;
                ch = (char) ch;
        printf("%c", ch);
    }
    fclose(fp);
    return 0;
}

Wishfully thinking, I was hoping that that work by magic for some reason but that was not the case. How can I read a UTF-16 CSV file and convert it to ascii? My guess is since each utf-16 char is two bytes (i think?) I’m going to have to read two bytes at a time from the file into a variable of some datatype which I am not sure of. Then I guess I will have to check the bits of this variable to make sure it is valid ascii and convert it from there? I don’t know how I would do this though and any help would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:32:10+00:00Added an answer on June 10, 2026 at 7:32 am

    You should use fgetwc. The below code should work in the presence of a byte-order mark, and an available locale named en_US.UTF-16.

    #include <stdio.h>
    #include <wchar.h>
    #include <locale.h>
    
    main() {
      setlocale(LC_ALL, "en_US.UTF-16"); 
    
      FILE *fp = fopen("x.csv", "rb");
      if (fp) {
        int order = fgetc(fp) == 0xFE;
        order = fgetc(fp) == 0xFF;
    
        wint_t ch;
        while ((ch = fgetwc(fp)) != WEOF) {
          putchar(order ? ch >> 8 : ch);
        }
        putchar('\n');
    
        fclose(fp);
        return 0;
      } else {
        perror("opening x.csv");
        return 1;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to read the contents of an XML file into a Map
I am trying to read from csv file. The file contains UTF-8 characters. So
Im currently trying to read an XML file and populate an observable collection, but
I am currently trying to read in names from an input file. The file
I am trying to have PHP read an XML file and then convert it
I'm trying to read an xml file into memory, add a node, then write
I'm currently trying to read in an XML file, make some minor changes (alter
I'm trying to read a file which is encoded in ISO-8859(ansi), and it contains
I’m trying to read and parse a UTF-8 Excel file using the PHPExcelReader but
Currently am trying to read and write mp3 tags without using any external modules

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.