How to use .mat file in C++ Visual Studio [duplicate] - c++

This question already has answers here:
Using mat file in C++ program
(3 answers)
Closed 8 years ago.
I am doing one of my project on Pattern Recognition, Earlier I was training data with .mat file in Matlab, but now I need to use the same in C++.

MATLAB makes available a MAT-file API that can be used to read and write MAT files from C/C++ (or Fortran).

Although there are some external libraries that allow c++ to read .mat files, most of them are not well supported, and can load files of much earlier versions.
However, I would suggest you just save your variables in plain text format
save('output.txt', 'variable_name', '-ascii');
This is simple, since you work with numbers, and safe (any other language you use will be forever able to read text files, in constrast to relying on libraries that may not work next year). A file containing numbers has a natural interpretation, and can be viewed and manipulated by any program (excel, text editor, etc...)
If you are doing pattern recognition, you should probably keep your dataset in one file, with one pattern per line, eg:
a1 a2 a3 a4 ... 1 0 0
where a_i is the value of the i-th attribute, and 1 0 0 is the class of the pattern. The weights of the neural network/ svm parameters/whatever else you are using, should be saved to a different, also text file. It will give you much less problems than a .mat file, the contents of which you cannot view and use in any other way other than loading it on matlab.

Related

Quick way to output a picture in C++

I'm coding a physical simulation on 2d array and I'm now thinking that I could benefit from having a graphical output. My system is an array of cells (up to 2048*2048 of them) taking binary values, until now I used a prompt or text file output of '+' and '-' but it's not efficient for 2048*2048 lattice and maybe outputting in an image would be quicker and neater. Still, I've never done that. Ideally a library allowing me to write blue and red pixels/cell while parsing my lattice would get the job done. Are there some pre-existing not too long tools for doing it in c++?
Edit: I think that I just found what I was looking for: png++
After no more than 10 lines of coding I got the following output:
All I was asking for! Thank you for the suggestions ;)
You can easily get away without using an external imaging library by outputting a very simple format such as PGM or PBM. Refer to the wikipedia page on Netbpm for more details, but you're essentially outputting all the values as either ASCII or binary numbers, then any image viewer or editor that supports PGM (many of which do) can open and display them. Even if you don't have an editor, something like ImageMagick can easily convert it to a PNG or any other more accessible format.
I've used this technique in the past to quickly visualize 2D data, as you're intending to.
C++ does not have native support for graphics. You need an additional C++ library.
Personally, I suggest you to use Qt, which is free, powerful and cross-platform.

How to read and write doc, pdf files using files in c++

I m writing a c++ program using files and i need to take the input from existing files such as doc files and pdf files. how to program it in c++? And after getting the inputs, how can i write those details into a new doc or pdf files? Can anyone explain me with an example?
C++ as a language doesn't equip you with such features as "write to DOC file" or "read from PDF file". The only staff available to you a a programmer is raw byte-by-byte reading or writing. To make your new brand file as PDF/DOC/etc compatible you have to conform the chosen file format. The same about reading - you should understand which portions of raw byte array are responsible for what.
In common, this task named as "parsing" or "serialization". And it's a good idea to use one of existing parsers for particular file format instead of reinventing the wheel. Moreover, some file formats can be patent-pending so you may be not allowed to deal with it without license purchase.
Some clues so far:
PDF parsing in C++ (PoDoFo)
Microsoft word Text Parser in "C"
There are some libraries available on the web now(the question is from 2013, maybe that time there weren't many).
Apart from the links in selected answer, you can try PDFTron. It also supports new features, eg. Linearization.
Here is one of their samples is ->
https://www.pdftron.com/documentation/samples/cpp/TextExtractTest
(That program itself contains 4 if blocks, with slightly different features of the library/SDK, to try)
There should be more, search on the web for PDF parsing libraries.

Game Programming: .DAT file?

I've seen a lot of games use something similar to a .DAT file or a specific file type that the game has for itself. I'm just beginning with C++ and DirectX and I was interested in keeping my information in something similar to a .DAT.
My initial conception was that it would hold information on the files you wanted to store within the .DAT file. Something similar to a .RAR file. Unfortunately, my googleing skills did not help me in finding the answers.
Right now I'm simply loading textures and sound files from a folder called Data.
EDIT: While I understand that .DAT is short for data, and I've found that a .DAT file generally contains any assortment of information, I'm still unsure about how to go about doing something as packing images and sound files into any type of file and being able to read them.
I'm not sure about using fstreams to achieve my task, however I will look into streams related to storing data and how to properly read from that data. Meanwhile if anyone has another answer to offer based on this new information, it would be appreciated.
EDIT: Thanks to the answers, I stumbled across a similar question on stackoverflow and felt I'd share it here. Combining resources into a single binary file
I don't think there is really such thing as .dat file format. It's short for "data," and different applications just put in some proprietary stuff in it and call it ".dat." You can read up on fstream classes to do file IO in C++. See Input/Output with files.
What you then do is make up your own file format. For example, first 4 byte is int that indicates the number of blocks in the .dat and for each block, you have 4 byte indicating the length of each block, 4 byte indicating the type of the block, the variable length data itself .. something like that.
DAT obviously stands for data, and there is no real or de facto standard on what that extension actually refers to. Your decisions on the best file formats should be based on technical considerations, not pointless attempts at security through obscurity.
Professional games use a technique where they put all the needed resources (models, textures, sounds, ai, config, etc) zipped/packed into a single file thus making it faster to manage, harder to change (some even make use of a virtual filing system from what's inside the data file). Now, for what's inside the file is different depending on the needs of the game and the data structures that you use.
If you're just starting into gamedev, i recommend you stick with keeping all you assets separate and don't bother too much about packing them into a single file.
Now if you really want to start using a packed format here's a good pointer:
Creating a PAK File Format
Here's a link which claims that .dat is a movie format, 'DAT' being short for Digital Audio Tape.
I'm not sure I believe the link, but I do remember something about a Microsoft supported format called DAT, from long ago, when I used an earlier version of Windows.
It makes more sense as a logical extension for a DATA file of some kind.
.dat, as others have said, is literally just a data file. In reality, the file extension means nothing other than association with a program. For example, I could make a word processor that saves all the documents with the .mp3 file extension. These files wouldn't be playable in any media software, but the software might try. File extensions are used to help programs know what types of files they can and cannot open--however those rules don't have to be followed.
Anyway, you can dump any sort of information to a file. Programmers/software writers will often choose .dat as the extension of that file because it has become the standard to signify 'this file just holds a ton of data' and that the data doesn't necessarily hold any standardized headers, footers, or formatting.
A dat file could really contain anything. It might be as simple as a zip archive with the extension changed, or it could be a completely custom file type. If you're just starting out, you probably don't want to write your own file format, although doing so can be fun and educational. If you want to encapsulate your data files into some kind of container, you should probably go with a zip, paq, or maybe tar.gz.

What's the easiest way to read .dbf file from gfortran

I'm using gfortran, I need to write a function that reads records from a .dbf file associated with an ESRI Shapefile. The file I should be able to read is available from internet http://diss.rm.ingv.it/diss/DISS_3.0.4.shp.zip
The opinion of the file command about the format of the file is:
$ file GGSources_polyline.dbf
GGSources_polyline.dbf: \012- DBase 3 data file\012- (119 records)
Thanks for your suggestions
I found a rough description of the file format here. It looks like there is quite a mix of variable types and sizes throughout, which is going to complicate things somewhat. I don't know if using Fortran to try and read this data is the best option, but if you must here are some hints:
Open the file for direct access unformatted I/O. Unformatted means that you can just read the bytes straight out of the file, and direct access won't add any padding to records.
Set the record length as the lowest common length between fields
Use the transfer() function to interpret a location in memory as a particular type. This will allow you to read the binary data from the file into a variable of type integer but then assign to a real without doing a type cast.
I'm in a similar situation now trying to read a file with a structure very similar to the dBase file (i.e. varying sizes of headers pointing to regions of the file with different types) and ended up using Python and Numpy to read the file. Reading consists of seeking to a location in the file, reading a bunch of bytes, then using the numpy.fromstring option to convert that into real*4, real*8, integer*8, etc. You can make this work, but you may want to keep your options open.
Your best bet is to conver the dbf file into something else, using e.g. the OGR tools, available in most linux distributions. You can just convert the contents of the dbf file into a CSV file using ogr2ogr:
ogr2ogr -f "CSV" output.csv FaultScarps_polyline.shp FaultScarps_polyline
(note that you need to include the layername, which for Shapefiles, is identical to the shapefile's name). The first 3 lines of the CSV look like this:
IDSOURCE,IDSCARP,SOURCENAME,FAULTSCARP,LENGHT,HEIGHT,AVGVOFFSET,MAXVOFFSET,VOFFSETTYP,AVGHOFFSET,MAXHOFFSET,HOFFSETTYP,AGE,NOEVENTS,LENGHTQ,HEIGHTQ,VOFFSETQ,HOFFSETQ,AGEQ,NOEVENTSQ,LENGHTN,HEIGHTN,VOFFSETN,HOFFSETN,AGEN,NOEVENTSN,REFERENCE
ITGG001, 1,Ovindoli-Pezza,Ovindoli-Pezza Fault Piano Pezza, 4.40, 18.00, 9.750, 16.000, 1, 0.000, 0.000,3, 10.000000000000000,3,1,0,1,1,1,1,Based on topographic observations.,Max height in late Pleistocene-Holocene fluvioglacial deposits.,Based on geological survey and refers to late Pleistocene-Holocene deposits.,Based on geological survey.,Based on geological observations.,Refers to Holocene and based on paleoseismology.,Pantosti et al. [1996].
ITGG001, 2,Ovindoli-Pezza,Ovindoli-Pezza Fault Campo Porcaro, 8.60, 0.00, 8.700, 12.000, 1, 3.045, 4.025,1, 18.000000000000000,3,1,0,1,1,1,1,Based on topographic observations.,,Max offset observed in the late Pleistocene-Holocene fluvioglacial and moraine deposits.,"Calculated as 35 % of the vertical component, on the basis of literature data.",Based on geological observations.,Refers to Holocene and based on paleoseismology.,Pantosti et al. [1996]
An alternative would be to access the Shapefile using OGR (or Shapelib) and doing the processing in C, returning it to the main Fortran program.
You may struggle reading binary unformatted files in Fortran which were not written from a Fortran write statement unless your compiler has some extensions.
Fortran binary unformatted files have beginning of record and end of record marks. These marks are usually the length of the record in bytes.
So the runtime system will try to interpret characters in the file as record marks and get confused.
Converting to csv ascii and reading that from Fortran will work. If you were going to try reading other file types then writing some C functions to interface to the C I/O library should allow you to read the files directly.
The FortranGIS package has Fortran bindings to the shapelib library, allowing to encode/decode shapefiles and the associated dbf files directly from a Fortran program:
http://fortrangis.berlios.de/ later moved to https://github.com/ARPA-SIMC/fortrangis
It works with gfortran 4.1.2 or later (F2003 ISO_C_BINDING module).

Should the text in a C++ text based game be in the code or in external files? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am creating a text based game using C++ for a school project, the game works by allowing the user to pick a choice from a list of options in each scene; similar to how the games hosted by Choice of Games work. As a result of this I have a large amount of text that must be displayed in my game, however I am unsure as to the proper conventions when working with large amounts text in a program. Should I simply make use of std::cout and write the text directly into the code, or should I write into text files an used std::ifstream in order to read the text.
My only major concern regarding the use of files to hold the text is that each choice the user makes results in a different paragraph being displayed and as a result I believe that I would need to create a text file for each paragraph, which seems like it will lead to more issues (such as using the wrong file name or mistyping my code leading to the game reading from the wrong file) than writing the text straight into the code could. If there is a way to read particular sections of a text file then this would be useful to know, however I am currently unaware of any such method. However I am new to C++ and I am certain that there is plenty that I have yet to learn so I would not be surprised if such a method did exist.
Any help is greatly appreciated, be it anything from simply telling me if I should enter text into my code or into files, to telling me if there is a way to read text from specific sections of a text file. And once again, I am very grateful for any help you can provide.
Please don't put displayed text into code. That's an antipattern. You have to recompile your game for every minor text change like fixing typos, and for major changes like translating into other languages.
Convention for most programming languages is to put all the displayed text into (a few) resource files or properties files as key-value pairs, where the code only references the key of the paragraph to be displayed and the value will be loaded from that external file. (Usually once during startup.) No need to use one file per paragraph, but the kv pairs have to be parsed. There'll be utilities for you to reuse.
I recommend using external files. It makes changing the content much easier and doesn't require recompiling the entire program for a simple typo.
You can use one file and just separate each paragraph with a blank line. Grabbing "all text between blank lines" at that point is trivial.
If the choices cause the paragraph choices to jump around the file you can give them IDs and load them on-the-fly by searching linearly through the file for a given ID.
--EDIT--
As per the request here is an algorithm or two:
Algorithm 1:
Give each paragraph an ID, usually a simple number on the line immediately above the paragraph.
Separate each number-paragraph pair by blank lines.
Parse the file line-by-line looking for a "line" that contains only a number.
From that point you found the paragraph you are looking for, all lines until the next blank is the content of that paragraph.
Display to user.
Algorithm 2 (recommended):
Use XML to store your paragraphs and their IDs.
Use TinyXML2 to parse the file: http://www.grinninglizard.com/tinyxml2/index.html
If you do not plan to translate you game to other languages, you are on your own, both approaches have their pros and cons:
text in source: easy to write, text is near the place where it is used.
text in resource files: easier to remove duplicate strings, forces a better structure of text data.
If you simply imagine that your application could be translated, then you should put all text in ressource files. You can even find framework that will assist your for translations as Gnu gettext, but you can find others, for example qt has its own translation tools.
Storing text in the program files is not a good coding practice. This would result in unnecessary code bloat (it's not even code) and the need to recompile if you need to change the text.
A simple solution would be to create a text file with careful formatting like line numbers or whitespace that would allow you to pull out the desired text.
A more elegant solution would be to put the necessary text in xml or json files, and read them into your program when necessary. This would be a great choice.