1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
/**
* util_file_example - Example program for util_file
*
* Copyright IBM Corp. 2016, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
//! [code]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lib/util_file.h"
/*
* Write buffer to file and read it back again
*/
int main(void)
{
char buf_wr[4096], buf_rd[4096];
unsigned long long value_ull;
long value_l;
/* Generate input */
sprintf(buf_wr, "Say something interesting!\nSecond line\n");
printf("Write.....:\n%s", buf_wr);
/* Write string to file */
if (util_file_write_s(buf_wr, "/tmp/%s", "testfile")) {
perror("util_file_write_s failed\n");
return EXIT_FAILURE;
}
/* Read back first line of file */
if (util_file_read_line(buf_rd, sizeof(buf_rd), "/tmp/%s", "testfile")) {
perror("util_file_read_line failed\n");
return EXIT_FAILURE;
}
printf("Read......: %s\n", buf_rd);
/* Write long to file */
printf("Write.....: %ld\n", 4711L);
if (util_file_write_l(4711L, 10, "/tmp/%s", "testfile")) {
perror("util_file_write failed\n");
return EXIT_FAILURE;
}
/* Read back long from file */
if (util_file_read_l(&value_l, 10, "/tmp/%s", "testfile")) {
perror("util_file_read_l failed\n");
return EXIT_FAILURE;
}
printf("Read......: %ld\n", value_l);
/* Write long long hexadecimal to file */
printf("Write.....: 0x%llx\n", 0x4712ULL);
if (util_file_write_ull(0x4712ULL, 16, "/tmp/%s", "testfile")) {
perror("util_file_write failed\n");
return EXIT_FAILURE;
}
/* Read back long long hexadecimal from file */
if (util_file_read_ull(&value_ull, 16, "/tmp/%s", "testfile")) {
perror("util_file_read_ull failed\n");
return EXIT_FAILURE;
}
printf("Read......: 0x%llx\n", value_ull);
/* Remove file */
unlink("/tmp/testfile");
return EXIT_SUCCESS;
}
//! [code]
|