File Handling in C: Functions, Structures, and File Splitting
File Handling in C: A Practical Explanation
File Operations
Data type: FILE *
fopen: This function opens or creates a file physically.
fich = fopen("filename", "mode");
Opening modes: "r"
(read), "w"
(write), "a"
(append), "+"
(update)
fclose(tab);
character = getc(tab);
fgets(string, length or size, fich);
putc(character, cards);
fputs(string, cards);
EOF: End Of File
rewind(tab)
Structures and Records
Creating variables of type STRUCTURE OR RECORD:
struct reg_alumno alumno1;
Access fields using dot notation. Example: alumno1.edad
Arrow notation ->
(If the parameter is input/output). Example: alumno1->age
File Writing and Reading
fwrite: Procedure with parameters:
- Variable that contains the data (
&alumno1
). - Number of bytes that we entered into the file.
sizeof
function: Returns the number of bytes of a data type.sizeof(struct reg_alumno)
- Fixed left as a
1
(indicates that we want to enter data: a student). - File where we enter the data (
cards
).
fwrite(&alumno1, sizeof(struct reg_alumno), 1, cards);
fread: Procedure with 4 parameters.
fread(&alumno1, sizeof(struct reg_alumno), 1, cards);
struct reg_alumno { char name[12]; char surname[25]; int age; float notaMedia; };
Switch Statement Example
int option; switch (option) { case 1: printf("number 1"); }
String Manipulation Function
Function to Delete Character at Position
int funcionborrarpos(char str[20], char elim)
int main() {
char elim;
char str[20];
printf("\n Enter a string:");
gets(str);
printf("\n Enter one character to delete: ");
scanf("%c", &elim);
printf("\n");
funcionborrarpos(str, elim);
printf("\n outside the function ");
getch();
}
int funcionborrarpos(char str[20], char elim) {
printf("\n Inside the function");
printf("\n%c", str);
int i;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == elim) {
while (str[i] != '\0') {
str[i] = str[i + 1];
i++;
}
}
}
printf("\n%c", str);
getch();
}
File Splitting Functions
void mayusculas(FILE *fp);
void lowercase(FILE *fp);
void numbers(FILE *fp);
/* file * is better to fichorigen, fichmayus, fichminus and fichnume. -create a procedure to split files .*/
int main() {
char letter;
char name[25];
printf("\n Separate mayus minussy nums:");
printf("\n --");
printf("\n Enter the name of the file you have open (and the extension...): \n-> ");
gets(name);
getch();
FILE *fp;
fp = fopen(name, "r");
if (fp == NULL)
printf("\nError");
else {
printf("Ok open-file ");
printf("\n - ");
printf("\n ...");
mayusculas(fp);
rewind(fp);
lowercase(fp);
rewind(fp);
numbers(fp);
fclose(fp);
}
getch();
}
void mayusculas(FILE *fp) {
char car;
FILE *fmayus;
fmayus = fopen("mayusculas.txt", "w");
rewind(fp);
if (fmayus == NULL) {
printf("\n Error in mayusculas.txt");
}
else {
printf("\n mayusculas.txt open file");
car = getc(fp);
while (!feof(fp)) {
if ((car >= 'A') && (car <= 'Z')) {
putc(car, fmayus);
}
car = getc(fp);
}
fclose(fmayus);
}
}
void lowercase(FILE *fp) {
char car;
FILE *fminus;
fminus = fopen("minusculas.txt", "w");
rewind(fp);
if (fminus == NULL) {
printf("\n Error in minusculas.txt ");
}
else {
printf("\n minusculas.txt open file ");
car = getc(fp);
while (!feof(fp)) {
if ((car >= 'a') && (car <= 'z')) {
putc(car, fminus);
}
car = getc(fp);
}
fclose(fminus);
}
}
void numbers(FILE *fp) {
char car;
FILE *fnum;
fnum = fopen("numeros.txt", "w");
rewind(fp);
if (fnum == NULL) {
printf("\n Error in numeros.txt");
}
else {
printf("\n numeros.txt open file");
car = getc(fp);
while (!feof(fp)) {
if ((car >= '0') && (car <= '9')) {
putc(car, fnum);
}
car = getc(fp);
}
fclose(fnum);
}
}