Использование fopen()
и fputs()
функций.
Полный пример (с чрезмерными комментариями):
#include <stdio.h>
int main(void)
{
/* where to write */
const char filepath[] =
"/var/streaming/playlists/chunkcombined/chunkcombined.playlist";
/* what to write */
const char output_lines[] =
"\"/usr/local/movies//chunk0.mp4\" 1\n"
"\"/usr/local/movies//chunk1.mp4\" 1\n"
"\"/usr/local/movies//chunk2.mp4\" 1\n"
"\"/usr/local/movies//chunk3.mp4\" 5\n"
"\"/usr/local/movies//chunk4.mp4\" 5\n";
/* define file handle */
FILE *output;
/* open the file */
output = fopen(filepath, "wb");
if(output == NULL) return -1; /* fopen failed */
/* write the lines */
fputs(output_lines, output);
/* close the file */
fclose(output);
return 0;
}
Эта версияизвлекает текстовую строку, заданную в качестве аргумента программы, и затем записывает ее в нужный файл:
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argv[1] == NULL) return -1; /* no arguments, bail out */
/* where to write */
const char filepath[] =
"/var/streaming/playlists/chunkcombined/chunkcombined.playlist";
/* define file handle */
FILE *output;
/* open the file */
output = fopen(filepath, "wb"); /* change "wb" to "ab" for append mode */
if(output == NULL) return -1; /* fopen failed */
/* write the lines */
fputs(argv[1], output);
putc('\n', output);
/* close the file */
fclose(output);
return 0;
}
Пример:
./write "\"Hello, World!\""
пишет: "Hello, World!"
до:
/var/streaming/playlists/chunkcombined/chunkcombined.playlist
.