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 7827311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:48:26+00:00 2026-06-02T09:48:26+00:00

I would like to translate the following C code to C++. FILE *fp =

  • 0

I would like to translate the following C code to C++.

FILE *fp = NULL;
fp = fopen("./filename", "r");
int i = 0;
fscanf(fp, "%d\n", &i);
uint16_t j = (uint16_t) i;

This is what I came up with this:

  ifstream file;
  string filename = "./filename";

  file.open(filename.c_str(), ios::in);
  errno = 0;
  if (file.fail()) {
      int tmp = errno;
      std::cout << file.c_str () << " not found: strerror(" << tmp << "): " << strerror(tmp) );
  }
  int i = 0;
  file >> i >> std::endl;       
  uint16_t j = (uint16_t) i;

I would like to know whether the syntax is correct or improvable and more importantly whether it’s safe against all kinds of inputs.

  • 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-02T09:48:33+00:00Added an answer on June 2, 2026 at 9:48 am
    int read_int(const std::string file_name) {
        std::ifstream file(file_name); //the file will close itself on destruction
        std::uint16_t i;
        //extract type, don't worry about what it is it will either compile or not
        if(!(file >> i)) { //Catch failure
             //or however you wish to deal with it.
             throw std::runtime_error("can't read file");
        }
        return i;
    }
    
    int main() {
        try{
            std::uint16_t i=read_int("./filepath");
            //do with i...
        }
        catch(const std::exception& e) {
             std::cerr << e.what() << std::endl;
             return EXIT_FAILURE;
        }
        return EXIT_SUCCESS;
    }
    

    Note if you do not have C++11 then you will need to use c_str() to open the file, but the string method is prefered.

    EDIT: fstream close themselves, there is no need to close it yourself, the functionality is there incase you do have to do that however it is far better to rely on RAII semantics:

    http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

    RAII dictates that you should open the file on construction and it will close on destructions, this ensures that there isn’t any invalid (exclude EOF, file not found…) fstream object preventing bugs. RAII is a fundamental construct in C++ and should be used where ever resources are concerned.

    The docs for the fstream destructors is here:

    http://en.cppreference.com/w/cpp/io/basic_fstream

    destructs the basic_fstream and the associated buffer, closes the file

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to translate the following code to codebehind instead
I've got the following MySQL query that I would like to translate so it
I've got some code which I would like to translate into Opengl ES. I'm
I would like to translate this MySQL query to the Zend format: SELECT count_panne,
I would like to translate or 301-redirect urls such as: www.domain.com/example.html to www.domain.com/example Here
Would like to know the c# code to actually retrieve the IP type: Static
I have the following string that I would like to Huffman-encode and store efficiently
I'm a PHP guy, and in PHP I would do something like the following:
I would like to know how one achieves the following signature. I have read
Would like to get a list of advantages and disadvantages of using Stored Procedures.

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.