//// 10-9 ํ์ด
//
//
//#define _CRT_SECURE_NO_WARNINGS // strcpy, strcat ๋ณด์ ๊ฒฝ๊ณ ๋ก ์ธํ ์ปดํ์ผ ์๋ฌ ๋ฐฉ์ง
//#include <stdio.h>
//#include <string.h> // strcpy, strcat ํจ์๊ฐ ์ ์ธ๋ ํค๋ ํ์ผ
//
//
//int main()
//{
// char s1[10] = "world"; // ํฌ๊ธฐ๊ฐ 10์ธ charํ ๋ฐฐ์ด์ ์ ์ธํ๊ณ ๋ฌธ์์ด ํ ๋น
// char* s2 = malloc(sizeof(char) * 20); // char 20๊ฐ ํฌ๊ธฐ๋งํผ ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น
//
// strcpy(s2, "Hello"); // s2์ Hello ๋ฌธ์์ด ๋ณต์ฌ
//
// strcat(s2, s1); // s2 ๋ค์ s1์ ๋ถ์
//
// printf("%s\n", s2); // Helloworld
//
//
// return 0;
//}
#include <stdio.h>
void my_Strcat(char* dest, char* source);
int main() {
char strDestination[20] = "king";
char strSource[20] = "dom";
my_Strcat(strDestination, strSource);
printf("Source: %s , Destination: %s\n", strSource, strDestination);
}
void my_Strcat(char* dest, char* source) {
for (; *dest; dest++);
for (; *source; dest++, source++) {
*dest = *source;
}
*dest = '\0';
}
//11_9.c
#include<stdio.h>
struct EMP {
char name[20];
int age;
int salary;
}emps[4] = { {"์ง๋ฌ๋", 20, 500000}, {"๊ฐ๋๋ฆฌ", 23, 600000}, {"๊น๊ฟ์ด", 27, 700000}, {'\0'}}; //๋ฐฐ์ด์ ์ ์ญ์ผ๋ก ์ ์ธ
// struct emp* ptr; // *ptr;
int main()
{
struct EMP* ptr, tmp;
ptr = emps; //๊ตฌ์กฐ์ฒด ๋ณ์๋ ๋ฉค๋ฒ๋ค์ ์งํฉ
//while (*ptr->name) // ์ฃผ์๋ ํญ์ ์ฐธ์ด๋ฏ๋ก ๋ฌดํ๋ฃจํ, null์ ๋ง๋ ๋๊น์ง๋ง ์ถ๋ ฅํ๋ ค๋ฉด ๋ฐฐ์ด์ ํน์ ์ฒจ์(๊ฐ) ์ง์ ํ => emps ๋ฐฐ์ด์ ํน์ ์ธ์(๊ฐ)๊ฐ null ์ด๋ฉด break+) ptr->name => (*ptr).name => emps.name
//while (*((*ptr)).name != '\0'){ // while(0) ์ด๋ฉด break // *((*ptr)).name != '\0'
/*while (*(*ptr).name)
{
printf("%s, %d, %d \n", ptr->name, ptr->age, ptr->salary);
ptr++;
}
*/
/*while (*ptr->name) {
printf("%s, %d, %d \n",
ptr->name, ptr->age, ptr->salary); //emp ๋ฐฐ์ด ์ถ๋ ฅ
ptr++; //๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ ์๋ก์ด ์ด์ ๋ฐ์ดํฐ ์์์ฃผ์ ํ ๋นํตํ emp๋ฉค๋ฒ ์์ฐจ ์ถ๋ ฅ => emp[0], emp[1],,,
}
ptr = emps;
tmp = *ptr;
// printf("%s, %d, %d \n",tmp.name, tmp.age, tmp.salary);
return 0;
}
*/
//
//#include <stdio.h>
//int main()
//{
// struct EMP {
// char name[20];
// int age;
// int salary;
// } emps[4] = { {"์ง๋ฌ๋", 20, 500000},
// {"๊ฐ๋๋ฆฌ", 23, 600000},
// {"๊น๊ฟ์ด", 17, 700000},{NULL} };
// struct EMP* ptr;
//
//
// ptr = emps;
// printf("\n");
//
// while (*ptr->name)
// {
// printf("%s, %d, %d \n", ptr->name, ptr->age, ptr->salary);
// ptr++;
// }
// printf("\n");
// return 0;
//}
//
//
//struct1.c
#include <stdio.h>
// ๊ตฌ์กฐ์ฒด(Struct) : ๊ด๋ จ์ด ์๋ ์ฌ๋ฌ ๋ฐ์ดํฐ๋ค์ ํ๋์ ์๋ฃํ์ผ๋ก ๋ง๋ค์ด ์ฌ์ฉํ๊ฒ ํ๋ค.
// ์ฆ, ์ฌ์ฉ์ ์ ์ ์๋ฃํ์ ๋ง๋๋ ๊ฒ์ด๋ค.
// ๋ฐ์ดํฐ๋ค์ ์งํฉ์ ์๋ฃํ์ ๊ตฌ์ ๋ฐ์ง ์์.
int main()
{
// ์ฌ์ฉ์ ์ ์ ์๋ฃํ์ ์ ์ธ
// DB์ create table๊ณผ ์ ์ฌํ ๊ธฐ๋ฅ(field)
// struct ๊ตฌ์กฐ์ฒด ์ด๋ฆ {๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ๋ค};
/*struct EMPLOYEE {
char name[20]; //๊ตฌ์กฐ์ฒด๋ ๋ฉค๋ฒ์ ์งํฉ
int salary;
float height;
char comAddr[50];
} ;
*/
// typedef ํตํด ๊ตฌ์กฐ์ฒด ์ ์ธ๊ณผ ๋์์ ์ ์
// ํ๊ทธ๋ค์์ธ `EMPLOYEE` ์๋ต ๊ฐ๋ฅ(์๋ฃํ ๋ช
์ด EMP๋ก ๋ง๋ค์ด์ง๊ธฐ ๋๋ฌธ)
typedef struct EMPLOYEE
{
char name[20]; //๊ตฌ์กฐ์ฒด๋ ๋ฉค๋ฒ์ ์งํฉ
int salary;
float height;
char comAddr[60];
} EMP;
// ๊ตฌ์กฐ์ฒด ๋ฉ๋ชจ๋ฆฌ ํฌ๊ธฐ๋ ๋์ผ๋ก ๋ณด์ด๋ ๊ฒ๋ณด๋ค (๋จ์ ์ฐ์) ๊ฐ๊ฑฐ๋ ํฌ๋ค => sizeof ์ฌ์ฉ์ผ๋ก ํ์ธ ํ
//์๋ฃํ๋ช
//๊ตฌ์กฐ์ฒด๋ณ์
struct EMPLOYEE emp = { "ํ๊ธธ๋", 4500000, 173.5, "์์ธ์ ๊ฐ๋จ๊ตฌ" };
EMP tmp;
printf("%d, %d \n", sizeof(emp), sizeof(struct EMPLOYEE));
printf("%p, %p \n", &emp, emp.name); // ๊ตฌ์กฐ์ฒด๋ณ์๋ ์ฃผ์๊ฐ ์๋ => & ์ฌ์ฉํ ,๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์์์ฃผ์(์ฒจ์๊ฐ ์๋ ๋ฐฐ์ด ๋ณ์๋ช
)
/*printf("์ฑ๋ช
? ");
gets(emp.name); //๊ณต๋ฐฑ์ ์
๋ ฅ๋ฐ๊ธฐ ์ํด gets() ์ฌ์ฉ
printf("์๊ธ ? ");
scanf_s("%d", &emp.salary); //์ ์, ์ค์๊ฐ์ ์
๋ ฅ๋ฐ๊ธฐ ์ํด scanf_s() ์ฌ์ฉ
// ์ฃผ์ ๋์
printf("ํค(์ ์ฅ) ? ");
scanf_s("%f%*c", &emp.height);
printf("ํ์ฌ ์ฃผ์ ? ");
gets(emp.comAddr);
*/
//๊ตฌ์กฐ์ฒด ๋ณ์์ด๋ฆ.๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์ด๋ฆ
printf("%s, %d, %.2f, %s \n",
emp.name, emp.salary, emp.height, emp.comAddr); //์ฃผ์๊ฐ ์๋, ๋ฐ์ดํฐ ๊ฐ์
tmp = emp;
printf("%s, %d, %.2f, %s \n",
tmp.name, tmp.salary, tmp.height, tmp.comAddr); //์ฃผ์๊ฐ ์๋, ๋ฐ์ดํฐ ๊ฐ์
return 0;
}
//struct2.c
#include <stdio.h>
#define EMP_SZ 30
int main()
{
typedef struct EMPLOYEE
{
char name[20]; //๊ตฌ์กฐ์ฒด๋ ๋ฉค๋ฒ์ ์งํฉ
int salary;
float height;
char comAddr[60];
} EMP;
struct EMPLOYEE emp[EMP_SZ];
struct EMPLOYEE* ptr;
int i, Cn;
for (i = 0; i < EMP_SZ; i++)
{
printf("์ฑ๋ช
? "); //kim
gets(emp[i].name); //emp๋ 1์ฐจ์ ๋ฐฐ์ด (์ด์ ์งํฉ์)
if (!strcmp(emp[i].name, "end"))
break;
printf("์๊ธ ? "); //1000
scanf_s("%d", &emp[i].salary);
printf("ํค(์ ์ฅ) ? "); //163
scanf_s("%f%*c", &emp[i].height);
printf("ํ์ฌ ์ฃผ์ ? "); //๋ถ์ฐ์
gets(emp[i].comAddr);
}
Cn = i;
ptr = emp; // ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ ์์ ์ฃผ์ ์ ์ฅ (๋ฐฐ์ด ๋ณ์๋ ์ฃผ์๊ฐ์ด๋ `&`๊ธฐ์ฌ ๋ถํ์)
//๊ตฌ์กฐ์ฒด ๋ฐฐ์ด ์ถ๋ ฅ ๋ฐ๋ณต๋ฌธ
for (i = 0; i < Cn; i++)
{
printf("%s, %d, %.2f, %s \n",
ptr->name, ptr->salary, ptr->height, ptr->comAddr); //emp ๋ฐฐ์ด ์ถ๋ ฅ
ptr++; //๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ ์๋ก์ด ์ด์ ๋ฐ์ดํฐ ์์์ฃผ์ ํ ๋นํตํ emp๋ฉค๋ฒ ์์ฐจ ์ถ๋ ฅ => emp[0], emp[1],,,
}
return 0;
}
//struct3.c
#include <stdio.h>
// ์ฌ์ฉ์ ์ ์ ์๋ฃํ์ ์ ์ธ -> ํจ์ ์์ ๋ง๋ค๋ฉด ํจ์ ์์์๋ง ์ด์ฉ ๊ฐ๋ฅ โด ๋ฐ์๋ค๊ฐ ์ ์ธ
typedef struct EMPLOYEE {
char name[20];
int salary;
float height;
char comAddr[60];
}emp;
void funcA(struct EMPLOYEE *emp);
struct EMPLOYEE funcB();
int main()
{
struct EMPLOYEE emp = { "ํ๊ธธ๋", 4500000, 173.5, "์์ธ์ ๊ฐ๋จ๊ตฌ" };
struct EMPLOYEE my;
funcA(&emp);
printf("์ค์ธ์๋ก ์ฃผ์ ์ ๋ฌ() emps : % s, % d, % .2f, % s \n", emp.name, emp.salary, emp.height, emp.comAddr);
my = funcB();
printf("my : % s, % d, % .2f, % s \n", my.name, my.salary, my.height, my.comAddr);
return 0;
}
struct EMPLOYEE funcB()
{
struct EMPLOYEE tmp;
tmp.salary = 370000;
tmp.height = 163;
*tmp.name = "์ง๋ฌ๋"; // ๋ถ๊ฐ -> *tmp.name๋ 4000๋ฒ์ง (1๋ฐ์ดํธ (char ํ์
)) ์ฐธ์กฐ = tmp.name[0] ๊ณผ ๋์ผ / *tmp.name = 'A'; ๋ ๊ฐ๋ฅ
strcpy(tmp.name, "์ง๋ฌ๋"); // ๋ฐฐ์ด ๋ณ์๋ ์ฃผ์ ๋ฐ๊ฟ ์ ์์ (ํฌ์ธํฐ ์์) / ํ๊ธ์ 2๋ฐ์ดํธ๋ผ ""์ ์จ์ผ ํจ -> ""๋ ์ฃผ์
// ์ฃผ์์ ์๋ ๋ฉ๋ชจ๋ฆฌ์ ๊ฐ์ ๋ฃ์ ๊ฒ -> ํจ์ ์์ฑ ํน์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด์ฉ
strcpy(tmp.comAddr, "์ฐฝ์์ ์งํด ๋ฒ๊ฝ 100๋ฆฌ");
//printf("funcB() emps : % s, % d, % .2f, % s \n", tmp.name, tmp.salary, tmp.height, tmp.comAddr);
return tmp;
}
void funcA(struct EMPLOYEE *emp) // EMP emp๋ ๊ฐ๋ฅ
{
printf("funcA() emps : % s, % d, % .2f, % s \n", emp->name, emp->salary, emp->height, emp->comAddr);
// (*emp).name
emp->salary += 200000;
}
//struct4.c
/* ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ฅผ ์ด์ฉํด์ ๋์ ์ฌ์ด์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ์์ค*/
//#include "stdio.h"
//#include "math.h"
//
//struct point
//{
// int x;
// int y;
//};
//
////๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๊ฐ๋ ํจ์ ์ ์ธ
//double Get(struct point* p1, struct point* p2)
//{
// int dx = p2->x - p1->x;
// int dy = p2->y - p1->y;
// return sqrt((double)(dx * dx) + (double)(dy * dy));
//}
//
//int main()
//{
// struct point p1 = { 430,170 };
// struct point p2 = { 120,80 };
// double distance;
//
// distance = Get(&p1, &p2); // ๊ตฌ์กฐ์ฒด ๋ณ์์ ์ฃผ์๋ฅผ Get ํจ์์ ์ธ์๋ก ์ ๋ฌํ๋ค.
//
// printf("๋ ์ ์ฌ์ด์ ๊ฑฐ๋ฆฌ : %5.2f\n", distance);
// return 0;
//}
#include <stdio.h>
#include <math.h>
struct Point
{
double x;
double y;
};
double Getdistance(struct Point p1, struct Point p2)
{
double dist;
dist = sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
return dist;
}
int main()
{
double Distance;
struct Point p1 = { 430, 170 };
struct Point p2 = { 120, 80 };
Distance = Getdistance(p1, p2);
printf("๋ ์ ์ฌ์ด์ ๊ฑฐ๋ฆฌ : %lf \n", Distance);
return 0;
}
//struct4.1.c
#include <stdio.h>
typedef struct EMPLOYEE {
char name[20];
int salary;
float height;
char comAddr[60];
}EMP;
int main()
{
struct EMPLOYEE emp = { "ํ๊ธธ๋", 4500000, 173.5, "์์ธ์ ๊ฐ๋จ๊ตฌ" }; //EMP emp = { "ํ๊ธธ๋", 4500000, 173.5, "์์ธ์ ๊ฐ๋จ๊ตฌ" };
struct EMPLOYEE* ptr; // EMP *ptr
printf("%d, %d \n", sizeof(emp), sizeof(ptr)); //100byte, 4byte // ptr : ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ ๋ณ์
ptr = &emp; //๊ตฌ์กฐ์ฒด ๋ณ์์ ์์ ์ฃผ์๋ &emp
// ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ ๋ณ์๊ฐ ๋ฉค๋ฒ์ ์ ๊ทผํ๋ ค๋ฉด โ๊ตฌ์กฐ์ฒดํฌ์ธํฐ๋ณ์->๋ฉค๋ฒ๋ช
โ์ผ๋ก ์ฌ์ฉํ๋ค. , `(*ptr).`๊ณผ ๊ฐ์ ์ญํ ์ํ
printf("%s, %d, %.2f \n", ptr->name, ptr->salary,
ptr->height, (*ptr).comAddr); // *(ptr+0) = ptr[0]
/* printf("%s, %d, %.2f \n", (*ptr).name, (*ptr).salary,
(*ptr).height, (*ptr).comAddr);*/
}
// fgets(str, sizeof(str), fp);
// ํ์ผํฌ์ธํฐ(fp)์ ์ฐ๊ฒฐ๋ ํ์ผ๋ก๋ถํฐ ์ฃผ์ด์ง ๋ฐ์ดํธ ์์๋ฐ๋ผ
// ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์์ tmp๋ก ์ฃผ์ด์ง๋ฐฐ์ด์ ์ ์ฅํ๋ค.
์ฝ์ ์
์ถ๋ ฅ๊ณผ ํ์ผ ์
์ถ๋ ฅ
https://pgh268400.tistory.com/357
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=tipsware&logNo=221558259987
FILE *ํ์ผํฌ์ธํฐ;
ํ์ผํฌ์ธํฐ= fopen("ํ์ผ๋ช
โ, โ๋ชจ๋โ);
fclose(ํ์ผํฌ์ธํฐ๋ณ์);
ํ์ผ ์ ์ถ๋ ฅ ๊ณผ์
ํ์ผ ์คํธ๋ฆผ ์์ฑ : FILE *ํ์ผํฌ์ธํฐ;
ํ์ผ ์ด๊ธฐ๋ฅผ fopen() ํจ์๋ก ์ฒ๋ฆฌ
ํ์ผ ์ ์ถ๋ ฅ์ ์ํ
fclose() ํจ์๋ก ํ์ผ ๋ซ๊ธฐ
๐ฝ
//fileio1.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE* fp; //ํ์ผํฌ์ธํฐ
//1.์คํธ๋ฆผ ์ฐ๊ฒฐ
fp = fopen("file1.txt", "at"); //๋ฌธ์์ด ์์๋ ์ด๋์ธ๊ฐ ์กด์ฌํ๋ ์ฃผ์๋ฅผ ๊ฐ์ ธ์จ๋ค
if (fp == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ \n");
exit(1);
}
printf("stdin -> disk \n"); //๋ฉ๋ชจ๋ฆฌ์ ์๋๊ฑธ ๋์คํฌ์ ๋ฃ๋๋ค
//2.ํ์ผ ์ ์ฅ
while ((ch = fgetc(stdin)) != EOF) //EOF : ์คํ ์ข
๋ฃ (ctrl + z)
fputc(ch, fp);
//3.์คํธ๋ฆผ ๋ซ๊ธฐ
fclose(fp);
//1.์คํธ๋ฆผ ์ฐ๊ฒฐ
fp = fopen("file1.txt", "rt"); //๋ฌธ์์ด ์์๋ ์ด๋์ธ๊ฐ ์กด์ฌํ๋ ์ฃผ์๋ฅผ ๊ฐ์ ธ์จ๋ค
if (fp == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ \n");
exit(1);
}
printf("stdin -> disk \n"); //๋ฉ๋ชจ๋ฆฌ์ ์๋๊ฑธ ๋์คํฌ์ ๋ฃ๋๋ค
//2.ํ์ผ ์ ์ฅ
while ((ch = fgetc(fp)) != EOF) //EOF : ์คํ ์ข
๋ฃ (ctrl + z)
fputc(ch, stdout);
//3.์คํธ๋ฆผ ๋ซ๊ธฐ
fclose(fp);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE* in, * out; //ํ์ผํฌ์ธํฐ
//1.์คํธ๋ฆผ ์ฐ๊ฒฐ
in = fopen("fileio1.c", "rt");
if (in == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ(fileio1.c) \n");
exit(1);
}
out = fopen("fileio1.bak", "wt");
if (out == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ(fileio1.bak) \n");
exit(1);
}
//2.ํ์ผ๋ณต์ฌ
while ((ch = fgetc(in)) != EOF)
fputc(ch, out);
//3.์คํธ๋ฆผ ๋ซ๊ธฐ
fclose(in);
fclose(out);
printf("ํ์ผ๋ณต์ฌ. \n");
return 0;
}
//fileio1_3.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE* fp; //ํ์ผํฌ์ธํฐ
//1.์คํธ๋ฆผ ์ฐ๊ฒฐ
fp = fopen("file1.txt", "wt"); //๋ฌธ์์ด ์์๋ ์ด๋์ธ๊ฐ ์กด์ฌํ๋ ์ฃผ์๋ฅผ ๊ฐ์ ธ์จ๋ค
if (fp == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ \n");
exit(1);
}
printf("stdin -> disk \n"); //๋ฉ๋ชจ๋ฆฌ์ ์๋๊ฑธ ๋์คํฌ์ ๋ฃ๋๋ค
//2.ํ์ผ ์ ์ฅ
while ((ch = fgetc(stdin)) != EOF) //EOF : ์คํ ์ข
๋ฃ (ctrl + z)
fputc(ch, fp);
//3.์คํธ๋ฆผ ๋ซ๊ธฐ
//fclose(fp);
//๋ซ๊ณ ์ฐ๊ฒฐํ๋ ๊ณผ์ ์๋ต => freopen ์ฌ์ฉ ๊ฐ๋ฅ
//1.์คํธ๋ฆผ ์ฌ์ฐ๊ฒฐ
fp = freopen("file1.txt", "rt", fp); //๋ฌธ์์ด ์์๋ ์ด๋์ธ๊ฐ ์กด์ฌํ๋ ์ฃผ์๋ฅผ ๊ฐ์ ธ์จ๋ค
if (fp == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ \n");
exit(1);
}
printf("stdin -> disk \n"); //๋ฉ๋ชจ๋ฆฌ์ ์๋๊ฑธ ๋์คํฌ์ ๋ฃ๋๋ค
//2.ํ์ผ ์ ์ฅ
while ((ch = fgetc(fp)) != EOF) //EOF : ์คํ ์ข
๋ฃ (ctrl + z)
fputc(ch, stdout);
//3.์คํธ๋ฆผ ๋ซ๊ธฐ
fclose(fp);
return 0;
}
//fileio2.c
// ๋ฌธ์์ด ์
์ถ๋ ฅ
#include <stdio.h>
#include <stdlib.h> // exit ์ฌ์ฉ
int main()
{
char str[20] = "kingdom", tmp[20];
FILE *fp; // ํ์ผ ํฌ์ธํฐ ๋ณ์
/*fp = fopen("file2.txt", "wt"); //๋ฌธ์์ด ์์๋ ์ด๋์ธ๊ฐ ์กด์ฌํ๋ ์ฃผ์๋ฅผ ๊ฐ์ ธ์จ๋ค
if (fp == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ \n");
exit(1);
}
fputs(str, fp); // int fputs(char*,FILE*);//ํ์ผ๋ก ๋ฌธ์์ด์์ถ๋ ฅํ๋ค.
fclose(fp);
*/
fp = fopen("file2.txt", "rt"); //๋ฌธ์์ด ์์๋ ์ด๋์ธ๊ฐ ์กด์ฌํ๋ ์ฃผ์๋ฅผ ๊ฐ์ ธ์จ๋ค
if (fp == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ \n");
exit(1);
}
fgets(tmp, 20, fp); // fgets(str, sizeof(str), fp);
// ํ์ผํฌ์ธํฐ(fp)์ ์ฐ๊ฒฐ๋ ํ์ผ๋ก๋ถํฐ ์ฃผ์ด์ง ๋ฐ์ดํธ ์์๋ฐ๋ผ
// ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์์ tmp(์์๋ฐฐ์ด)๋ก ์ฃผ์ด์ง ๋ฐฐ์ด์ ์ ์ฅํ๋ค.
fclose(fp);
printf("tmp: %s \n", tmp);
return 0;
}
// ํ์ผ ์คํธ๋ฆผ ์์ฑ , ํ์ผ ์ด๊ธฐ๋ฅผ fopen() ํจ์๋ก ์ฒ๋ฆฌ => ํ์ผ ์
์ถ๋ ฅ์ ์ํ => fclose() ํจ์๋ก ํ์ผ ๋ซ๊ธฐ
//fileio2_2.c
#include <stdio.h>
#include <stdlib.h>
// ๋ฌธ์์ด ์ถ๋ ฅํ๊ธฐ
int main()
{
char tmp[100];
FILE* fp; //FILE ๊ตฌ์กฐ์ฒด
fp = fopen("C:\\Windows\\system.ini", "rt"); // `//` => ํน์๋ฌธ์ ํด์ ํ์ฌ ๊ฒฝ๋ก๋ช
์ง์
if (fp == NULL)
{
printf("C:\\Windows\\system.ini Open Error.");
exit(1);
}
while (1)
{
if (fgets(tmp, 80, fp) == NULL) //๋น ํ์ผ์ ์ฝ์์ ๊ฒฝ์ฐ fgetc๋ EOF๋ฅผ ์๋ฏธํ๋ -1์ ๋ฐํํ์ง๋ง, fgets๋ ๋ํฌ์ธํฐ๋ฅผ ๋ฐํํ๋ค.
break;
printf("%s ", tmp);
}
fclose(fp);
return 0;
}
//fileio2_3.c
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE* fp;
char str[5][20] = { "kingdom\n", "king\n", "queen\n", "prince\n", "princess\n" };
char tmp[20];
int i;
/*
fp = fopen("fileio2_3.txt", "wt");
if (fp == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ \n");
exit(1);
}
for (i = 0; i < 5; i++)
fputs(str[i], fp); // str[i]๋ ํ ์๋ฏธ, ํ ๋ณ ๋ฐ์ดํฐ ๊ฐ ์
๋ ฅ๋จ.
*/
fp = fopen("fileio2_3.txt", "rt");
if (fp == NULL)
{
printf("ํ์ผ ์ด๊ธฐ ์ค๋ฅ \n");
exit(1);
}
for (i = 0; i < 5; i++)
{
fgets(tmp, sizeof(str[i]), fp); // str[i]๋ ํ ์๋ฏธ, ํ ๋ณ ๋ฐ์ดํฐ ๊ฐ ์
๋ ฅ๋จ.
printf("%s", tmp);
}
fclose(fp);
return 0;
}
// 3000์ฃผ์ str[0] => `kingdom` `\n`(new line) `\0`
// 3020์ฃผ์ str[1] => `king` `\n(new line)` `\0`
// 3040์ฃผ์ str[2] => `queen` `\n(new line)` `\0`
// 3060์ฃผ์ str[3] => `prince` `\n(new line)` `\0`
// 3080์ฃผ์ str[4] => `princess` `\n(new line)` `\0`
#include <stdio.h>
#include <stdlib.h>
struct STUDENT {
char name[20];
int kor;
int eng;
int math;
float avg;
};
int main() {
FILE* fp;
struct STUDENT stu = { "kim", 50, 90, 62 }, tmp;
stu.avg = (stu.kor + stu.eng + stu.math) / 3.0;
//printf("%s, %d, %d, %d, %.2f\n", stu.name, stu.kor, stu.eng, stu.math, stu.avg);
fp = fopen("file3.txt", "wt");
if (fp == NULL) {
printf("Wrong Access.\n");
exit(1);
}
fprintf(fp, "%s %d %d %d %f", stu.name, stu.kor, stu.eng, stu.math, stu.avg);
fclose(fp);
fp = fopen("file3.txt", "rt");
if (fp == NULL) {
printf("Wrong Access.\n");
exit(1);
}
fscanf(fp, "%s %d %d %d %f", tmp.name, &tmp.kor, &tmp.eng, &tmp.math, &tmp.avg);
printf("%s %d %d %d %.2f", tmp.name, tmp.kor, tmp.eng, tmp.math, tmp.avg);
fclose(fp);
}