Tasks studies - laboratory
https://pl.wikibooks.org/wiki/C/fopen https://pl.wikibooks.org/wiki/C/fclose
https://pl.wikibooks.org/wiki/C/fputs https://pl.wikibooks.org/wiki/C/perror https://pl.wikibooks.org/wiki/C/puts
https://cpp0x.pl/documentation/standard-C/fseek/344 https://cpp0x.pl/documentation/standard-C/ftell/471
https://pl.wikibooks.org/wiki/C/scanf
file1.txt file content
321 Programowanie 3.14 4.52
#include <stdio.h>
int main(int argc, char const *argv[])
{
FILE *in = fopen("files/file1.txt", "r"); // otwiera plik do odczytu (plik musi istniec)
printf("wskaznik na pierwszy adres w pamieci gdzie zpaisany jest odczytany z dysku plik: %p\n",in);
if (in == NULL)
{
perror("Nie udalo sie otworzyc pliku.");
return 1;
}
// funkcja fseek() zmienia pozycję kursora (kursor pokazuje ile bitów lub znaków zostało odczytane - zależy to trybu odczytu pliku)
// Stała SEEK_END wskazuje koniec pliku.
fseek(in, 0, SEEK_END);
// Funkcja ftell() zwraca aktaulną pozycję kursora - dzięki temu możemy odczytać ilość znaków w pliku lub wielkość pliku w binarnym trybie odczytu.
long size = ftell(in);
printf("Aktualna pozycja kursora: %li\n",size);
// SEEK_SET wskazuje na poczatek pliku
fseek(in, 0, SEEK_SET);
size = ftell(in);
printf("Aktualna pozycja kursora(SEEK_SET): %li\n",size);
// Podczas używania funkcji fscanf() i podobnych pozycja kursora będzie się zmieniać.
int a;
char b[50];
fscanf(in, "%i %s", &a, &b); // funkcja fscanf() odczytuje pewną ilość znaków i ustawia kursor na ostatnim odczytanym
size = ftell(in); // sprawdzamy pozycję kursora
printf("Aktualna pozycja kursora: %li\n",size);
fseek(in, 0, SEEK_SET); // ustawiamy pozycję kursora na początkową
// fscanf() - skanuje w poszukiwaniu wzorca i przypisuje wzorce do zmiennych
int x1;
char x2[30];
float x3;
double x4;
int charNumber = fscanf(in, "%i %s %f %lf", &x1, x2, &x3, &x4); // <--------------------------
printf("%i %s %f %f \n", x1, x2, x3, x4);
fclose(in);
// fgets() - odczytuje wskazaną ilość znaków aż do napotkania znaku \n lub wczytania ostatniego znaku podanego jako argument funkcji
in = fopen("files/file1.txt", "r");
char readedText[101];
char restult[100];
fgets(readedText , 30 , in ); // <--------------------------
printf("%s\n", readedText);
fclose(in);
// fgetc() - odczytuje jeden znak z pliku - zwraca kod asci tego znaku
in = fopen("files/file1.txt", "r");
char readedChars[40];
for(int i=0;i<40;i++){
readedChars[i] = fgetc(in); // <--------------------------
}
printf("%s", readedChars);
fclose(in);
return 0;
}
#include <stdio.h>
int main(int argc, char const *argv[])
{
FILE *out = fopen("files/file2.txt", "w");
// fprintf()
int x1 = 321;
float x2 = 3.21;
double x3 = 42.32;
char x4 = 'f';
char x5[] = "abcd";
fprintf(out, "%d\n", x1);
fprintf(out, "%f\n", x2);
fprintf(out, "%lf\n", x3);
fprintf(out, "%c\n", x4);
fprintf(out, "%s\n", x5);
// fputs()
char y1[] = "efgh";
fputs(y1, out);
fputs("\nijklm",out);
fclose(out);
/* code */
return 0;
}
Write a program task1
that will create a file file.txt
containing the string “1 Bike 432”.
1 Bike 432
Write a program task2
that will add another line to the file file.txt
“2 Monitor 200”.
1 Bike 432
2 Monitor 200
When the program is run again, the file.txt
file should add the next lines “2 Monitor 200”. e.g. after 3 runs, the file should look like this.
1 Bike 432
2 Monitor 200
2 Monitor 200
2 Monitor 200
Modify the program to check if the line with the number “2.” exists. If so, do not modify the file, if it does not exist, overwrite the file.
Write the task3
program that will print the contents of the file.txt
file. Then it will ask the user to enter the item name and price, append them to the file, then print its new contents and exit. When the program is run again, it should generate the next item numbers.
File Contents:
1 Bike 432
2 Monitor 200
Add new item:
name: Helmet
price: 376
File Contents:
1 Bike 432
2 Monitor 200
3 Helmet 376
Write a program file_stat
that prints the number of characters in the file file.txt
.
Extend the program to display the number of lines in the file.
Extend the program to print the file size in kB or Bytes.
Create utils.c
files. In the file, create a function filetoarray(char *path)
that will read data from the file whose name is passed by the path parameter. In the function, create an array that can store the entire file (you can use the malloc() function). Then fill the array with data from the file.
The function should return a pointer to the filled array.
Test the function on the file.txt
file and print the result in the console (the contents of the array returned by the filetoarray()
function).
csv file (comma-separated values) is a file where values are separated by a comma. Download the iris.data file from the uci archive https://archive.ics.uci.edu/ml/machine-learning-databases/iris/ Information contained in the file:
Write a program podział
that will read data from the iris.data file and then read 80% of the data from each species and save it to the train.csv
file.
The fgetc() function will be helpful, thanks to which it will be possible to check whether the ‘,’ character has already been read.
Save the rest of the data to the test.csv
file (the remaining 20% from each species).
Create a program test.c
and in it:
iris_row
that will store data from one row iris.data.load_data()
that takes a pointer to an array containing a string of characters that is the path to the file.load_data(char *path)
iris_row
Write a program calculator
that will have options for adding, subtracting, multiplying, division, exponentiation and square root of a number. The calculator should save the history of operations performed to the file log.txt
.