Teknikat e Gjuheve Te Programimit-1
Teknikat e Gjuheve Te Programimit-1
Teknikat e Gjuheve Te Programimit-1
Teknikat e gjuheve te
programimit
Programi
Programi I pare ne gjuhen C
Modifikimet dhe kontrolli i gabimeve
Komentet
Librarite
Funksion main()
Karakteret e Controllit
Funksionet dhe programimi me module
Komentet
#include <stdio.h> /* kjo eshte
nje librari */
int main(void)
/* funksioni kryesor*/
{ // Fillimi i main-it
printf(Komentet!"); /* ky resht paraqet nje shprehje */
return 0; /* Perfundon programin */
}
Librarite
#include <stdio.h>
#include <math.h>
#include <string.h>
Funksion main()
Karakteret e Controllit
#include <stdio.h>
int main(void)
{
printf("\nProgrami i pare ne c.\nProgramoi xxxx yyyy.");
return 0;
}
\n
\r
\b
\t
\a
\?
\"
\\
Rresht i ri
carriage return
backspace
tab horizontal
sinjal
?
\
backslash (\)
Leksioni 2 - Variablat
Cfare eshte nje variabel
Variablat e numbrave
Integer
Emertimi i variablave
Inicializimi dhe perdorimi
Floating-Point
Perdorimi i variablave
/* Program Perdorimi i variablave*/
#include <stdio.h>
int main(void)
{
int salary; /* deklarimi i variablit me emer salary */
salary = 10000; /* vleredhenia */
printf(vlera : %d.", salary);
return 0;
}
shembuj
#include <stdio.h>
int main(void)
{
int Total_Pets;
int Cats;
int Dogs;
int Ponies;
int Others;
/* komente*/
Cats = 2;
Dogs = 1;
Ponies = 1;
Others = 46;
/* llogaritjet*/
Total_Pets = Cats + Dogs + Ponies + Others;
printf(Ne kemi %d total", Total_Pets); /* Output */
return 0;
}
Llojet e variablave
signed char
short int
int
long int
long long int
1
2
4
4
8
unsigned char
1
0 deri 255
unsigned short int ose
unsigned short
2
0 deri 65,535
unsigned int
4
0 deri 4,294,967,295
unsigned long int ose
unsigned long
4
0 deri 4,294,967,295
unsigned long long int ose
unsigned long long 8
0 deri +18,446,744,073,709,551,615
Float point
float
4 3.4E38 (6 decimal)
double
Arithmetika
#include <stdio.h>
int main(void)
{
float radius = 0.0f; /* The radius of the table */
float diameter = 0.0f; /* The diameter of the table */
float circumference = 0.0f; /* The circumference of the table */
float area = 0.0f; /* The area of a circle */
float Pi = 3.14159265f;
printf(Diametri:");
scanf("%f", &diameter); /* Read the diameter from the keyboard */
radius = diameter/2.0f; /* Calculate the radius */
circumference = 2.0f*Pi*radius;
area = Pi*radius*radius; /* Calculate the area */
printf("\nPerimetri eshte %.2f", circumference);
printf("\nSiperfaqja %.2f\n", area);
return 0;
}
Arithmetika
Diametri : 6
Perimetri eshte 18.85
Siperfaqja 28.27
#include <stdio.h>
int main(void)
{
char first = 'T';
char second = 20;
printf("\nThe first example as a letter looks like this - %c", first);
printf("\nThe first example as a number looks like this - %d", first);
printf("\nThe second example as a letter looks like this - %c", second);
printf("\nThe second example as a number looks like this - %d\n", second);
return 0;
}
The output from this program is the following:
The first example as a letter looks like this - T
The first example as a number looks like this - 84
The second example as a letter looks like this -
The second example as a number looks like this - 20
Karakteri
Char letter = 'A';
Char digit = '9';
Char exclamation = '!';
char ch = 0;
scanf("%c", &ch); /* Read one character */
Enumerations
enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday, Sunday};
enum Weekday today = Wednesday;
Boolean
_Bool 0 False , 1 - True
_Bool valid = 1; /* Boolean variable initialized to true */
Ose
#include <stdbool.h>
bool valid = true; /* Boolean variable initialized to true */
Numrat komplex
float _Complex
double _Complex
long double _Complex
double complex z1 = 2.0 + 3.0*I;
op= (Operatoret)
number = number + 10; number += 10;
( +, -, *, /)
Funksionet matematik
floor(x) Returns the largest integer that isnt greater than x as type double
ceil(x) Returns the smallest integer that isnt less than x as type double
fabs(x) Returns the absolute value of x
log(x) Returns the natural logarithm (base e) of x
log10(x) Returns the logarithm to base 10 of x
exp(x) Returns the value of ex
sqrt(x) Returns the square root of x
double x = 2.25;
double less = 0.0;
double more = 0.0;
double root = 0.0;
less = floor(x); /* Result is 2.0 */
more = ceil(x); /* Result is 3.0 */
root = sqrt(x); /* Result is 1.5 */
Leksioni 3
Vendimet
Krahasimet aritmetike
If else
Operatoret Logjik
Operatoret e kushtezuar
Rradha e operatoreve
Switch
goto
Operatoret e Biteve
Krahasimet aritmetike
<
==
>
>=
<=
!=
me e vogel
e barabarte
me e madhe
me e madhe ose e barabarte
me e vogel ose e barabarte
e ndryshme
if(expression)
Statement1;
Next_statement;
Krahasimet aritmetike
If
#include <stdio.h>
int main(void)
{
int number = 0;
printf("\Shkruani nje numer : ");
scanf("%d",&number);
if(number > 5)
printf(Ju shkruajtet %d e cila eshte me e madhe se 5\n", number);
if(number < 6)
printf(" Ju shkruajtet %d e cila eshte me e vogel se 6\n ", number);
return 0;
If else
If (expression)
Statement1;
else
Statement2;
Next_statement;
IF - ELSE
#include <stdio.h>
int main(void)
{ const double p = 35.0; /* Cmimi per cope */
int s = 0;
printf(Shkruani sasine :");
scanf(" %d", &s); /* input ne variablin s */
/* Krahasojme sasine per te bere zbritje */
if(s>10) /* 5% zbritje */
printf(Vlera e %d copeve= %.2f\n", s, s*p*0.95);
else /* nuk ka zbritje */
printf(" Vlera e %d copeve= %.2f\n", s, s*p);
return 0;
}
IF - ELSE
if(expression)
{
StatementA1;
StatementA2;
...
}
else
{
StatementB1;
StatementB2;
...
}
Next_statement;
IF - ELSE
if(expression1)
{
StatementA;
if(expression2)
StatementB;
else
StatementC;
}
else
StatementD;
Statement E;
Operatoret Logjik
Operator AND --- &&
if(age > 12 && age < 20)
printf("You are officially a teenager.");
Operator OR --- ||
if(a < 10 || b > c || c > 50)
printf("At least one of the conditions is true.");
Operator NOT --- !
If (!(age > 12 && age < 20))
printf("You are not a teenager.");
Operatoret Logjik
#include <stdio.h>
int main(void)
{
char letter =0;
printf(Shtypni nje shkronje te madhe :");
scanf(" %c", &letter);
if((letter >= 'A') && (letter <= 'Z'))
{
letter += 'a'-'A'; /* Convert to lowercase */
printf(Ju shtypet nje %c te madhe.\n", letter);
}
else
printf(Ju nuk shtyper nje shkronje te madhe.\n");
return 0;
}
Operatoret e kushtezuar
condition ? expression1 : expression2
Shembull :
x = y > 7 ? 25 : 50;
if(y > 7)
x = 25;
else
x = 50;
Shembull II :
total_price = p*s*(s>10 ? 1.0 : 0.95);
Else - If
if(choice1)
/* Statement or block for choice 1 */
else if(choice2)
/* Statement or block for choice 2 */
else if(choice3)
/* Statement or block for choice 2 */
/* and so on */
else
/* Default statement or block */
Switch
switch(integer_expression)
{
case constant_expression_1:
statements_1;
break;
....
case constant_expression_n:
statements_n;
break;
default:
statements;
}
Switch
/* Lucky Lotteries */
#include <stdio.h>
int main(void)
{ int choice = 0;
printf("\nZgjidhnji nje numer nga 1 tek 10 : ");
scanf("%d",&choice);
switch(choice)
{
case 7:
printf("\nCongratulations!");
break;
case 2:
printf("\nYou win a pen.");
break;
default:
printf("\nSorry, you lose.\n");
break;
}
return 0; }
goto
goto there;
Shembull :
#include <stdio.h>
int main(void)
{
int i= 0,s=0;
Shuma:
s=s+i;
i=i+1;
if (i<=10) goto Shuma;
printf(Totali = %d,s);
return 0;
}
Operatoret e Biteve
&
|
^
~
>>
<<
x
y
x&y
00001101
00000110
00000100
Leksioni 4 - Ciklet
Cfare jane ciklet dhe si punojne
Operatori i Inkrementit dhe Dekrementit
Cikli FOR
Cikli WHILE
Cikli Do While
Ciklet e kombinuara
y = ++a + b;
-- Zvogelon vleren me 1
number--; ose --number;
Cikli FOR
int count;
for(count = 1 ; count <= 10 ; ++count)
printf("\n%d", count);
Cikli FOR
/* Afishimi i numrave nga 1 deri ne 10
#include <stdio.h>
int main(void)
{
int count = 1;
for( ; count <= 10 ; ++count)
printf("\n%d", count);
printf("\nProgrami perfundoi.\n");
return 0;
}
Cikli FOR
Cikli FOR
#include <stdio.h>
int main(void)
{
long s = 0L; /* Variabli s per shumen e numrave */
int n = 0;
printf("\nShkruani N: ");
scanf(" %d", &n);
/* Shuma e numrave te plote nga 1 deri ne n*/
for(int i = 1 ; i <= n; i++)
s += i;
printf("\nTotal = %ld\n", sum);
return 0;
}
Cikli FOR
for( ;; )
statement;
Shembull :
char answer = 0;
for( ;; )
{
/* Code to read and process some data */
printf("Do you want to enter some more(y/n): ");
scanf("%c", &answer);
if(tolower(answer) == 'n')
break; /* Go to statement after the loop */
}
/* Statement after the loop */
Cikli WHILE
While this condition is true
Keep on doing this
while( expression )
Statement1;
Statement2;
Cikli WHILE
Cikli WHILE
#include <stdio.h>
int main(void)
{
long sum = 0L;
int i = 1;
int count = 0;
printf("\nShkruani N: ");
scanf(" %d", &count);
/* Shuma e numrave te plote nga 1 deri ne n*/
while(i <= count)
sum += i++;
printf("Totali = %ld\n", sum);
return 0;
}
Cikli Do While
do
Statement;
while(expression);
Cikli Do While
int number = 4;
while(number < 4)
{
printf("\nNumber = %d", number);
number++;
}
int number = 4;
do
{
printf("\nNumber = %d", number);
number++;
}
while(number < 4);
Cikli Do While
/* Reversing the digits */
#include <stdio.h>
int main(void)
{
int number = 0; int rebmun = 0;
int temp = 0;
printf("\nEnter a positive integer: ");
scanf(" %d", &number);
temp = number;
do
{
rebmun = 10*rebmun + temp % 10;
temp = temp/10;
} while(temp); /* Continue while temp>0 */
printf("\n %d reversed is %d \n, number, rebmun );
return 0;
}
Ciklet e kombinuara
for(int i = 0 ; i<10 ; ++i)
{
for(int j = 0 ; j<20 ; ++k) /* Loop executed 10 times */
{
for(int k = 0 ; k<30 ; ++k) /* Loop executed 10x20 times */
{
/* Loop body executed 10x20x30 times */
/* Do something useful */
}
}
}
Ciklet e kombinuara
Te realizohet programi i cili llogarit shumen e numrave nga 1 deri
ne nje numer te dhene dhe e paraqet ne formen e meposhteme :
1+2+3+4+5 = 15
#include <stdio.h>
int main(void)
{ long sum = 1L; int j = 1; int count = 0;
printf("\nEnter the number of integers : "); scanf(" %d", &count);
for(int i = 1 ; i <= count ; i++)
{ sum = 1L; j=1;
printf("\n1");
/* Llogaritja e shumes nga 1 ne i */
while(j < i)
{ sum += ++j;
printf("+%d", j); /* Output +j ne te njejtin vend */
}
printf(" = %ld\n", sum); /* Output = sum */
}
return 0; }
Vektoret
Programimi pa vektore
Cfare eshte nje vektor
Perdorimi i vektoreve
Memorja dhe Adresat
Inicializimi i nje vektori
Vektoret Multidimensional
Programimi pa vektore
#include <stdio.h>
int main(void)
{
int number = 0;
int count = 10;
long sum = 0L;
float average = 0.0f; /* Mesatarja */
/* Leximi i numrave per llogaritjen e mesatares */
for(int i = 0; i < count; i ++)
{
printf(Shkruani numrin %d: ", i);
scanf("%d", &number);
sum += number;
}
average = (float)sum/count; /* Mesatarja */
printf("\nMesatarja e 10 numrave qe ju futet= %f\n", average);
return 0;
}
Perdorimi i vektoreve
#include <stdio.h>
int main(void)
{
int numbers[10];
/* Vektor per 10 numra */
int count = 10;
/* Numri i elementeve per lexim*/
long sum = 0L;
/* Shuma e numrave*/
float average = 0.0f;
/* Mesatarja */
printf("\nShkruani 10 numrats:\n");
/* Leximi i numrave nga tastjera*/
for(int i = 0; i < count; i ++)
{
printf("%2d> ",i+1);
scanf("%d", &numbers[i]);
sum += numbers[i];
}
average = (float)sum/count;
printf("\nMesatarja = %f\n", average);
return 0;
}
Output :
A variable of type long occupies 4 bytes.
Here are the addresses of some variables of type long:
The address of a is: 0064FDF4 The address of b is: 0064FDF0
The address of c is: 0064FDEC
Vektoret Multidimensional
float numbers[3][5];
Vektoret Multidimensional
String
Cfare eshte nje String?
Veprimet me String-e
Vektoret e Stringave
Funksionet e librarise String
Transformimi i Stringave
Output :
The character
Veprimet me String-e
Bashkimi i String-ave
#include <stdio.h>
int main(void)
{
char str1[40] = "To be or not to be";
char str2[] = ",that is the question";
int count1 = 0;
int count2 = 0;
while (str1[count1] )
count1++;
while (str2[count2])
count2++;
if(sizeof str1 < count1 + count2 + 1)
printf("\nYou can't put a quart into a pint pot.");
else
{
count2 = 0;
while(str2[count2]) str1[count1++] = str2[count2++];
str1[count1] = '\0';
printf("\n%s\n", str1 );
}
return 0; }
Vektoret e Stringave
char sayings[3][32] = {
Fjalia 1.",
Fjalia 2.",
Fjali tjeter."
};
strcpy(string1, string2);
count2 = strlen(str2);
strcat(str1, str2); ose strncat(str1, str2, 5)
strcmp(str1, str2);
strchr(str, c);
strstr(text, word);
strcpy(string1, string2);
if(sizeof(string2) <= sizeof (string1))
strcpy(string1, string2);
strlen(str2);
while (str2[count2])
count2++;
Ose
count2 = strlen(str2);
strcat(str1, str2);
count2 = 0;
while(str2[count2])
str1[count1++] = str2[count2++];
str1[count1] = '\0';
strcat(str1, str2);
Ose
strncat(str1, str2, 5);
Krahasimi i stringave
char str1[] = "The quick brown fox";
char str2[] = "The quick black fox";
if(strcmp(str1, str2) < 0)
printf("str1 is less than str2");
Transformimi i Stringave
islower()
Lowercase letter
isupper()
Uppercase letter
isalpha()
Uppercase or lowercase letter
isalnum()
Uppercase or lowercase letter or a digit
iscntrl()
Control character
isprint()
Any printing character including space
isgraph()
Any printing character except space
isdigit()
Decimal digit ('0' to '9')
isxdigit() Hexadecimal digit ('0' to '9', 'A' to 'F', 'a' to 'f')
isblank()
Standard blank characters (space, '\t')
isspace()
Whitespace character (space, '\n', '\t', '\v', '\r', '\f')
ispunct()
Printing character for which isspace() and
isalnum() return false
Pointer
Cfare eshte nje pointer dhe si perdoret
Ndryshimi midis pointerit dhe vektorit
Perdorimi i pointerave me string
Si mund te deklarojme dhe te perdorim vektor
pointerash
#include <stdio.h>
int main(void)
{
long num1 = 0L;
long num2 = 0L;
long *pnum = NULL;
pnum = &num1; /* Get address of num1 */
*pnum = 2; /* Set num1 to 2 */
++num2; /* Increment num2 */
num2 += *pnum; /* Add num1 to num2 */
pnum = &num2; /* Get address of num2 */
++*pnum; /* Increment num2 indirectly */
printf(\nnum1= %ld num2 = %ld *pnum = %ld *pnum + num2 = %ld\n",
num1, num2, *pnum, *pnum + num2);
return 0;
}
num1 = 2 num2 = 4 *pnum = 4 *pnum + num2 = 8
#include <stdio.h>
main()
{
char *c= test;
printf(C permban %s", c);
return 0;
}
Vektor pointerash
char names[5][20]={ {"George"},
{''Michael"},
{"Joe"},
{"Marcus"},
{"Stephanie"} };
/* Prints George */
printf("%s", *(names+1));
/* Prints Michael */
Strukturimi i programit
Hapsira e variablave
{
int a = 0; /* Create a */
/* Reference to a is OK here */
/* Reference to b is an error here */
{
int b = 10; /* Create b */
/* Reference to a and b is OK here */
} /* b dies here */
/* Reference to b is an error here */
/* Reference to a is OK here */
} /* a dies here */
Hapsira e variablave
#include <stdio.h>
int main(void)
{
int count1 = 1; /* Declared in outer block */
do
{
int count2 = 0; /* Declared in inner block */
++count2;
printf("\ncount1 = %d count2 = %d", count1,count2);
} while( ++count1 <= 8 );
/* count2 no longer exists */
printf("\ncount1 = %d\n", count1);
return 0;
}
Funksionet
Return_type Function_name ( Parameterat)
{
Statements;
}
Funksionet
#include <stdio.h>
/* Definition of the function to calculate an average */
float average(float x, float y)
{
return (x + y)/2.0f;
}
/* main program - execution always starts here */
int main(void)
{
float value1 = 0.0F; float value2 = 0.0F; float value3 = 0.0F;
printf("Enter two floating-point values separated by blanks: ");
scanf("%f %f", &value1, &value2);
value3 = average(value1, value2);
printf("\nThe average is: %f\n", value3);
return 0;
}
Enter two floating-point values separated by blanks: 2.34 4.567
The average is: 3.453500
Mekanizmi i parametrave
Funksionet
#include <stdio.h>
float average(float, float); /* Function prototype */
int main(void)
{
/* Code in main() ... */
}
float average(float x, float y)
{
return (x + y)/2.0;
}
Funksionet
void str_sort(const char *p[], int n)
{
char *pTemp = NULL; /* Temporary pointer */
bool sorted = false; /* Strings sorted indicator */
while(!sorted) /* Loop until there are no swaps */
{
sorted = true; /* Initialize to indicate no swaps */
for(int i = 0 ; i < n-1 ; i++ )
if(strcmp(p[i], p[i + 1] ) > 0)
{
sorted = false; /* indicate we are out of order */
swap(&p[i], &p[i+1]); /* Swap the pointers */
}
}
}
Rekursiviteti
void Looper(void)
{
printf("\nLooper function called.");
Looper(); /* Recursive call to Looper() */
}
Rekursiviteti
#include <stdio.h>
unsigned long factorial(unsigned long);
int main(void)
{
unsigned long number = 0L;
printf("\nEnter an integer value: ");
scanf(" %lu", &number);
printf("\nThe factorial of %lu is %lu\n", number, factorial(number));
return 0;
}
/* Our recursive factorial function */
unsigned long factorial(unsigned long n)
{
if(n < 2L)
return n;
return n*factorial(n - 1);
}
Rekursiviteti
main()
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Program name: %s\n", argv[0]);
for(int i = 1 ; i<argc ; i++)
printf("\nArgument %d: %s", i, argv[i]);
return 0;
}
Struktura
Cfare jane Strukturat
Si deklarohet nje strukture
Perdorimi struktures
Vektore e llojeve struct
Structura
struct [structure tag]
{
member definition;
member definition;
:
member definition;
} [one or more structure variables];
struct student
{
int age;
int group;
char name[20];
char father[20];
char mother[20];
};
struct student st1,st2;
Structura
#include <stdio.h>
int main(void)
{
struct student /* Structure declaration */
{
int age;
char name[20]; char father[20];
char mother[20]; };
struct student st1; /* Structure variable declaration */
/* Initialize the structure variable from input data */
printf("Enter the name : " ); scanf("%s", st1.name); /* Read the student's name */
printf("How old is %s? ", st1.name ); scanf("%d", &st1.age ); /* Read age */
printf("Who is %s's father? ",st1.name ); scanf("%s",st1.father ); /*the father's name */
printf("Who is %s's mother? ", st1.name ); scanf("%s", st1.mother );
/* Now tell them what we know */
printf("\n%s is %d years old,st1.name, st1.age);
printf(" and has %s and %s as parents.\n", st1.father,at1.mother );
return 0;
}
Structura
#include <stdio.h>
int main(void)
{ int nr,i;
struct student /* Structure declaration */
{
char emri[20];
char mbieri[20];
};
struct student st[100]; /* Structure variable declaration */
printf(Shkruani nr e studenteve: " ); scanf("%d", &nr);
for (i=0;i<nr;i++)
{
printf(Emri i studentit %d ", i+1 ); scanf("%s", st[i].name);
printf(Mbiemri i studentit %d ", i+1 ); scanf("%s", st[i].mbieri);
}
return 0;
}
Structura
struct student
{
struct Date
{
int day;
int month;
int year;
} dob;
char name[20];
char father[20];
char mother[20];
};
Structura
#include <stdio.h>
struct student
{ char stName[25];
char grade;
int age;
float average; };
main()
{
struct student std1 = {''Joe Brown", 'A', 13, 91.4};
struct student std2, std3;
/* Not initialized */
std2 = std1;
/* Copies each member of std1 */
std3 = std1;
/* to std2 and std3
*/
printf("The contents of std2:\n");
printf("%s, %c, ", std2.stName, std2.grade);
printf("%d, %.1f\n\n", std2.age, std2.average);
printf("The contents of std3:\n");
printf("%s, %c, ", std3.stName, std3.grade);
printf("%d, %.1f\n", std3.age, std3.average);
return 0;
}
File
Cfare eshte nje file
Si perdoren file-at
Shkrimi dhe leximi mbi file
File-at temporary
File
Accessing Files
Hapja e nje File
FILE *fopen(char *name, char *mode);
Mode
"w" Hap nje file text per shkrim. Nese egziston e mbishkruan.
"a" Hap nje file text per shkrim ne fund .
"r" Hap nje file text per lexim.
FILE *pfile = fopen("myfile.txt", "w");
fclose(pfile);
Perdorimi i file-ve
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int LENGTH = 80;
int main(void)
{
char mystr[LENGTH]; int lstr = 0;
int mychar = 0; FILE *pfile = NULL; char *filename = "C:\\myfile.txt";
printf("\nShkruani nje shprehje :\n"); gets(mystr);
if(!(pfile = fopen(filename, "w")))
{ printf(gabim ne hapjen e file-it %s.", filename); exit(1); }
lstr = strlen(mystr);
for(int i = lstr-1 ; i >= 0 ; i--)
fputc(mystr[i], pfile);
fclose(pfile);
if(!(pfile = fopen(filename, "r")))
{ printf(" gabim ne hapjen e file-it %s.", filename); exit(1); }
while((mychar = fgetc(pfile)) != EOF)
putchar(mychar);
fclose(pfile);}
File-at binar
Pozicionimi ne file
Pozicioni aktual
long ftell(FILE *pfile);
fpos = ftell(pfile);
Pozicioni i ri
int fseek(FILE *pfile, long offset, int origin);
File-at
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
/* Global Data */
const int NAME_MAX = 20;
struct
{
char *filename; /* Physical file name */
FILE *pfile; /* File pointer */
} global = {"C:\\myfile.bin", NULL };
/* Structure types */
struct Date /* Structure for a date */
{
int day;
int month;
int year;
};
File-at
typedef struct family
{
struct Date dob;
char name[NAME_MAX];
char pa_name[NAME_MAX];
char ma_name[NAME_MAX];
}Family;
bool get_person(Family *pfamily); /* Input function */
void getname(char *name); /* Read a name */
void show_person_data(void); /* Output function */
void get_parent_dob(Family *pfamily); /* Function to find pa & ma */
int main(void)
{
Family member; /* Stores a family structure */
if(!(global.pfile = fopen(global.filename, "wb")))
{ printf("\n Gabim me file %s.\n", global.filename); exit(1); }
File-at
while(get_person(&member)) /* As long as we have input */
fwrite(&member, sizeof member, 1, pfile); /* write it away */
fclose(global.pfile); /* Close the file now its written */
show_person_data(); /* Show what we can find out */
}
File-at
bool get_person(Family *temp)
{
static char more = '\0'; /* Test value for ending input */
printf("\nDo you want to enter details of a person (Y or N)? );
scanf(" %c", &more);
if(tolower(more) == 'n') return false;
printf("\nEnter the name of the person: "); gets(temp->name);
printf("\nEnter %s's date of birth (day month year); ", temp->name);
scanf("%d %d %d", &temp->dob.day, &temp->dob.month, &temp->dob.year);
File-at
void show_person_data(void)
{
Family member;
fpos_t current = 0;
if(!(global.pfile = fopen(global.filename, "rb")))
{ printf("\nUnable to open %s.\n", global.filename); exit(1); }
while(fread(&member, sizeof member, 1, global.pfile))
{
fgetpos(global.pfile, ¤t); /* Save current position */
printf("\n\n%s's father is %s, and mother is %s.",
member.name, member.pa_name, member.ma_name);
get_parent_dob(&member); /* Get parent data */
fsetpos(global.pfile, ¤t); /* Position file to read next */
}
fclose(global.pfile); /* Close the file */
}
Temporary
FILE *tmpfile(void);
FILE pfile; /* File pointer */
pfile = tmpfile(); /* Get pointer to temporary file */
File unike
char *tmpnam(char *filename);
Header