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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:54:09+00:00 2026-06-15T06:54:09+00:00

My main question is about how you read data from a file that is

  • 0

My main question is about how you read data from a file that is not of the char data type.
I am writing a file of data from MATLAB as follows:

x=rand(1,60000);
fID=fopen('Data.txt','w');
fwrite(fID,x,'float');
fclose(fID);

Then when I try to read it in C++ using the following code “num” doesn’t change.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream fin("Data.txt",ios::in | ios::binary);
    if (!fin)
    {   
        cout<<"\n Couldn't find file \n";
        return 0;
    }
    float num=123;
    float loopSize=100e3;
    for(int i=0; i<loopSize; i++)
    {
        if(fin.eof())
        break;

        fin >> num;
        cout<< num;
    }
    fin.close();
    return 0;
}

I can read and write file in matlab fine, and I can read and write in c++, but I can’t write in matlab and read in c++. The files I write in matlab are in the format I want, but the files in c++ seem to be writing/reading the numbers out at text. How do you read a series of floats in from a file in C++, or what am I doing wrong?

edit: The loop code is messy because I didn’t want an infinite loop and the eof flag was never being set.

  • 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-15T06:54:10+00:00Added an answer on June 15, 2026 at 6:54 am

    Formatted I/O using << and >> does indeed read and write numeric values as text.

    Presumably, Matlab is writing the floating-point values in a binary format. If it uses the same format as C++ (most implementations of which use the standard IEEE binary format), then you could read the bytes using unformatted input, and reinterpret them as a floating-point value, along the lines of:

    float f;  // Might need to be "double", depending on format
    fin.read(reinterpret_cast<char*>(&f), sizeof f);
    

    If Matlab does not use a compatible format, then you’ll need to find out what format it does use and write some code to convert it.

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

Sidebar

Related Questions

I have a question about the 'Event Dispatch Thread'. I have a Main class
So I just have posted a question about this code (which was answered): $(document).ready(Main);
I am trying to read in with C# a file written with CArchive. From
OK, so I've read several question and articles about this topic, and I feel
I was wondering how i should go about writing an XML data layer for
I'm making my first API that gets data and parses it from websites. So
Main question is what are the implications of allowing the this keyword to be
My main question is given a feature centroid, how can I draw it in
My main question is : Is there a good practice to serve binary files
I guess my main question is, will this always work as long as I

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.