What is a simple translation in code for 'textscan' from matlab into C or C++? I am using Ubuntu and I am trying to translate a Matlab code into C++. Thank you very much.
2 Answers
First, the answer is not the same if you're using C or if you're using C++. These are different programming languages.
Matlab is a much higher-level language than C and C++. In Matlab textscan
reads from files or strings. C and C++ have different mechanisms for that.
To read from a file :
In C, you should use the FILE
object and its associated functions (fopen, fgets ...) from the header file : stdio.h
.
In C++, you should use std::ifstream
from the <fstream>
header file. For formatted input use the >>
operator.
To read from a string :
In C, you might want to look at the functions in the string.h
header.
In C++, the better way is to use the std::istringstream
class from the sstream
header file.

- 3,838
- 23
- 41
-
Can you elaborate on reading file using ifstream and >>? How does ">>" work like matlab's textscan? – js0823 Mar 10 '11 at 17:07
It is fscanf
. You will need to #include <stdio.h>
and open FILE
objects with fopen
to use it.

- 9,451
- 2
- 33
- 59