0% found this document useful (0 votes)
35 views

Lab5.docx 0

The document summarizes a lab report on working with files and dynamic memory allocation in C. It includes code to: 1) Create 3 text files - one to write user input to, one to count lines, and one to combine the first two files. 2) Take user input, write it to a file, read the file and write it to a new file adding line breaks between words. The conclusion states the lab worked on files, structures, sorting algorithms, and dynamic memory allocation in C.

Uploaded by

Alberto Bonnuci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Lab5.docx 0

The document summarizes a lab report on working with files and dynamic memory allocation in C. It includes code to: 1) Create 3 text files - one to write user input to, one to count lines, and one to combine the first two files. 2) Take user input, write it to a file, read the file and write it to a new file adding line breaks between words. The conclusion states the lab worked on files, structures, sorting algorithms, and dynamic memory allocation in C.

Uploaded by

Alberto Bonnuci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Ministerul Educaţiei

al Republicii Moldova

Universitatea Tehnică a Moldovei

RAPORT
despre lucrarea de laborator Nr. 5
la Structuri de Date si Algoritmi

Tema: Lucru cu Fisierele in C și Lucrul cu alocarea dinamică a memoriei


în C
Varianta 11

A îndeplinit:

Chişinău – 2020
Mersul lucrării:

Ex 1

Codul:
#include <stdio.h>
#include <string.h>
int main(){
FILE *fg;
int a;
char c;
printf("Introdu cate linii de text vor fi scrise: ");
scanf("%d", &a);
fisier1(fg, a);
fisier2();
stergere();
fisier3();
return 0;
}

int fisier1(FILE *f,int n){


char str[30];
int i;
f = fopen("1.txt", "w");
for(i=0;i<n+1;i++){
fgets(str, sizeof str, stdin);
fputs(str, f);
}
fclose(f);
return 0;
}

int fisier2(){
FILE *f;
FILE *f2;
char ch;
int nr=0;
f = fopen("1.txt", "r");
f2 = fopen("2.txt", "w");
int frecv = 0;
while((ch = fgetc(f)) != EOF){
if(ch == '\n'){
nr++;
}
}
fprintf(f2, "sunt %d linii de text", nr-1);
fclose(f);
fclose(f2);
return 0;
}
void stergere()
{
FILE *f1, *f2;
f1 = fopen("./1.txt", "r");
f2 = fopen("./2.txt", "a");
int i, j = 0, k = 0, count = 0;
char str[100], key[20];
fgets(str, 100, f1);
char str1[10][20];
for (i = 0; str[i]!= '\0'; i++)
{
if (str[i]==' ')
{
str1[k][j] = '\0';
k++;
j = 0;
}
else
{
str1[k][j] = str[i];
j++;
}
}
str1[k][j] = '\0';
printf("introdu cuvantul:");
scanf("%s", key);
for (i = 0;i < k + 1; i++)
{
if (strcmp(str1[i], key) == 0)
{
for (j = i; j < k + 1; j++)
strcpy(str1[j], str1[j + 1]);
k--;
}
}
for (i = 0;i < k + 1; i++)
{
fprintf(f2, "\n%s ", str1[i]);
}
fclose(f1);
fclose(f2);
}

int fisier3(){
char ch;
FILE *f1, *f2, *f3;
f1 = fopen("1.txt", "r");
f2 = fopen("2.txt", "r");
f3 = fopen("3.txt", "w");
while((ch = fgetc(f1)) != EOF){
fputc(ch,f3);
}
while((ch = fgetc(f2)) != EOF){
fputc(ch,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
return 0;
}
1.txt

2.txt

3.txt
Ex 2

Codul:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int func2();

int main()
{
char text[100];
printf("Introdu un string\n");
introdu(text);
func2();
return 0;
}

int introdu(char *text){


FILE *f;
f = fopen("input.txt", "w");
gets(text);
fprintf(f, "%s", text);
fclose(f);
return 0;
}

int func2() {
FILE *f, *f1;
char ch;
char *text;
int nr = 0;
int n = 0;
int i = 0;
f = fopen("./input.txt", "r");
f1 = fopen("./output.txt", "w");
text = (char*)malloc(100);
fgets(text, 100, f);
while(i<strlen(text)){
if(text[i] == ' ' && text[i+1] != ' '){
fprintf(f1, "\n");
nr++;
}else if(text[i] != ' '){
fputc(text[i], f1);
}
i++;
}
fprintf(f1, "\n%d cuvinte au fost scrise", nr+1);
free(text);
fclose(f);
fclose(f1);
}
input.txt

Output.txt

Concluzie: In aceasta lucrare de laborator am lucruat cu fisiere, structuri si diferiti


algoritmi de sortari in C. Si cu alocarea memorie in C, functii cum ar fi malloc si free.

You might also like