C Programming Basic - Week 2
C Programming Basic - Week 2
Basic – week 2
For HEDSPI Project
Lecturers :
Cao Tuan Dung
Le Duc Trung
Dept of Software Engineering
Hanoi University of Technology
Topics
• Structures, dynamic allocation
review
len1 = strlen(str1);
len2 = strlen(str2);
strcpy(result, str1);
strcpy(result + len1, str2);
return result;
}
Solution: main()
int main(void)
{
char str1[MAX_LEN + 1], str2[MAX_LEN + 1];
char *cat_str;
scanf("%100s", str1);
scanf("%100s", str2);
return 0;
}
Structures - User Defined
Types
• A collection of variables under a
single name.
• A convenient way of grouping several
pieces of related information
together.
• Variables in a struct (short for
structure) are called members or
fields.
Defining a struct
struct struct-name
{
field-type1 field-name1;
field-type2 field-name2;
field-type3 field-name3;
...
};
Example – complex
numbers
struct complex {
int real;
int img;
};
struct complex num1, num2,
num3;
Typedef
• We can combine the typdef with the
structure definition:
int main(void)
{
point_t p;
circle_t c;
if (is_in_circle(&p, &c))
printf("point is in circle\n");
else
printf("point is out of circle\n");
return 0;
}
Pointers in Structures
• If a member in a struct is a pointer,
all that gets copied is the pointer
(the address) itself
• Exercise: Give this type of Student
Working mode for binary file
mode Description
main(void) {
FILE *fptr1, *fptr2;
char filename1[]= "lab1a.txt";
char filename2[]= "lab1.txt";
int reval = SUCCESS;
while (!feof(fin)){
num = fread(buff, sizeof(char),
MAX_LEN, fin);
buff[num * sizeof(char)] = `\0';
printf("%s", buff);
fwrite(buff, sizeof(char), num, fout);
}
}
Solution
#include <stdio.h>
enum {SUCCESS, FAIL, MAX_LEN = 80};
void BlockCat(FILE *fin);
while (!feof(fin)){
num = fread(buff, sizeof(char),
MAX_LEN, fin);
buff[num * sizeof(char)] = `\0';
printf("%s", buff);
}
}
Exercise
• A)Improve the program in previous exercise so
that it accepts the two filenames as command
arguments.
int main(void)
{
FILE *fp;
phoneaddress phonearr[MAX_ELEMENT];
int i,n, irc; // return code
int reval = SUCCESS;
Solution
printf("How many contacts do you want to enter (<20)?");
scanf("%d", &n);
for (i=0; i<n; i++){
printf("name:"); scanf("%s",phonearr[i].name);
printf("tel:"); scanf("%s",phonearr[i].tel);
printf("email:"); scanf("%s",phonearr[i].email);
}
if ((fp = fopen("phonebook.dat","w+b")) == NULL){
printf("Can not open %s.\n", "phonebook.dat");
reval = FAIL;
}
int main(void)
{
FILE *fp;
phoneaddress *phonearr;
// Memory allocation
phonearr =
(phoneaddress *)malloc(2 * sizeof(phoneaddress));
if (phonearr == NULL)
{
printf("Memory allocation failed!\n");
return FAIL;
}
if (fseek(fp,1*sizeof(phoneaddress),SEEK_SET) != 0)
{
printf("Fseek failed!\n");
return FAIL;
}
irc = fread(phonearr, sizeof(phoneaddress), 2, fp);
Solution
for (i=0; i<2; i++){
printf("%s-",phonearr[i].name);
printf("%s-",phonearr[i].tel);
printf("%s\n",phonearr[i].email);
}
fseek(fp,1*sizeof(phoneaddress),SEEK_SET);
irc = fwrite(phonearr, sizeof(phoneaddress), 2, fp);
printf(" fwrite return code = %d\n", irc);
fclose(fp); free(phonearr);
return reval;
}
Exercise
Exercise
• Given a text file call class1EF.txt.
Write a program to insert a space
line between each line in file's
content.
Solution for homework
#include <stdio.h>
#include <stdlib.h>
if (argc != 3) {
prn_info(argv[0]);
exit(1);
}
ifp = fopen(argv[1], "r"); /* open for reading */
ofp = fopen(argv[2], "w"); /* open for writing */
double_space(ifp, ofp);
fclose(ifp);
fclose(ofp);
return 0;
}
Solution for homework
void double_space(FILE *ifp, FILE *ofp)
{
int c;
int main(void)
{
FILE *fp;
int *p;
int i,n, value, sum;
int reval = SUCCESS;