/* nag_file_open (x04acc) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* NAG C Library
*
* Mark 30.3, 2024.
*
*/
#include <nag.h>
#include <stdio.h>
#include <string.h>
int main(void) {
Integer mode, exit_status = 0;
Nag_FileID fileid;
const char *filename;
char line[100], line2[100];
NagError fail;
INIT_FAIL(fail);
printf("nag_file_open (x04acc) Example Program Results\n");
printf("\n");
filename = "x04acc_example.txt";
printf("Attempting to write to and read from file %s:\n", filename);
/* Open the file for writing */
mode = 1;
/* nag_file_open (x04acc).
* Open unit number for reading, writing or appending, and
* associate unit with named file
*/
nag_file_open(filename, mode, &fileid, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_open (x04acc) %s\n", fail.message);
exit_status = 1;
goto END;
}
/* Write a line to the file we opened */
sprintf(line, "%s", "NAG nag_file_open (x04acc) example program");
/* nag_file_line_write (x04bac).
* Write formatted record to external file
*/
nag_file_line_write(fileid, line);
/* Close the file */
/* nag_file_close (x04adc).
* Close file associated with given unit number
*/
nag_file_close(fileid, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_close (x04adc) %s\n", fail.message);
exit_status = 1;
goto END;
}
/* Open the file for reading */
mode = 0;
/* nag_file_open (x04acc), see above. */
nag_file_open(filename, mode, &fileid, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_open (x04acc) %s\n", fail.message);
exit_status = 1;
goto END;
}
/* Read the line back from the file */
/* nag_file_line_read (x04bbc).
* Read formatted record from external file
*/
nag_file_line_read(fileid, line2, 100, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_line_read (x04bbc) %s\n", fail.message);
exit_status = 1;
goto END;
}
if (!strcmp(line, line2))
printf("\n - correctly wrote line:\n \"%s\"\n"
" to file and read it back.\n",
line);
else {
printf("\n - failed to write line:\n \"%s\"\n"
" to file and read it back.\n",
line);
exit_status = 1;
}
END:
return exit_status;
}