Clilstore Facebook WA Linkedin Email
Login

This is a Clilstore unit. You can link all words to dictionaries.

Input-output stream data. Programming of data streams

The input-output in programming shall mean the process of information exchange between operative memory and external devices.Basic concept related to the information on external computer devices, is the concept of file. Any input-output operation is interpreted as exchange operation with files: input - is reading from file into operative memory; output - is recording of information from operative memory into file. In Pascal the concept of internal and external file is used. Analogue of the concept of internal file in C ++ languages is the concept of the data stream.The difference from the Pascal file variable is that the type is not assigned to thestream in C ++. Stream - is a byte sequence transferred during input-output process. The stream shall be connected to any external device or file on disk. Main differences of files in C are as follows: there is no concept of the file type, and therefore, a fixed structure of the file record. Any file is considered as byte sequence:

byte0 byte1 byte2 …. EOF.

Here EOF is a standard constant – mark of the end of file (EOF- End Off File).

Work with disk file begins with declaration of the pointer to the stream. Format of such declaration:

file *name_of pointer;

For example: FILE *f1;

The next step after declaration of the pointer to the stream - opening of the stream, which is produced using standard function fopen (). This function returns a specific value for the pointer to the stream and therefore, its value is assigned to the previously declared pointer. The corresponding statement has the format:

name_of pointer = fopen (name_of file, mode_of opening);

Parameters of function fopen () arerows that can be both constants and pointers to character arrays.

For example: f1= fopen(”test.txt”,”r”);

Here test. txt – is the name of the physical file in the current disk directory, with which now the stream with pointer f1is connected.

There are the following modes of stream opening and parameters corresponding to them:

Table 4. Stream opening mode

Parameter

Mode

R

open for reading

W

create for recording

A

open for adding

r+

open for reading and recording

w+

create for reading and recording

a+

openforaddingorилиfor reading and recording

 

Stream closing is carried out by functionfclose(),whose prototype is of the form:

int fclose(file *fptr);

Here fptr represents the formal name of the pointerto the closing stream. The function returns zero if the file closing operation was successful.

For example: fclose(f1); fclose(f2);

Using function of formatted output, it is possible to generate a text file on the disc with the results of calculations, presented in character form. Here after, this can be viewed on screen, printed out, edited with a text editor. General view of the function of formatted output:

int fprintf(pointer to the stream, format row, variables list);

Previously used function pintf() to organize display is a special variation of function fprintf().

For example: fprintf(f2,”\n S=%d P=%f”, S,P);

fprintf(f,”\n Square=%5.2f”, pi*R*R);

Formatted input from the text file is carried out using function fscanf (), the general format of which is as follows:

int fscanf(pointer to the stream, formatted row, list of variable adresses);

For example: fscanf(f1,”\n%d%d%d”,&a,&b,&c);

fscanf(f,”%d%f”,&n,&k);

Example 65. 4 integer numbers are recorded in file esep1.txt. The program calculates their sum and product, and displays them in file esep1_1.txt.

main()

{ FILE *f1,*f2; int i,x,s=0,p=1;

f1=fopen("d:\\Pr_C\\esep1.txt","r");

f2=fopen("d:\\Pr_C\\esep1_1.txt","w");

for (i=1; i<5; i++)

{fscanf(f1,"%d",&x);

s+=x; p*=x;}

fprintf(f2,"\n s=%d p=%d",s, p);

fclose(f1); fclose(f2);

}

Example 66.Words are recorded in the file. The program outputs words in the file and their length in two columns.

#include <iostream.h>

#include<string.h>

main()

{ FILE *f1,*f2;int i; char s[20];

f1=fopen("d:\\Pr_C\\esep2.txt","r");

f2=fopen("d:\\Pr_C\\esep2_1.txt","w");

for(i=1;i<4;i++)

{fscanf(f1,"%s",&s);

fprintf(f2,"\n %s %d", s, strlen(s));}

fclose(f1);fclose(f2);

}

Example 67. A few words are recorded in the file. Record the longest word into another file.

#include <iostream.h>

#include<string.h>

main()

{ FILE *f1,*f2;int i,max; char s[20];

f1=fopen("d:\\Pr_C\\esep3.txt","r");

f2=fopen("d:\\Pr_C\\esep3_1.txt","w");

fscanf(f1,"%s",&s); max=strlen(s);

for(i=1; i<4; i++)

{fscanf(f1,"%s",&s);

if (max<strlen(s)) max=strlen(s);}

printf("\n max= %d",max);

f1=fopen("d:\\Pr_C\\esep2.txt","r");

for(i=1; i<4; i++)

{fscanf(f1,"%s",&s);

if (strlen(s)==max) fprintf(f2,"\n The longest word = %s",s);}

fclose(f1); fclose(f2);

}

Example 68. Matrix 3x3 is recorded in the file, filled with integer random numbers in range [0,50], find the sum of matrix rows.

#include <iostream.h>

#include<time.h>

#include<stdlib.h>

main()

{ FILE *f;

int i,j,s; int A[3][3];

f=fopen("d:\\Pr_C\\esep6.txt","w");

srand(time(0));

//filling of the matrix with random numbers

for(i=0;i<3;i++)

{ for (j=0;j<3; j++)

{A[i][j]=rand()%50;

fprintf(f,"%d \t",A[i][j]);}

fprintf(f,"\n");}

//calculation of the sum of rows

for(i=0;i<3;i++)

{s=0;

for (j=0;j<3; j++)

s+=A[i][j];

fprintf(f,"\n s=%d",s);}

fclose(f);

}

Clilstore

Short url:   https://clilstore.eu/cs/6257