---유틸리티 루틴---

문자열

복사 strcpy*, strncpy, strlncpy

연결 strcat*, strncat

비교 strcmp*, strncmp, strcasecmp, strncasecmp

탐색 strchr, strrchr, strstr

추출 strlen*, strpbrk, strtok, strtok_r, strspn, strcspn

 

---저수준 입출력 루틴---

파일열기와 닫기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fcntl.h>
#include <stdio.h>
int main()
{
//    char *fname="test.txt";
    char *fname="/etc/passwd";
    int fd;
    
    fd=open(fname,O_RDONLY); //test.txt 파일이 미리 존재해야 함
    if(fd<0)
        perror("open()");
    else{
        printf("Success!\nFilename:%10s\nDescriptor:%d\n",fname,fd);
        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

 

 

 

입력

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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
//    char *fname="test.txt";
    char *fname="/etc/passwd";
    int fd;
    int linecnt=0;
    char c;    
 
    if((fd=open(fname,O_RDONLY))<0) {
            perror("open()");
            exit(-1);
    }
 
    for( ; ;) {
            if(read(fd,&c,1)>0) {
                if(c=='\n') linecnt ++;
            } else break;
    }
 
    printf("Total line : %d\n",linecnt);
 
/*
    fd=open(fname,O_RDONLY);
    if(fd<0)
        perror("open()");
    else{
        printf("Success!\nFilename:%10s\nDescriptor:%d\n",fname,fd);
        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

 

 

+ Recent posts