Open tonykang22 opened 2 years ago
#include <stdio.h> FILE * fopen (const char *path, const char *mode);
#include <stdio.h> int fclose (FILE *stream);
#include <stdio.h> FILE * fdopen (int fd, const char *mode);
#include <stdio.h> int fgetc (FILE *stream);
#include <stdio.h> int fputc (int c, FILE *stream);
#include <stdio.h>
int main(int argc, char argv[]) { FILE fp; int c;
if (argc < 2) { printf("Usage: %s <file>\n", argv[0]); return -1; } fp = fopen(argv[1], "r+"); if (!fp) { perror("fopen error: "); return -1; } while ((c = fgetc(fp)) != EOF) { if (c == '!' || c == '@' || c == '#' || c == '$' || c == '%' || c == '^' || c == '&' || c == '*' || c == '-' || c == '_') { if (ungetc(c, fp) == EOF) { perror("ungetc error: "); return -1; } if (ungetc(c, fp) == EOF) { perror("fputc error: "); return -1; } } } if (fclose(fp) == EOF) { perror("fclose error: "); return -1; } return 0;
}
<br> - 결과 <img width="700" alt="image" src="https://user-images.githubusercontent.com/86760744/180644402-90fdc465-ae70-4bfc-8c24-61bf4543bc93.png"> <br><br> ### fgets / fputs ``` c #include <stdio.h> char * fgets (char *str, int size, FILE *stream);
#include <stdio.h> int fputs (const char *str, FILE *stream);
#include <stdio.h> size_t fread (void *buf, size_t size, size_t nr, FILE *stream);
#include <stdio.h> size_t fwrite (void *buf, size_t size, size_t nr, FILE *stream);
int main(void) { FILE readfile, writefile;
struct address_info { unsigned int index; char name[32]; char phone_number[16]; char address[100]; }; struct address_info kim, who; kim.index = 1; sprintf(kim.name, "Kim"); sprintf(kim.phone_number, "010-1234-5678"); sprintf(kim.address, "Seoul"); writefile = fopen(FILENAME, "w"); if (!writefile) { perror("fopen error: "); return -1; } if (!fwrite(&kim, sizeof(struct address_info), 1, writefile)) { perror("fwrite error: "); return -1; } if (fclose(writefile)) { perror("fclose error: "); return -1; } readfile = fopen(FILENAME, "r"); if (!readfile) { perror("fopen error: "); return -1; } if (!fread(&who, sizeof(struct address_info), 1, readfile)) { perror("fread error: "); return -1; } if (fclose(readfile)) { perror("fclose error: "); return -1; } printf("index: %d\n", who.index); printf("name: %s\n", who.name); printf("phone number: %s\n", who.phone_number); printf("address: %s\n", who.address); return 0;
<br> - 결과 <img width="500" alt="image" src="https://user-images.githubusercontent.com/86760744/180644649-5809966a-f3b5-477b-8bff-c519d4f5381a.png">
08. 표준 I/O 라이브러리
표준 I/O 라이브러리란?
표준 I/O 라이브러리 개요
사용자 버퍼 입출력
fopen
fclose
fdopen
fgetc / fputc
fgetc / fputc 실습
int main(int argc, char argv[]) { FILE fp; int c;
}
fread / fwrite
fread / fwrite 실습
define FILENAME "./data.file"
int main(void) { FILE readfile, writefile;
}