// created by Michael Levy 1997 #include #include #include #include #include #define SIZE 1025 /* This program takes text information from one file and then stores it in another file ,but by reverse way - I did it by introducing "stack" with functions: init_stack,push_stack,pop_stack which are standard in all stack programs. The stack itself is a linked list and any amount of lines you want ,but each line won't be more than 1024 characters. Before use of this program you should create two files (input ,output) it will also print to you the files on the screen */ void init_stack(); int pop_stack( char *ret_array ); int push_stack(FILE* folo); main(int argc,char* argv[]) // program takes arguments-files { // from DOS-shell like that for example: // adv2.exe test aaa //where, test -input file, aaa -output file //program works only on hard disk FILE *fp,*fp1; char msgbuf[1025]; int a,b; a=1; //help parameters b=1; clrscr(); if(argc!=3) { printf("You forgot to enter the file name\n"); exit(1); } if( (fp1=fopen(argv[1],"r"))==NULL ) { printf("cannot open reading file\n"); exit(1); } if((fp=fopen(argv[2],"w"))==NULL) { printf("cannot open writing file\n"); exit(1); } printf("The original file is:\n"); while(!feof(fp1)) { fgets(msgbuf,SIZE,fp1); if(!feof(fp1)) { fputs(msgbuf,stdout); } } rewind(fp1); init_stack(); while(b!=0) // in main I first take the input file { // by "push_stack function" and then b = push_stack(fp1); } // I put it in reverse order by pop_stack // function printf("The reversed file is:\n"); while(a) { a=pop_stack(msgbuf); fputs(msgbuf,fp); fputs(msgbuf,stdout); } fclose(fp); fclose(fp1); return(0); } struct node { char *message; struct node *next; }; struct node *head; // head of the linked list void init_stack() { struct node *n_pointer; //deallocate memory from stack elements for( n_pointer=head; n_pointer != (struct node *)NULL; ) { struct node *temp_pointer; free( n_pointer->message); temp_pointer=n_pointer->next; free( n_pointer ); n_pointer=temp_pointer; } head=(struct node *)NULL; printf("\nStack is initialized\n"); } int push_stack(FILE* folo) { char temp_message[SIZE]; struct node *n_pointer; n_pointer=(struct node *)calloc( 1,(unsigned)sizeof(struct node) ); if ( n_pointer == (struct node *)NULL ) { printf("Memory is full\n" ); return(0); } else { if( (fgets( temp_message,SIZE,folo)) != (FILE*) NULL ) { n_pointer->message= (char*) malloc(sizeof ( temp_message ) ); if (n_pointer->message !=(char *)NULL ) { strcpy( n_pointer->message,temp_message ); n_pointer->next = head; head = n_pointer; return(1); } else { printf( "Memory is full\n"); return(0); } } else { printf("End of file reached\n"); return(0); } } } int pop_stack(char *ret_array ) { if(head==(struct node *)NULL) { printf("Stack is empty\n"); strcpy(ret_array,""); return(0); } else { struct node *temp_pointer; strcpy(ret_array,head->message); temp_pointer=head->next; free(head->message); free( (char *)(head) ); head=temp_pointer; return(1); } } /* *****OUTPUT***** The original file is: The program - "stack reverse" created by : Levy Michael 22/4/98 12:15 Stack is initialized End of file reached The reversed file is: 22/4/98 12:15 Levy Michael created by : "stack reverse" The program - Stack is empty C:\PROGRAMS\TC\KIRILL>exit */