NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "project.h"

typedef struct Student {
char studentNumber[7]; // Opiskelijanumero (enintään 6 merkkiä + '')
char surname[50]; // Sukunimi
char name[50]; // Etunimi
struct Student *next; // Osoitin seuraavaan opiskelijaan listalla
} Student;

// Globaali osoitin listan ensimmäiseen opiskelijaan
Student *head = NULL;

// Funktio uuden opiskelijan lisäämiseen listalle
void addStudent(const char *studentNumber, const char *surname, const char *name) {
Student *newStudent = malloc(sizeof(Student));
if (newStudent == NULL) {
printf("Memory allocation failed.n");
return;
}
strcpy(newStudent->studentNumber, studentNumber);
strcpy(newStudent->surname, surname);
strcpy(newStudent->name, name);
newStudent->next = head;
head = newStudent;
}

// Funktio listan tulostamiseen
void handleList() {
Student *current = head;
printf("Students in database:n");
while (current != NULL) {
printf("%s %s %sn", current->studentNumber, current->surname, current->name);
current = current->next;
}
}

// Funktio tiedostoon kirjoittamiseen
void handleWrite(char *filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Failed to open file: %sn", filename);
return;
}
Student *current = head;
while (current != NULL) {
fprintf(file, "%s %s %sn", current->studentNumber, current->surname, current->name);
current = current->next;
}
fclose(file);
printf("Database written to file: %sn", filename);
}

// Funktio tiedostosta lataamiseen
void handleLoad(char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Failed to open file: %sn", filename);
return;
}
// Vapauta nykyinen lista ennen uuden lataamista
Student *current = head, *temp;
while (current != NULL) {
temp = current;
current = current->next;
free(temp);
}
head = NULL;

char studentNumber[7], surname[50], name[50];
while (fscanf(file, "%6s %49s %49s", studentNumber, surname, name) == 3) {
addStudent(studentNumber, surname, name);
}
fclose(file);
printf("Data loaded from file: %sn", filename);
}

// Funktio ohjelman lopettamiseen ja muistin vapauttamiseen
void handleQuit() {
Student *current = head;
while (current != NULL) {
Student *temp = current;
current = current->next;
free(temp);
}
head = NULL;
printf("Exiting program and freeing memory.n");
exit(0);
}

// Komentojen käsittelyfunktio 'A'
void handleAdd(char *args) {
char studentNumber[7], surname[50], name[50];
if (sscanf(args, "%6s %49s %49s", studentNumber, surname, name) == 3) {
addStudent(studentNumber, surname, name);
printf("Successfully added student: %s %s %sn", studentNumber, surname, name);
} else {
printf("Invalid arguments for Add command.n");
}
}

// Ohjelman pääfunktio
int main() {
char input[1024]; // Määritellään syötteelle varattu muistialue
char command; // Muuttuja yksittäisen komennon tallentamiseen
printf("Enter commands:n"); // Käyttäjälle ohjeistus syötteiden antamiseen

// Toistetaan, kunnes fgets palauttaa NULL (esim. EOF tai virhe)
while (fgets(input, sizeof(input), stdin)) {
command = input[0]; // Oletetaan, että komento on rivin ensimmäinen merkki
switch (command) { // Tarkistetaan mikä komento on annettu
case 'A': // Käsittele 'Add' komento
handleAdd(input + 2); // Ohjataan syöte 'A' komennon käsittelijälle
break;
case 'L': // Käsittele 'List' komento
handleList(); // Kutsu listausfunktiota
break;
case 'W': // Käsittele 'Write' komento
handleWrite(strtok(input + 2, "n")); // Kirjoita tiedostoon annettu polku
break;
case 'O': // Käsittele 'Load' komento
handleLoad(strtok(input + 2, "n")); // Lataa tiedot annetusta tiedostopolusta
break;
case 'Q': // Käsittele 'Quit' komento
handleQuit(); // Suorita lopetusfunktio ja poistu ohjelmasta
break;
default: // Jos komento ei vastaa mitään tunnettua komentoa
printf("Invalid command.n"); // Ilmoita virheellisestä komennosta
}
}
return 0; // Päättää ohjelman (tätä riviä ei yleensä saavuteta, koska handleQuit kutsuu exit())
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.