Standard Files C Standard File Device Pointer Standard

  • Slides: 25
Download presentation

Standard Files • ���� C ��������� • ������� ������������� Standard File Device Pointer ������������

Standard Files • ���� C ��������� • ������� ������������� Standard File Device Pointer ������������ Standard stdin Keyboard input Standard output stdout Screen Standard error stderr Your screen https: //developer. apple. com/library/content/documentation/Device. Drivers/Conceptual/Writing

�������� (Direct input/output) ������� fread � ������� fwrite � #include <stdio. h> #include <string.

�������� (Direct input/output) ������� fread � ������� fwrite � #include <stdio. h> #include <string. h> int main(){ FILE *fp; char c[] = "this is tutorialspoint"; char buffer[100]; /* Open file for both reading and writing */ fp = fopen("file. txt", "w+"); /* Write data to the file */ fwrite(c, strlen(c) + 1, 1, fp); /* Seek to the beginning of the file */ fseek(fp, SEEK_SET, 0); /* Read and display data */ fread(buffer, strlen(c)+1, 1, fp); printf("%sn", buffer); fclose(fp); return(0); }

���������� (Unformatted input/output) fgetc fgetwc ���� wchar_t getc getwc fgets fgetws fputc fputwc ������

���������� (Unformatted input/output) fgetc fgetwc ���� wchar_t getc getwc fgets fgetws fputc fputwc ������ byte / wchar_t ������ / wchar_t �������������� / wchar_t ������� getchar getwchar ���� / wchar_t ��� stdin ������������������� gets N/A ������ (����� C 99 ������ C 11) putchar putwchar ����� / wchar_t ���� stdout fputs fputws

���������� (Formatted input/output) scanf fscanf sscanf vfscanf vsscanf printf fprintf snprintf vfprintf vsnprint f

���������� (Formatted input/output) scanf fscanf sscanf vfscanf vsscanf printf fprintf snprintf vfprintf vsnprint f wscanf fwscanf swscanf vfwscanf vswscanf perror ������� / wchar_t input ��� stdin, ����������� / wchar_t ���������������� wprintf fwprintf swprintf �������� / wchar_t ����� stdout, ���������� vwprintf vfwprintf vswprintf �������� / wchar_t ����� stdout ������������ N/A ������������ ��� stderr

�������� (Error handling) #include <stdio. h> clearerr ������������� feof ������������� ferror ������ int main

�������� (Error handling) #include <stdio. h> clearerr ������������� feof ������������� ferror ������ int main (){ FILE *fp; int c; fp = fopen("file. txt", "r"); if(fp == NULL) { perror("Error in opening file"); return(-1); } while(1) { c = fgetc(fp); if( feof(fp)) { break ; } printf("%c", c); } fclose(fp); return(0); } https: //fivera. net/hatom-feed-hatom-entry

�������� (Operations on files) remove ������ renam ������� int main (){ e int ret;

�������� (Operations on files) remove ������ renam ������� int main (){ e int ret; FILE *fp; ������� char filename[] = "file. txt"; fp = fopen(filename, "w"); tmpfile ���� fprintf(fp, "%s", "This is tutorialspoint. com"); fclose(fp); ret = remove(filename); tmpna ������� if(ret == 0) m ����� { #include <stdio. h> #include <string. h> printf("File deleted successfully"); } else { printf("Error: unable to delete the file"); } return(0); }

���� getchar() • �������������� • ������������ #include <stdio. h> • ���� getche() ��� getch()

���� getchar() • �������������� • ������������ #include <stdio. h> • ���� getche() ��� getch() int main (){ ������� char c; printf("Enter character: "); c = getchar(); printf("Character entered: "); putchar(c); return(0); } https: //www. tutorialspoint. com/cprogrammi ng/c_input_output. htm

���� gets() • ���� gets() ����������� ����� #include <stdio. h> int main() • ������

���� gets() • ���� gets() ����������� ����� #include <stdio. h> int main() • ������ { char str[50]; printf("Enter a string : "); gets(str); printf("You entered: %s", str); return(0); }

���� puts() • ���� puts() ����������� #include <stdio. h> #include <string. h> • ������

���� puts() • ���� puts() ����������� #include <stdio. h> #include <string. h> • ������ int main(){ char str 1[15]; char str 2[15]; strcpy(str 1, "tutorialspoint"); strcpy(str 2, "compileonline"); puts(str 1); puts(str 2); return(0); }

������� I/O • • Opening Files Closing a File Writing a File Reading a

������� I/O • • Opening Files Closing a File Writing a File Reading a File http: //na 5 cent. blogspot. com/2015/01/java-

������� -������� (Opening Files) #include <stdio. h> r - open for reading #include <stdlib.

������� -������� (Opening Files) #include <stdio. h> r - open for reading #include <stdlib. h> w - open for writing (file need not exist) a - open for appending (file need int main(){ not exist) FILE * fp; r+ - open for reading and writing, fp = fopen ("file. txt", "w+"); start at beginning w+ - open for reading and writing fprintf(fp, "%s %s %s %d", "We", (overwrite file) "are", "in", 2560); a+ - open for reading and writing (append if file exists) fclose(fp); return(0); }

������ (Writing a File) FILE *fp; • FILE *fp; fp=fopen("c: \test. bin", "wb"); •

������ (Writing a File) FILE *fp; • FILE *fp; fp=fopen("c: \test. bin", "wb"); • fp=fopen("c: \test. txt", "w"); char x[10]="ABCDEFGHIJ"; • fprintf(fp, "Testing. . . n"); fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp); • int fgetc (FILE *fp); • int fputc( int c, FILE *fp ); http: //www. cprogramming. com/

������ (Reading a File) FILE* fp; char buffer[255]; fp = fopen("file. txt", "r"); while(fgets(buffer,

������ (Reading a File) FILE* fp; char buffer[255]; fp = fopen("file. txt", "r"); while(fgets(buffer, 255, (FILE*) fp)) { printf("%sn", buffer); } fclose(fp);