Construct the path of rays in a prism. Stepanova V.A. elements of geometric optics

The fopen() function opens a stream for use, associates a file with that stream, and then returns a FILE pointer to that stream. Most often the file is treated as a disk file. The fopen() function has the following prototype:

FILE *fopen(const char *filename, const char *mode);

Where mode points to a string containing the desired mode for opening the file. Valid values ​​for mode in Borland C++ are shown in the table. filename must be a character string providing a valid filename operating system, and may contain a path indication.

The fopen() function returns a pointer to the base type FILE. This pointer identifies the file and is used by most functions. file system. You should never change it yourself. The function returns a null pointer if the file cannot be opened.

As the table shows, the file can be opened in either text or binary mode. In text mode, as you type, the sequence of carriage returns and line feeds is translated into a newline character. In output, the opposite is true: the newline character is translated into a carriage return and a line feed. This translation does not occur in binary files. When neither t nor b is specified in the mode argument, then the file's text/binary status is determined by the value of the global variable _fmode defined in Borland C++. By default, fmode is set to O_TEXT, that is, text mode is set. If you set _fmode to O_BINARY, then files will be opened in binary mode. (These macros are defined in fcntl.h.) Naturally, using an explicit t or b eliminates the effects associated with the _fmode variable. In addition, _fmode is specific to Borland products. It is not defined in the ANSI C I/O system.

If you need to open a file named test for writing, you should write:

Fp = fopen("test", "w");

Where fp is type variable FILE *. However, it is common to see the following:

If((fp = fopen("test", "w"))==NULL) (
puts("Cannot open file.");
exit(1);
}

This method allows you to detect errors when opening a file, for example, the presence of write protection or lack of free space on disk.

If fopen() is used to open a file for writing, then any pre-existing file with the specified name will be deleted. If a file with the specified name does not exist, it will be created.

If you need to append information to the end of the file, you should use mode a (append). If the file does not exist, it will be created.

Opening a file for reading requires the presence of the file. If the file does not exist, an error will be returned. If the file is opened for read/write operation, then it is not deleted if it exists, and if the file does not exist, then it is created.

Table: Valid values modes

Meaning

Opens a file for reading. (Opens by default as a text file.)

Creates a file to write to. (Opens by default as a text file.)

Attaches to a file. (Opens by default as a text file.)

Opens a binary file for reading.

Opens a binary file for writing.

Attaches to a binary file.

Opens a file for reading/writing. (Opens by default as a text file.)

Creates a read/write file. (Opens by default as a text file.)

Attaches or creates a read/write file. (Opens by default as a text file.)

Opens a binary file for reading/writing.

Creates a read/write binary file.

Attaches or creates a read/write binary file.

Creates a text file for writing.

Attaches to a text file.

Opens a text file for reading.

Creates a text file for reading/writing.

Opens or creates a text file for reading/writing.

For the programmer open file is represented as a sequence of data being read or written. When a file is opened, it is associated with I/O stream . Output information is written to the stream, input information is read from the stream.

When a stream is opened for I/O, it is associated with a standard FILE structure, which is defined in stdio.h. The FILE structure contains necessary information about the file.

Opening a file is done using the fopen() function, which returns a pointer to a structure of type FILE that can be used for subsequent operations on the file.

FILE *fopen(name, type);

name – name of the file to open (including path),
type - pointer to a string of characters defining how the file is accessed:

· "r" - open the file for reading (the file must exist);

· "w" - open an empty file for writing; if the file exists, its contents are lost;

· "a" - open the file for writing to the end (for appending); the file is created if it does not exist;

· "r+" - open the file for reading and writing (the file must exist);

· "w+" - open an empty file for reading and writing; if the file exists, its contents are lost;

· "a+" - open the file for reading and appending; if the file does not exist, it is created.

The return value is a pointer to the open stream. If an error is detected, NULL is returned.

The fclose() function closes the stream or streams associated with files opened using the fopen() function. The stream to be closed is determined by the argument of the fclose() function.

Return value: value 0 if the stream was closed successfully; constant EOF if an error occurred.

#include
int main()

char name="my.txt";

if(fp = fopen(name, "r")!=NULL)

// was it possible to open the file?
... // required actions on data

else printf("Failed to open file");

Reading a character from a file:

char fgetc(stream);

The function argument is a pointer to a stream of type FILE. The function returns the code of the read character. If the end of the file is reached or an error occurs, the constant EOF is returned.
Writing a symbol to a file:

fputc(char, stream);

The function's arguments are a character and a pointer to a stream of type FILE. The function returns the code of the read character.

The fscanf() and fprintf() functions are similar to the scanf() and printf() functions, but work with data files, and have a pointer to the file as their first argument.

fscanf(stream, "Input Format", arguments);
fprintf(stream, "Output Format", arguments);

The fgets() and fputs() functions are designed for string input/output; they are analogous to the gets() and puts() functions for working with files.

fgets(Pointer To Line, Number Of Characters, stream);

Characters are read from the stream until a newline character "\n" is read, which is included in the string, or until the stream ends EOF or the maximum number of characters has been read. The result is placed in a string pointer and ends with the null character "\0". The function returns the address of the string.

fputs(Pointer To String, stream);

Copies a string to the stream from the current position. The terminating null character is not copied.
Example Enter the number and save it in the s1.txt file. Read the number from the file s1.txt, increase it by 3 and save it in the file s2.txt.

Text files

Let's look at working with a text file in C using an example. Create a text file on drive C named TextFile.txt. Type the following lines in this file:

String_1 123 String_11, 456
String_2
String_3

Save the file.

And this is the code for a C program that opens our file and reads lines from it:

/* *Author: @author Subbotin B.P..h> #include #define LEN 50 int main(void) ( puts("Text file operations"); char cArray; FILE *pTextFile = fopen("C:\\TextFile.txt", "r"); if(pTextFile == NULL) ( puts("Problems"); return EXIT_FAILURE; ) while(fgets(cArray, LEN, pTextFile) != NULL) ( printf("%s", cArray); ) fclose(pTextFile); return EXIT_SUCCESS;

To open a text file in C, use the fopen function:

FILE *pTextFile = fopen("C:\\TextFile.txt", "r");

The first argument to the fopen function points to a file, and the second says that the file is open for reading from it.

We read the lines using the fgets function:

fgets(cArray, LEN, pTextFile);

the first argument of the fgets function points to a character array in which the received strings will be stored, the second argument is maximum quantity characters to read, the third is our file.

After finishing working with the file, you need to close it:

fclose(pTextFile);

We get:

Russian letters also appear in the lines.

By the way, I made this program in Eclipse. You can see how to work with C/C++ in Eclipse.

So, we opened and read data from a text file.

Now we will learn how to programmatically create a text file and write data to it.

/* Author: @author Subbotin B.P..h> #include int main(void) ( FILE *pTextFile = fopen("C:\\TextFileW.txt", "w"); char *cString = "This is a string"; char cNewLine = "\n"; int nVal = 123 ; if(pTextFile == NULL) ( puts("Problems"); return EXIT_FAILURE; ) fprintf(pTextFile, "%s%c", cString, cNewLine); ; )

Create a text file to write data to:

FILE *pTextFile = fopen("C:\\TextFileW.txt", "w");

if the file already exists, it will be opened and all data from it will be deleted.

The C-string cString and the number nVal are written by the program to a text file. cNewLine is simply a new line.

We write data to a text file using the fprintf function:

fprintf(pTextFile, "%s%c", cString, cNewLine);

the first argument here is our file, the second is the format string, the third or more is the number of arguments required for this format.



Did you like the article? Share with your friends!