Subject: PRF192- PFC
Workshop 08
Nguyen Dang Anh Nhat – DE170617
Objectives:
Managing text files
Problem 1(1 mark):
Implement the problem introduced in the demonstration 1 of the lecture
#include <stdio.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
int exist(char* filename)
{
int existed = FALSE;
FILE* f= fopen(filename,"r");
if (f!=NULL)
{
existed = TRUE;
fclose(f);
}
return existed;
}
int writeFile(char* filename)
{
char c;
int CTRL_Z = -1;
if (exist (filename) == TRUE)
{
printf("The file %s existed. Override it Y/N?:", filename);
if(toupper(getchar()) == 'N')
return FALSE;
}
FILE* f = fopen(filename, "w");
fflush(stdin);
do
{
c = getchar();
if (c != CTRL_Z)
fputc(c,f);
}
while (c != CTRL_Z);
fclose(f);
return TRUE;
}
int main(int argCount, char* args[])
{
if (argCount != 2)
printf("Syntax: copy_con filename\n");
else if (writeFile(args[1]) == TRUE)
printf("Writting the file %s: done\n", args[1]);
else
printf("Can not write the file %s\n", args[1]);
return 0;
}
int printFile(char* filename)
{
char c;
if (exist (filename) == FALSE)
{
printf("The file %s does not exist.\n", filename);
return FALSE;
}
FILE* f = fopen(filename, "r");
while ((c = fgetc(f)) != EOF)
putchar(c);
fclose(f);
return TRUE;
}
Problem 2(1 mark):
Implement the problem introduced in the demonstration 2 of the lecture
#include <stdio.h>
#include <string.h>
int exist(char* filename)
{
int existed = -1;
FILE* f= fopen(filename,"r");
if (f != NULL)
{
existed = 1;
fclose(f);
}
return existed;
}
int writeFile(char* filename)
{
if (exist(filename) == 1)
{
printf("Write file:\n");
printf("The file %s existed. Override it Y/N?: ", filename);
if (toupper(getchar()) == 'N')
return 0;
else
{
char line[201];
int length = 0;
FILE* f = fopen(filename,"w");
fflush(stdin);
do
{
gets(line);
length = strlen(line);
if (length > 0)
{
fputs(line, f);
fputs("\n", f);
}
}
while (length > 0);
fclose(f);
return 1;
}
}
}
int printFile(char* filename)
{
char c;
if (exist(filename) == 0)
{
printf("The file %s does not exist.\n", filename);
return 0;
}
FILE* f = fopen(filename, "r");
while ((c = fgetc(f)) != EOF)
putchar(c);
fclose(f);
return 1;
}
int main(int argCount, char* args[])
{
char filename[81];
printf("Program for writting then reading a file\n");
printf("Enter a file name: ");
gets(filename);
int x = writeFile(filename);
if (x == 1)
{
printf("Data in the filename %s:\n", filename);
if (printFile(filename) == 0)
printf("File error!\n");
}
else if (x == 0)
printf("\nWriting file fail!\n");
else if (x == -1)
printf("The file %s does not exist.\n", filename);
return 0;
}
Problem 3(1 mark):
Implement the problem introduced in the demonstration 3 of the lecture
#include <stdio.h>
#include <string.h>
int exist(char* filename)
{
int existed = -1;
FILE* f= fopen(filename,"r");
if (f != NULL)
{
existed = 1;
fclose(f);
}
return existed;
}
int writeFile(char* filename)
{
if (exist(filename) == 1)
{
printf("Write file:\n");
printf("The file %s existed. Override it Y/N?: ", filename);
if (toupper(getchar()) == 'N')
return 0;
else
{
char line[201];
int length = 0;
FILE* f = fopen(filename,"w");
fflush(stdin);
do
{
gets(line);
length = strlen(line);
if (length > 0)
fprintf(f,"%s\n", line);
}
while (length > 0);
fclose(f);
return 1;
}
}
}
int printFile(char* filename)
{
char c;
if (exist(filename) == 0)
{
printf("The file %s does not exist.\n", filename);
return 0;
}
FILE* f = fopen(filename, "r");
char line[201];
while (fscanf(f,"%[^\n]%*c", line) > 0)
puts(line);
fclose(f);
return 1;
}
int main(int argCount, char* args[])
{
char filename[81];
printf("Program for writting then reading a file\n");
printf("Enter a file name: ");
gets(filename);
int x = writeFile(filename);
if (x == 1)
{
printf("Data in the filename %s:\n", filename);
if (printFile(filename) == 0)
printf("File error!\n");
}
else if (x == 0)
printf("\nWriting file fail!\n");
else if (x == -1)
printf("The file %s does not exist.\n", filename);
return 0;
}
Problem 4(1 mark):
Implement the problem introduced in the demonstration 4 of the lecture
#include <stdio.h>
void fileToArray(char* fname, int *a, int *pn)
{
FILE* f = fopen(fname, "r");
fscanf(f, "%d", pn);
int i;
for (i = 0; i <= *pn; i++)
fscanf(f, "%d", &a[i]);
fclose(f);
}
void asc_sort(int* a, int n)
{
int i, j, t;
for (i = 0; i < n-1; i++)
for (j = n-1; j > i; j--)
if (a[j] < a[j-1])
{
t = a[j];
a[j] = a[j-1];
a[j-1] = t;
}
}
int print(int* a, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int printToFile(char* fname, int* a, int n)
{
FILE* f = fopen(fname, "w");
fprintf(f, "%d ", n);
int i;
for (i = 0; i < n; i++)
fprintf(f, "%d ", a[i]);
fclose(f);
}
int main()
{
char infName[] = "array1.txt";
char outfName[] = "array2.txt";
int a[200];
int n = 0;
fileToArray(infName, a, &n);
asc_sort(a, n);
print(a, n);
printToFile(outfName, a, n);
getchar();
return 0;
}
Problem 5(1 mark):
Implement the problem introduced in the demonstration 5 of the lecture
#include <stdio.h>
void processFile(char* fname, int* pCount, double* pSum)
{
FILE* f = fopen(fname, "r");
*pCount = 0;
*pSum = 0;
double x;
while (fscanf(f, "%lf", &x) == 1)
{
(*pCount)++;
(*pSum) += x;
}
fclose(f);
}
int main()
{
char infName[] = "array3.txt";
int count = 0;
double sum = 0;
processFile(infName, &count, &sum);
printf("Number of value in the file: %d\n", count);
printf("Average of value in the file: %lf\n", sum/count);
getchar();
return 0;
}
Problem 6(3 mark):
• Data representing a student include: name, address, mark.
• A list of students are stored in the file students.txt as below:
• Write a C-program that will print out the list of students in descending order
based on their marks then the list will be written to the file students_2.txt with the
same format as the previous file.
o #include <stdio.h>
o #include <string.h>
o
o #define MAXN 50
o
o void print(char names[][41], char adds[][41], int * marks, int n)
o {
o int i;
o for (i = 0; i < n; i++)
o printf ("%-40s%-40s%4d\n", names[i], adds[i], marks[i]);
o }
o
o void readFile(char *filename, char names[][41], char adds[][41], int *marks, int
*pn)
o {
o *pn = 0;
o FILE* f = fopen(filename, "r");
o if (f != '\0')
o {
o while (fscanf(f, "%40[^;];%40[^;];%d%*c", names[*pn], adds[*pn],
&marks[*pn]) == 3)
o (*pn)++;
o fclose(f);
o }
o }
o
o void sort (char names[][41], char adds[][41], int *marks, int n)
o {
o int i, j;
o for (i = 0; i < n-1; i++)
for (j = n-1; j > i; j--)
if (marks[j] > marks[j-1])
{
char tName[41];
strcpy(tName, names[j]);
strcpy(names[j], names[j-1]);
strcpy(names[j-1], tName);
char tAdd[41];
strcpy(tAdd, adds[j]);
strcpy(adds[j], adds[j-1]);
strcpy(adds[j-1], tAdd);
int tMark = marks[j];
marks[j] = marks[j-1];
marks[j-1] = tMark;
}
}
void writeFile(char * filename, char names[][41], char adds[][41], int * marks, int
n)
{
FILE* f = fopen(filename, "w");
int i;
for (i = 0; i < n; i++)
fprintf(f, "%s;%s;%d\n", names[i], adds[i], marks[i]);
fclose(f);
}
int main()
{
char fileIn[] = "students.txt";
char fileOut[] = "students2.txt";
char names[MAXN][41];
char adds[MAXN][41];
int marks[MAXN];
int n = 0;
readFile(fileIn, names, adds, marks, &n);
sort(names, adds, marks, n);
printf("Sorted List : \n\n");
print(names,adds,marks,n);
writeFile(fileOut,names,adds,marks,n);
printf("\nResult file : %s \n", fileOut);
getchar(); }