bhupixb

File handling in C with spaces

Oct 3rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. #include <stdio.h>
  2. int main(){
  3. int n = 3, t;
  4. char a[100], b[100];
  5. scanf("%d", &n);
  6. fflush(stdin);
  7. // scanf("%[^\n]s", a);
  8. // printf("%s", a);
  9. FILE *fp;
  10.  
  11. fp = fopen("file.txt", "w");
  12. while(n--){
  13. fflush(stdin);
  14. scanf("%[^\n]s", a);
  15. //printf("%s\n", a);
  16. fprintf(fp, "%s", a);
  17. }
  18. // fprintf(fp, "%s", a);
  19. fclose(fp);
  20.  
  21. fp = fopen("file.txt", "r");
  22.  
  23. while(fscanf(fp, "%[^\n]s", b) != EOF){
  24. fflush(stdin);
  25. printf("%s\n", b);
  26. }
  27.  
  28. fclose(fp);
  29. return 0;
  30. }
Add Comment
Please, Sign In to add comment