Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

6.1 Η διαχείριση αρχείων στην C

Στην C, μπορείς να δημιουργήσεις, να ανοίξεις, να διαβάσεις και να γράψεις σε αρχεία, δηλώνοντας ένα δείκτη τύπου FILE και χρησιμοποιώντας τη συνάρτηση fopen():

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt", "w");
fprintf(fp, "This is some text that we're writing to a file.\n");
fclose(fp);
return 0;
}
#include <stdio.h> int main() { FILE *fp; fp = fopen("file.txt", "w"); fprintf(fp, "This is some text that we're writing to a file.\n"); fclose(fp); return 0; }
#include <stdio.h>

int main() {
    FILE *fp;

    fp = fopen("file.txt", "w");

    fprintf(fp, "This is some text that we're writing to a file.\n");

    fclose(fp);

    return 0;
}

Σε αυτό το παράδειγμα, δηλώνουμε έναν δείκτη fp τύπου FILE, και χρησιμοποιούμε τη συνάρτηση fopen() για να ανοίξουμε ένα αρχείο με όνομα "file.txt" σε λειτουργία εγγραφής ("w"). Στη συνέχεια, χρησιμοποιούμε τη συνάρτηση fprintf() για να εγγράψουμε το κείμενο "This is some text that we're writing to a file." στο αρχείο. Τέλος, κλείνουμε το αρχείο με τη συνάρτηση fclose().

Παρατηρήστε ότι μπορούμε να χρησιμοποιήσουμε τη συνάρτηση fprintf() για να εγγράψουμε κείμενο σε ένα αρχείο, αντί για τη συνήθη χρήση της printf() για να εμφανίσουμε κείμενο στην οθόνη.

Η FILE είναι ουσιαστικά ένας τύπος δεδομένων, και χρειάζεται να δημιουργήσουμε μια μεταβλητή δείκτη για να δουλέψουμε με αυτό (π.χ. fptr). Για να ανοίξουμε ένα αρχείο, χρησιμοποιούμε τη συνάρτηση fopen(), η οποία δέχεται δύο παραμέτρους:

Παράμετρος Περιγραφή
filename Το όνομα του αρχείου που θέλουμε να ανοίξουμε (ή να δημιουργήσουμε), όπως filename.txt
mode Ένας μοναδικός χαρακτήρας, που αντιπροσωπεύει τι θέλουμε να κάνουμε με το αρχείο (ανάγνωση, εγγραφή ή προσθήκη):
w – Γράφει σε ένα αρχείο
a – Προσθέτει νέα δεδομένα σε ένα αρχείο
r – Διαβάζει από ένα αρχείο

Για να δημιουργήσετε ένα αρχείο, μπορείτε να χρησιμοποιήσετε τη λειτουργία w μέσα στη συνάρτηση fopen().

Η λειτουργία w χρησιμοποιείται για εγγραφή σε ένα αρχείο. Ωστόσο, αν το αρχείο δεν υπάρχει, θα δημιουργηθεί ένα νέο αρχείο:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt", "w");
fprintf(fp, "This is some text that we're writing to a file.\n");
fclose(fp);
return 0;
}
#include <stdio.h> int main() { FILE *fp; fp = fopen("file.txt", "w"); fprintf(fp, "This is some text that we're writing to a file.\n"); fclose(fp); return 0; }
#include <stdio.h>

int main() {
    FILE *fp;

    fp = fopen("file.txt", "w");

    fprintf(fp, "This is some text that we're writing to a file.\n");

    fclose(fp);

    return 0;
}

Σε αυτό το παράδειγμα, ανοίγουμε ένα αρχείο με όνομα "file.txt" σε λειτουργία εγγραφής ("w") με τη συνάρτηση fopen(). Αν το αρχείο δεν υπάρχει, θα δημιουργηθεί ένα νέο αρχείο με το όνομα αυτό. Στη συνέχεια, χρησιμοποιούμε τη συνάρτηση fprintf() για να εγγράψουμε το κείμενο "This is some text that we're writing to a file." στο αρχείο. Τέλος, κλείνουμε το αρχείο με τη συνάρτηση fclose().

Δωρεα μεσω Paypal

Για την κάλυψη αναγκών φιλοξενίας και δημιουργίας περιεχομένου.

κατηγοριες μαθηματων

Ιστορικο ενοτητων

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων