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

  • SEARCH
  • Home
  • 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 8323453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:43:45+00:00 2026-06-08T23:43:45+00:00

I am trying to read a char from a file being passed in via

  • 0

I am trying to read a char from a file being passed in via the stdinput in ocaml. I was wondering why I keep getting a unit when I can print_char the char that I receive in the following code. Also, I am trying to make a list of these characters which will not work as of now.

let () =
let charList = [] in 
let inchar = open_in Sys.argv.(1) in
    try 
            while true do
                let c = Char.uppercase(input_char inchar) in
                    print_char c; print_int (List.length charList);c::charList
            done
    with End_of_file -> close_in inchar;(*; print_int (List.length charList); printTest charList;*)

;;

  • 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-08T23:43:47+00:00Added an answer on June 8, 2026 at 11:43 pm

    Your code does not modify charList, so charList remains an empty list throughout the whole execution.

    Moreover, if you define
    let charList = [] then charList is immutable.

    You might try something along these lines:

    let () =
        let charList = ref [] in
        let inchar = open_in Sys.argv.(1) in
      try
        while true do
          let c = Char.uppercase (input_char inchar) in
                charList := c :: !charList
        done
      with End_of_file ->
            charList := List.rev !charList;
            close_in inchar;
            print_int (List.length !charList);
            List.iter print_char !charList
    

    Some more comments:

    • reading input char by char is very inefficient, use for instance input_line.
    • your post mentions Sys.argv.(1) and the standard input, but those are two distinct things
    • if you want to read a whole file and store it in memory, search for “ocaml input file”, there are many examples on the net, like https://groups.google.com/forum/?fromgroups#!msg/fa.caml/EEinP7-yAko/dPSaR0eOkw4J or http://camltastic.blogspot.fr/2008/09/tip-read-all-lines-from-file-most.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read in from a file, and I can't get the
I'm trying to read a specific line from a file and I can get
I am trying to read a file and store the information in unsigned char
I am getting Bus Error trying to read stdin into a char* variable. I
I'm trying to read from a file, but the only thing I get on
I am trying to read from a file using fgets and sscanf. In my
We are trying to read each word from a text file and replace it
I am trying to read all content from a text file. Here is the
I'm trying to read from a text file using the read function and store
I'm trying to read a list of numbers from a file and sort them

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.