I am using Matlab to generate a large matrix, and I want to use it in C.
How can I read Matlab's .mat file in C?
If it is possible, please answer how?
Supposedly Matlab provides its own API to access such files from C:
Read and write MAT files. I haven't used it myself, so I don't know how straightforward this is.
If you really want to access the binary data, a search engine came up with this PDF file, matfile_format.pdf, which describes the entire format. This is definitely not an easy solution.
You can easily read such files in Python however, see this topic. Reading a file this way and writing it again in a format that's easy for you to use in C seems like a good solution.
matOpen (C)
C Syntax
#include "mat.h"
MATFile *matOpen(const char *filename, const char *mode);
filename
Name of file to open.
mode
File opening mode.
Here there are examples and explanations.
Here there are all the link for MAT-file API.
I recommend to read and study the examples.
If just text is enough...
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *pf;
pf = fopen("something.m", "r");
int buffer;
while (buffer != EOF)
{
buffer = getc(pf);
printf("%c", buffer);
}
}
Related
These terms may not be 100% accurate, but I'm using the GCC compiler and POSIX library. I have C code compiled with the SQLite amalgamation file to a single executable.
In the user interface that exchanges JSON messages with the C program, I'd like to make it possible for users to copy the SQLite database files they create through the C program, and copy a full directory/folder.
Thus far, I've been able to rename and move files and folders programmatically.
I've read many questions and answers here, at Microsoft's C runtime library, and other places but I must be missing the fundamental points. I'm using regular old C, not C++ or C#.
My question is are there POSIX functions similar to rename(), _mkdir(), rmdir(), remove(), _stat(), that allow for programmatic copying of files and folders in Windows and Linux?
If not, can one just make a new folder and/or file and fread/fwrite the bytes from the original file to the new file?
I am primarily concerned with copying SQLite database files, although I wouldn't mind knowing the answer in general also.
Is this answer an adequate method?
Is the system() function a poor method? It seems to work quite well. However, it took awhile to figure out how to stop the messages, such as "copied 2 files" from being sent to stdout and shutting down the requesting application since it's not well-formed JSON. This answer explains how and has a link to Microsoft "Using command redirection operators". A /q in xcopy may or may not be necessary also, but certainly didn't do the job alone.
Thank you very much for any direction you may be able to provide.
The question that someone suggested as an answer and placed the little submission box on this question is one that I had already linked to in my question. I don't mean to be rude but, if it had answered my question, I would not have written this one. Thank you whoever you are for taking the time to respond, I appreciate it.
I don't see how that would be a better option than using system() because with the right parameters all the sub-directories and files of a single parent folder can be copied in one statement without having to iterate through all of them manually. Is there any reason why it would not be better to use system() apart from the fact that code will need to be different for each OS?
Handling errors are a bit different because system() doesn't return an errno but an exit code; however, the errors can be redirected from stderr to a file and pulled from there, when necessary
rename(): posix
_mkdir(): not posix. You want mkdir which is. mkdir takes two arguments, the second of which should usually be 077.
rmdir(): posix
remove(): posix
_stat(): not posix, you want stat() which is.
_stat and _mkdir are called as such on the Windows C library because they're not quite compatible with the modern Unix calls. _mkdir is missing an argument, and _stat looks like a very old version of the Unix call. You'll have trouble on Windows with files larger than 2GB.
You could do:
#ifdef _WIN32
int mkdir(const char *path, int mode) { return _mkdir(path); } /* In the original C we could have #defined this but that doesn't work anymore */
#define stat _stat64
#endif
but if you do so, test it like crazy.
In the end, you're going to be copying stuff with stdio; this loop works. (beware the linked answer; it has bugs that'll bite ya.)
int copyfile(const char *src, const char *dst)
{
const int bufsz = 65536;
char *buf = malloc(bufsz);
if (!buf) return -1; /* like mkdir, rmdir, return 0 for success, -1 for failure */
FILE *hin = fopen(src, "rb");
if (!hin) { free(buf); return -1; }
FILE *hout = fopen(dst, "wb");
if (!hout) { free(buf); fclose(hin); return -1; }
size_t buflen;
while ((buflen = fread(buf, 1, bufsz)) > 0) {
if (buflen != fwrite(buf, 1, buflen)) {
fclose(hout);
fclose(hin);
free(buf);
return -1; /* IO error writing data */
}
}
free(buf);
int r = ferror(hin) ? -1 : 0; /* check if fread had indicated IO error on input */
fclose(hin);
return r | (fclose(hout) ? -1 : 0); /* final case: check if IO error flushing buffer -- don't omit this it really can happen; calling `fflush()` won't help. */
}
I'm reading the source code of a well-known open-source software in order to understand an algorithm, in one of the files (github link to the file) I read this:
ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) BLI_INLINE
bool BM_vert_in_edge(const BMEdge *e, const BMVert *v)
{
return (ELEM(v, e->v1, e->v2));
}
The file does not #include any other file or library, I don't understand how this file is able to use BMEdge, which is a structure declared in another file. I also don't understand what ATTR_WARN_UNUSED_RESULT , ATTR_NONNULL and BLI_INLINE are, where they are defined or what they do.
I'm learning about audio with openal and trying to load ogg files into memory so I can play them with openal. I have been searching for a library to load ogg files and the best one I found was this one, it has no dependences. But the documentation is messy and I can't find a decent tutorial-example online.
All I want to know is how to load an ogg and get something that I can actually send to a openal buffer.
Q: If I have this call:
stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output);
it should decode "filename" and store the data into "output". So I can send it to openal right?
It's not lack of research, after hours of reading I can't really get how it works. If there's another library to load ogg files easily then please let me know.
Thanks!
Have you tried it? What results/error did you get? Perhaps something like this:
int channels;
int sample_rate;
short * output;
int rc = stb_vorbis_decode_filename("somefile.ogg", &channels, &sample_rate, &output);
if (rc == -1) fprintf(stderr, "oops\n");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I delete a file pointed to by a FILE* in C?
I want to delete a file at the end of a C program, by which point the filename has been long forgotten. It would be nice if I could just use the FILE * to delete it directly or find the filename and then use remove()... rather than having to memorize the filename for this.
Does anybody know of any ways in which this could be achieved? I am on a Windows system, but I need to maintain portability so can't use any OS specific stuff.
nice if I could just use the FILE * to delete it directly or find the
filename and then use remove()... rather than having to memorize the
filename for this
There's no way to retrieve the file name from a FILE *. A FILE * isn't necessarily a real file; just think of popen(3) for example.
I do not know of a portable way, but there is a Linux version (which might work on other unices) and a Windows version:
Linux: a readlink() on sprintf("/proc/self/fd/%d",fd) should work
Windows: GetFileInformationByHandleEx() will give you the name as part of a struct (Search MSDN, don't have the details in my head)
E.g
#include <stdio.h>
#include <stdlib.h>
void endproc(void){
remove("removeFile.dat");
}
int main(){
atexit(endproc);
return 0;
}
I want to get the MIME type from a filename using C.
Is there a way to do this without using a textfile containing MIME types and file extensions (i.e. Apache's file mime.types)?
Maybe there is a function to get the MIME type using the filename? I rather not use the file extension if I don't have to.
I just implemented this for a project on which I'm working. libmagic is what you're looking for. On RHEL/CentOS its provided by file-libs and file-devel. Debian/Ubuntu appears to be libmagic-dev.
http://darwinsys.com/file/
Here's some example code:
#include <stdio.h>
#include <magic.h>
int main(int argc, char **argv){
const char *mime;
magic_t magic;
printf("Getting magic from %s\n", argv[1]);
magic = magic_open(MAGIC_MIME_TYPE);
magic_load(magic, NULL);
magic_compile(magic, NULL);
mime = magic_file(magic, argv[1]);
printf("%s\n", mime);
magic_close(magic);
return 0;
}
The code below uses the default magic database /usr/share/misc/magic. Once you get the dev packages installed, the libmagic man page is pretty helpful. I know this is an old question, but I found it on my hunt for the same answer. This was my preferred solution.
If there was a way to do it, Apache wouldn't need its mime.types file!
The table has to be somewhere. It's either in a separate file which is parsed by your code, or it's hard coded into your software. The former is clearer the better solution...
It's also possible to guess at the MIME type of a file by examining the content of the file, i.e. header fields, data structures, etc. This is the approach used by the file(1) program and also by Apache's mod_mime_magic. In both cases they still use a separate text file to store the lookup rules rather than have any details hard-coded in the program itself.
as far as I know, the unix command file outputs the mime string with the option -i:
> file -i main.c
main.c: text/x-c charset=us-ascii