I'm trying to write a generic function that will write both an uncompressed and compressed file (depending on user input). According to zlib, you just have to set the gzopen mode to "w0" (no compression), but I still get the ZLIB header!
In the ZLIB manual it mentions it is possible to write raw data (no header/trailer) but it doesn't say how. How can I write a plain (raw encoded) file with zlib?
thanks,
Open the file using the transparent mode "T":
#include <zlib.h>
int main()
{
gzFile file = gzopen("/tmp/a.dat", "wT");
(void) gzwrite(file, "test", 4);
(void) gzclose(file);
}
Related
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);
}
}
I am currently learning C, and am having some issues with trying to make a small program that utilizes zlib.
I have managed to compile my application (using Codeblocks/MinGW) with the zlib libraries, and compilation works fine. I have used an example based upon the zpipe.c example found over at the official zlib site (zlib.net).
On execution, the output zip file is created, but it seems malformed and/or empty. I am unable to open it using 7zip.
Here is the code that I have modified. I have simply replaced the main() function within zpipe.c.
int main() {
printf("Compression test...");
int ret;
FILE *fpsource;
FILE *fpdest;
fpsource = fopen("test.txt", "rb");
fpdest = fopen("output.zip", "wb");
ret = def(fpsource, fpdest, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK) {
printf("failure\n");
zerr(ret);
}
else {
printf("success..\n");
}
fclose(fpsource);
fclose(fpdest);
return EXIT_SUCCESS;
}
I receive no errors, and my 'success' message is printed. It's just the output file is corrupt.
zpipe.c as-is will generate the zlib format, which is raw deflate data wrapped in a zlib header and trailer. 7zip won't recognize that. It will recognize the gzip or zip format, which are entirely different wrappers on the same raw deflate data.
You can modify zpipe.c to use deflateInit2 (and inflateInit2) instead of the versions without the "2" to select the gzip format instead of the zlib format. You can read zlib.h for how to do this.
The code discussed simply compresses the file using the DEFLATE algorithm. The appropriate structures that make it a zip or gzip file are missing.
Is it possible to use fprintf in such a way that write data to a compressed file?
For example:
fopen ("myfile.txt","w");
will write to a plain text file. So the file size grows very large.
You can use zlib to write data to a compressed stream.
gzFile fp;
fp = gzopen(NAME, "wb");
gzprintf(fp, "Hello, %s!\n", "world");
gzclose(fp);
Compile it like this:
gcc -Wall -Wextra -o zprog zprog.c -lz
Use zcat to print the contents of the file.
The minimally-invasive solution if you're on a system that has pipes would be to open a pipe to an external gzip process. That way you can use all the normal stdio output functions without having to replace everything with zlib calls.
On Linux, you could use the zlib library (and link it as -lz) and use its compressed streams
I want to write audio data to stdout, preferably using libsndfile. When I output WAV to /dev/stdout I manage to write the header, but then I get an error
Error : could not open file : /dev/stdout
System error : Illegal seek.
I assume this is related to http://www.mega-nerd.com/libsndfile/FAQ.html#Q017, some file formats cannot be written without seeks. However, when I try to output SF_FORMAT_AU | SF_FORMAT_PCM_16 instead, I still get the same Illegal seek error.
Are there any audio file formats that can be written completely without seeking?
I'm using Linux.
EDIT: It might be obvious, but RAW format works (without seeking). Unfortunately I need a format that has meta information like sample rate.
You should finish reading that FAQ... the link you give us has all the answers.
However, there is at least one file format (AU) which is specifically designed to be written to a pipe.
So use AU instead of WAV.
Also make sure that you open the SNDFILE object with sf_open_fd, and not sf_open_virtual (or sf_open):
SNDFILE* sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
SNDFILE* sf_open_virtual (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo,
void *user_data) ;
If you use sf_open_fd, then libsndfile will use fstat to determine whether the file descriptor is a pipe or a regular file. If you use sf_open_virtual or sf_open, it will assume that the file is seekable. This appears to be a flaw in libsndfile, but you should be using sf_open_fd anyway.
Footnote: Don't open /dev/stdout to get standard output; it is already open and there is no need to open it again. Use file descriptor STDOUT_FILENO.
Ended outputting an "infinite" wav header, and then writing raw PCM data for as long as the audio lasts. Not really valid, but most players seem to understand anyway.
The wav header is here, in case anyone wants it: https://gist.github.com/1428176
You could write to a temp file (perhaps in /tmp), let the libsnd seek to modify the .wav(RIFF) header of the temp file, and then, after libsnd has closed the file, stream the temp file out to stdout.
I want to truncate the file something like setsizeof() with FILE *
I'm developing vs 2003 windows
#include <unistd.h> there's no such lib
how can I do it freopen() truncates all the data vut doesn't write- getting EINVAL error
some help?????
_chsize function does the job better it get fd ans size to change to
There are a number (roughly 20) of header files in the C standard and unistd is not one of them (it's a POSIX/UNIX95/UNIX98 header). So there's no requirement for a vendor to provide it. Neither C89 nor C99 have unistd as one the the mandated header files.
The easiest way to truncate a file is to reopen it in write mode (assuming you have the file name).
fclose (fh);
fh = fopen ("file_name", "w");
If all you have is the file handle, you need to use freopen(). You will only get EINVAL if the mode is incorrect. You cannot change the mode except accoording to the following table:
r -> r
w a -> a w
r+ w+ a+ -> any mode
See man freopen for further details.