Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char **argv) {
- FILE *fp;
- char line1[128], line2[128];
- fpos_t pos;
- if(argc < 2){
- printf("Usage : %s <file>", argv[0]);
- exit(0);
- }
- fp = fopen(argv[1], "r+");
- if (fp == NULL) {
- fprintf(stderr, "Cannot open file %s\n", argv[1]);
- return 1;
- }
- while (fgetpos(fp, &pos) == 0 &&
- fgets(line1, sizeof line1, fp) != NULL &&
- fgets(line2, sizeof line2, fp) != NULL) {
- fsetpos(fp, &pos);
- fputs(line2, fp);
- fputs(line1, fp);
- fflush(fp);
- }
- fclose(fp);
- return 0;
- }
Add Comment
Please, Sign In to add comment