■ fgetpos
파일 스트림에서 현재 위치를 구한다
■ fsetpos
파일 스트림에서 현재 위치를 설정한다
■ ftell
스트림에서 현재 파일 오프셋을 반환한다
■ rewind
스트림에서 파일 위치를 재설정한다
■ setvbuf
스트림을 위한 버퍼링 구성을 설정한다
■ remove
path 파라미터가 디렉토릭 아니라면 unlink와 같고, 디렉토리이면 rmdir과 같다
■ freopen()
- 스트림을 다른 파일이나 방식(mode)로 다시 연다
- 참고 링크 : http://itguru.tistory.com/59
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char *filename;
if(argc != 2) {
fprintf(stderr, "usage: useupper file\n");
exit(1);
}
filename = argv[1];
/* That done, we reopen the standard input, again checking for any errors as we do so,
and then use execl to call upper. */
if(!freopen(filename, "r", stdin)) {
fprintf(stderr, "could not redirect stdin to file %s\n", filename);
exit(2);
}
execl("./upper", "upper", 0); or getchar(); 를 하게되면, 리다이렉션 된 stdin은 text파일을 읽게 됨
/* Don't forget that execl replaces the current process;
provided there is no error, the remaining lines are not executed. */
fprintf(stderr, "could not exec upper!\n");
exit(3);
}
■ int ferror(FILE *stream)
에러 확인
■ int feof(FILE *stream)
스트림의 끝인지 확인
■ void clearerr(FILE *stream)
마지막 지시자와 에러 지시자 초기화
■ int fileno(FILE *stream)
파일 스트림을 파일 디스크립터로 전환
■ FILE *fdopen(int fildes, const char *mode);
디스크림터를 이용하여 파일 스트림 생성
■ int unlink(const char *path)
링크카운트 감소로 0이면, 파일 제거
■ int link(const char *path1, const char *path2);
■ int symlink(const char *path1, const char *path2);
■ int chdir(const char *path);
cd명령처럼 path로 이동
■ char *getcwd(char *buf, size_t size);
현재 디렉토리의 이름을 주어진 버퍼에 저장
디렉터리 검색하기
■ DIR *opendir(const char *name)
■ struct dirent *readdir(DIR *dirp)
■ long int telldir(DIR *dirp)
■ void seekdir(DIR *dirp, long int loc)
■ int closedir(DIR *dirp)
/* We start with the appropriate headers and then a function, printdir,
which prints out the current directory.
It will recurse for subdirectories, using the depth parameter is used for indentation. */
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/* Found a directory, but ignore . and .. */
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
printf("%*s%s/\n",depth,"",entry->d_name);
/* Recurse at a new indent level */
printdir(entry->d_name,depth+4);
}
else printf("%*s%s\n",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
/* Now we move onto the main function. */
int main(int argc, char* argv[])
{
char *topdir, pwd[2]=".";
if (argc != 2)
topdir=pwd;
else
topdir=argv[1];
printf("Directory scan of %s\n",topdir);
printdir(topdir,0);
printf("done.\n");
exit(0);
}
메모리맵
■ void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
- prot
PROT_READ: The segment can be read
PROT_WRITE: The segment can be written
PROT_EXEC: The segment can be executed
PROT_NONE: The segment can’t be accessed
- flags
MAP_PRIVATE: The segment is private, changes are local
MAP_SHARED: The segment changes are made in the file
MAP_FIXED: The segment must be at the given address, addr(사용되지않음)
■ int msync(void *addr, size_t len, int flags)
- flags: 갱신을 수행하는 방법
MS_ASYNC Perform asynchronous writes
MS_SYNC Perform synchronous writes
MS_INVALIDATE Read data back in from the file
■ int munmap(void *addr, size_t len)
/* We start by defining a RECORD structure
and then create NRECORDS versions each recording their number.
These are appended to the file records.dat. */
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
typedef struct {
int integer;
char string[24];
} RECORD;
#define NRECORDS (100)
int main()
{
RECORD record, *mapped;
int i, f;
FILE *fp;
fp = fopen("records.dat","w+");
for(i=0; i<NRECORDS; i++) {
record.integer = i;
sprintf(record.string,"RECORD-%d",i);
fwrite(&record,sizeof(record),1,fp);
}
fclose(fp);
/* We now change the integer value of record 43 to 143
and write this to the 43rd record's string. */
fp = fopen("records.dat","r+");
fseek(fp,43*sizeof(record),SEEK_SET);
fread(&record,sizeof(record),1,fp);
record.integer = 143;
sprintf(record.string,"RECORD-%d",record.integer);
fseek(fp,43*sizeof(record),SEEK_SET);
fwrite(&record,sizeof(record),1,fp);
fclose(fp);
/* We now map the records into memory
and access the 43rd record in order to change the integer to 243
(and update the record string), again using memory mapping. */
f = open("records.dat",O_RDWR);
mapped = (RECORD *)mmap(0, NRECORDS*sizeof(record),
PROT_READ|PROT_WRITE, MAP_SHARED, f, 0);
mapped[43].integer = 243;
sprintf(mapped[43].string,"RECORD-%d",mapped[43].integer);
msync((void *)mapped, NRECORDS*sizeof(record), MS_ASYNC);
munmap((void *)mapped, NRECORDS*sizeof(record));
close(f);
exit(0);
}