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.2 Εγγραφή αρχείων στην C

Ας χρησιμοποιήσουμε ξανά τη λειτουργία w από το προηγούμενο κεφάλαιο, και ας γράψουμε κάτι στο αρχείο που μόλις δημιουργήσαμε.

Η λειτουργία w σημαίνει ότι το αρχείο ανοίγει για εγγραφή. Για να εισάγετε περιεχόμενο σε αυτό, μπορείτε να χρησιμοποιήσετε τη συνάρτηση fprintf() και να προσθέσετε τον δείκτη μεταβλητής (fptr στο παράδειγμά μας) και κάποιο κείμενο:

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

int main() {
    FILE *fptr;

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

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

    fclose(fptr);

    return 0;
}

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

Σημείωση: Εάν γράψετε σε ένα αρχείο που υπάρχει ήδη, το παλιό περιεχόμενο διαγράφεται και το νέο περιεχόμενο εισάγεται. Αυτό είναι σημαντικό να το γνωρίζετε, καθώς μπορείτε να διαγράψετε κατά λάθος υπάρχον περιεχόμενο.

Για παράδειγμα:

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

int main() {
    FILE *fptr;

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

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

    fclose(fptr);

    return 0;
}

Σε αυτό το παράδειγμα, ανοίγουμε ξανά το αρχείο "file.txt" για εγγραφή, αλλά αυτή τη φορά γράφουμε νέο κείμενο, "This is some new text that we're writing to the file.". Το παλιό περιεχόμενο του αρχείου διαγράφεται και αντικαθίσταται από το νέο κείμενο.

Αν θέλετε να προσθέσετε περιεχόμενο σε ένα αρχείο χωρίς να διαγράψετε το παλιό περιεχόμενο, μπορείτε να χρησιμοποιήσετε τη λειτουργία a.

Η λειτουργία a προσθέτει περιεχόμενο στο τέλος του αρχείου:

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

int main() {
    FILE *fptr;

    fptr = fopen("file.txt", "a");

    fprintf(fptr, "This is some new text that we're appending to the file.\n");

    fclose(fptr);

    return 0;
}

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

Δωρεα μεσω Paypal

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

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

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

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