Opening a file. What is the C file extension? Inclusion and extraction operators

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 the maximum number of characters to read, and 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); fprintf(pTextFile, "%d", nVal); return EXIT_SUCCESS ; )

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.

We hope that we have helped you solve the problem with the C file. If you do not know where you can download an application from our list, click on the link (this is the name of the program) - you will find more detailed information on where to download the secure installation version of the required application.

What else could cause problems?

There may be more reasons why you cannot open a C file (not just the lack of an appropriate application).
Firstly- the C file may be incorrectly linked (incompatible) with the installed application to serve it. In this case, you need to change this connection yourself. To do this, right-click on the C file that you want to edit, click the option "To open with" and then select the program you installed from the list. After this action, problems with opening the C file should completely disappear.
Secondly- the file you want to open may simply be damaged. In this case, it would be best to find a new version of it, or download it again from the same source (perhaps for some reason in the previous session the download of the C file did not finish and it could not be opened correctly).

Do you want to help?

If you have additional information about the C file extension, we will be grateful if you share it with users of our site. Use the form below and send us your information about the C file.

If you have installed on your computer antivirus program Can scan all files on your computer, as well as each file individually. You can scan any file by right-clicking on the file and selecting the appropriate option to scan the file for viruses.

For example, in this figure it is highlighted file my-file.c, then you need to right-click on this file and select the option in the file menu "scan with AVG". When you select this option, AVG Antivirus will open and scan the file for viruses.


Sometimes an error may occur as a result incorrect software installation, which may be due to a problem encountered during the installation process. This may interfere with your operating system link your C file to the correct application software, influencing the so-called "file extension associations".

Sometimes simple reinstalling UNIX Compact File Achive may solve your problem by linking C correctly with UNIX Compact File Achive. In other cases, problems with file associations may result from bad software programming developer and you may need to contact the developer for further assistance.


Advice: Try updating UNIX Compact File Achive to the latest version to ensure you have the latest patches and updates.


This may seem too obvious, but often the C file itself may be causing the problem. If you received a file via an email attachment or downloaded it from a website and the download process was interrupted (such as a power outage or other reason), the file may become damaged. If possible, try getting a new copy of the C file and try opening it again.


Carefully: A damaged file can cause collateral damage to previous or existing malware on your PC, so it is important to keep your computer up-to-date with an up-to-date antivirus.


If your file is C related to the hardware on your computer to open the file you may need update device drivers associated with this equipment.

This problem usually associated with media file types, which depend on successfully opening the hardware inside the computer, e.g. sound card or video card. For example, if you are trying to open an audio file but cannot open it, you may need to update sound card drivers.


Advice: If when you try to open a C file you get .SYS file error message, the problem could probably be associated with corrupted or outdated device drivers that need to be updated. This process can be made easier by using driver update software such as DriverDoc.


If the steps do not solve the problem and you are still having problems opening C files, this may be due to lack of available system resources. Some versions of C files may require a significant amount of resources (e.g. memory/RAM, processing power) to properly open on your computer. This problem is quite common if you are using fairly old computer hardware and at the same time a much newer operating system.

This problem can occur when the computer is having difficulty keeping up with a task because the operating system (and other services running in the background) may consume too many resources to open a C file. Try closing all applications on your PC before opening UNIX Compact File Achive. Freeing up all available resources on your computer will provide the best conditions for attempting to open the C file.


If you completed all the steps described above and your C file still won't open, you may need to run equipment update. In most cases, even when using older versions of hardware, the processing power can still be more than sufficient for most user applications (unless you're doing a lot of CPU-intensive work, such as 3D rendering, financial/scientific modeling, or intensive multimedia work) . Thus, it is likely that your computer does not have enough memory(commonly called "RAM" or random access memory) to perform the task of opening a file.

Files allow the user to read large amounts of data directly from the disk without having to enter it from the keyboard. There are two main types of files: text and binary.

Text files consisting of any characters are called. They are organized into lines, each of which ends with the character " end of line". The end of the file itself is indicated by the symbol " end of file". When writing information to a text file, which can be viewed using any text editor, all data is converted to a character type and stored in character form.

IN binary In files, information is read and written in the form of blocks of a certain size, in which data of any type and structure can be stored.

To work with files, special data types called streams. Flow ifstream is used to work with files in reading mode, and ofstream in recording mode. To work with files in both writing and reading mode, a stream is used fstream.

In C++ programs, when working with text files, you need to include libraries iostream And fstream.

In order to write data to a text file, you must:

  1. describe a type variable ofstream.
  2. open.
  3. output information to a file.
  4. be sure to close the file.

To read data from a text file, you must:

  1. describe a type variable ifstream.
  2. open file using function open.
  3. read information from a file; when reading each piece of data, it is necessary to check whether the end of the file has been reached.
  4. close the file.

Writing information to a text file

As mentioned earlier, in order to start working with a text file, you need to describe a variable like ofstream. For example, like this:

ofstream F;

A variable will be created F to write information to a file. At the next stage, the file must be opened for writing. In general, the stream opening operator will look like:

F.open("file", mode);

Here F- a variable described as ofstream, file- full name of the file on disk, mode- mode of working with the file being opened. Please note that when specifying the full file name, you must use a double slash. To access, for example, a file accounts.txt, located in the folder sites on disk D, in the program you must specify: D:\\sites\\ accounts.txt.

The file can be opened in one of the following modes:

  • ios::in- open the file in data reading mode; mode is the default mode for threads ifstream;
  • ios::out- open the file in data recording mode (in this case, information about the existing file is destroyed); mode is the default mode for threads ofstream;
  • ios::app- open the file in the mode of writing data to the end of the file;
  • ios::ate- move to the end of an already open file;
  • ios::trunc- clear the file, this also happens in ios::out mode;
  • ios::nocreate- do not perform the operation of opening a file if it does not exist;
  • ios::noreplace- do not open an existing file.

The mode parameter may be absent, in which case the file is opened in the default mode for this stream.

After successfully opening the file (in any mode) in the variable F will be stored true, otherwise false. This will allow you to check the correctness of the file opening operation.

Open a file (let's take the file as an example D:\\sites\\ accounts.txt) in recording mode in one of the following ways:

After opening the file in write mode, an empty file will be created into which you can write information.

If you want to open an existing file in append mode, then you should use the value ios::app.

After opening a file in recording mode, you can write to it in the same way as to the screen, only instead of the standard output device cout you must specify the name of the open file.

For example, to write to a stream F variable a, the output statement will look like:

F<

For serial output to stream G variables b, c, d The output statement will become like this:

G<

Closing a stream is done using the operator:

F.close();

As an example, consider the following problem.

Problem 1

Create text file D:\\ sites\\accounts .txt and write into it n real numbers.

Solution

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

#include "stdafx.h"
#include
#include
#include
using namespace std;
int main()
{

int i, n;
double a;
//describes a stream for writing data to a file
ofstream f;
//open the file in write mode,
//ios::out mode is set by default
f.open("D: \\sites\\accounts.txt", ios::out ) ;
//enter the number of real numbers
cout<< «n=» ; cin >>n;
//loop for entering real numbers
//and write them to a file
for (i= 0 ; i< n; i++ )
{
cout<< «a=» ;
//enter a number
cin >> a;
f<< a<< «\t «;
}
//closing the stream
f.close();
system("pause");
return 0 ;
}

Reading information from a text file

In order to read information from a text file, you need to describe a variable like ifstream. After this, you need to open the file for reading using the operator open. If you name a variable F, then the first two operators will be like this:

After opening a file in reading mode, you can read information from it in the same way as from the keyboard, only instead of cin you need to specify the name of the stream from which data will be read.

For example, to read data from a stream F into a variable a, the input statement will look like this:

F>>a;

Two numbers in a text editor are considered separated if there is at least one of the characters between them: space, tab, end of line. It’s good when the programmer knows in advance how many and what values ​​are stored in the text file. However, often only the type of values ​​stored in the file is known, and their number may vary. To solve this problem, you need to read the values ​​​​from the file one by one, and before each read, check whether the end of the file has been reached. And the function will help to do this F.eof(). Here F- thread name the function returns a boolean value: true or false, depending on whether the end of the file has been reached.

Therefore, a loop to read the contents of the entire file can be written like this:

To better assimilate the material, consider the problem.

Problem 2

The text file D:\\game\\accounts.txt stores real numbers, display them on the screen and calculate their number.

Solution

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

#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, "RUS");
int n= 0 ;
float a;
fstream F;
//open the file in reading mode
F.open("D: \\sites\\accounts.txt") ;
//if the file was opened correctly, then
if (F)
{
//loop for reading values ​​from a file; the loop execution will be interrupted,
//when we reach the end of the file, in this case F.eof() will return true.
while (!f.eof())
{
//reading the next value from stream F into variable a
F >> a;
//output the value of variable a to the screen
cout<< a<< «\t «;
//increase the number of numbers read
n++ ;
}
//closing the stream
F.close();
//input on the screen the number of numbers read
cout<< «n=» << n<< endl;
}
//if the file opening was incorrect, then the output
//messages about the absence of such a file
else cout<< " File does not exist"<< endl;
system("pause");
return 0 ;
}

This concludes the relatively extensive lesson on text files. The next article will look at the manipulation methods used to process .