줄 단위 입력

#include <stdio.h>

char fgets(char *buf, int n, FILE *fp);

char *gets(char *buf);

fgets() 주어진 스트림 (fp)로 부터 입력

gets() 표줜 입력으로부터 문자열을 읽어 들인다.

 

줄 단위 출력

#include <stdio.h>

int fputs(const char *str,f FILE *fp);

int puts(const char *str);

성공하면 출력한 문자의 수, 실패하면 -1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
 
int main(int argc,char *argv[])
{
    FILE *fp;
    char buf[256];
    if((fp=fopen(argv[1],"w+"))==NULL) {
        perror("fopen error");
        return -1;
    }
 
    fputs("Inputs String >>",stdout);
    gets(buf);
    fputs(buf,fp);
    rewind(fp);
    fgets(buf,sizeof(buf),fp);
    puts(buf);
    close(fp);
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
 
int main(int argc,char **argv)
{
    FILE *in, *out;
    char line[BUFSIZ];
 
    if(argc !=3)
    {
        fprintf(stderr, "Usage: append-line file1 file2\n");
        return -1;
    }
 
    if((in=fopen(argv[1],"r"))==NULL) {
        perror("argv[1]");
        return -1;
    }
 
    if((out=fopen(argv[2],"a"))==NULL){
        perror(argv[2]);
        return -1;
    }
 
    while(fgets(line,sizeof(line),in) != NULL) fputs(line,out);
 
    fclose(out);
    fclose(in);
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

버퍼기반 입출력

#include <stdio.h>

size_t fread (void *ptr, size_t size, size_t nobj, FILE *fp);

size_t fwrite (const void *ptr, size_t size, size_t nobj, FILE *fp);

지정된 스트름에서 이진 구조체를 쓰거나 읽는다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
 
struct student{
    int sno;
    char name[10];
    int point;
};
 
int main()
{
    struct student s1,s2;
    FILE *fp;
 
    if((fp=fopen("student.dat","w"))==NULL){
        perror("fopen error");
        return -1;
    }
 
    printf("Input SNO >>"); scanf("%d",&s1.sno);
 
    printf("Input Name >>"); scanf("%s",&s1.name);
    
    printf("Input Point >>"); scanf("%d",&s1.point);
 
    if(fwrite(&s1,sizeof(struct student),1,fp)!=1){
        perror("frwite error");
        return -1;
    }
 
    if(fread(&s2,sizeof(struct student),1,fp)!=1){
        perror("fread error");
        return -1;
    }
 
    printf("\nNO\tName\tPoint\n");
    printf("=====================================\n");
    printf("%d\t%s\t%d\n",s2.sno,s2.name,s2.point);
 
    close(fp);
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
 
int main(int argc,char **argv)
{
    int n;
    FILE *in, *out;
    char buf[BUFSIZ];
 
    if(argc !=3){
        fprintf(stderr,"Usage: append-line file1 file2\n");
        return -1;
    }
 
    if((in=fopen(argv[1],"r"))==NULL) {
        perror(argv[1]);
        return -1;
    }
 
    if((out=fopen(argv[2],"a"))==NULL){
        perror(argv[2]);
        return -1;
    }
 
    while((n=fread(buf,sizeof(char),BUFSIZ,in))>0)
        fwrite(buf,sizeof(char),n,out);
 
    fclose(out);
    fclose(in);
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

읽기/쓰기 오프셋 재배치

오프셋 변경

#include <stdio.h>

long ftell (FILE *fp);

int fseek (FILE *fp, long offset, int whence);

void rewind (FILE *fp);

ftell() 파일의 현재 오프셋을 반환

fseek() 파일의 오프셋을 변경

rewind() 파일의 오프셋을 처음으로 이동

 

파일과 디렉토리

파일 속성 구하기

 

심볼릭 링크에서 정보 얻기

#include <unistd.h>

int readlink(const char *pathname, char *buf, int bufsize);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
 
int main()
{
    char buf[200];
    int n;
 
    n=readlink("/lib/libc.so.6",buf,200);
    buf[n]='\0';
 
    printf("%s\n",buf);
 
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs
불러오는 중입니다...

 

 

파일 디렉토리 생성과 삭제

파일 삭제

#include <unistd.h>

int unlink (const char *pathname);

unlink()는 디렉토리 항목을 지우며, 파일의 연결 계수를 1만큼 감수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
 
int main(void)
{
    int fd,len;
    char buf[20];
    if((fd=open("tempfile",O_RDWR | O_CREAT | O_TRUNC, 0666))
            <0){
        perror("open error");
        return -1;
    }
 
    unlink("tempfile");
 
    if(write(fd,"How are you>",12)!=12){
        perror("write error");
        return -1;
    }
 
    lseek(fd,0L,SEEK_SET);
    if((len=read(fd,buf,sizeof(buf)))<=0){
        perror("read error");
        return -1;
    } else
        buf[len]='\0';
 
    printf("%s\n",buf);
    close(fd);
 
    if((fd=open("tempfile",O_RDWR))<0){
        perror("Second open error");
        return -1;
    }
    else
        close(fd);
 
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

파일 삭제

int remove (const char *pathname);

파일의 연결 계수를 감소 시킴

pathname이 파일이면 unlink()와 같은 동작, 디렉토리면 rmdir()과 같은 역할

 

디렉토리 생성 및 삭제

#include <sys/types.h>

#include <sys/stat.h>

int mkdir (const char *pathname, mode_t mode);

새로운 디렉토리를 생성, 성공하면 0 실패하면 -1 반환

 

#include <unistd.h>

int rmdir (const char *pathname);

비어 있는 디렉토리를 삭제 ( 연결 계수 1 감소) 성공하면 0, 실패하면 -1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
 
int main()
{
    char buffer[256];
 
    getcwd(buffer,256); printf("%s\n",buffer);
    mkdir("apple",0755); chdir("apple");
    getcwd(buffer,256); printf("%s\n",buffer);
    chdir("..");
 
    rmdir("apple");
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

+ Recent posts