Thread: How can I manage to file rewrite

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Unhappy How can I manage to file rewrite

    I have a program that consists of a parent and a number of child processes. Basically parent writes to a file, then first child reads and edits it, and writes to another file that is read and edited by second file. This goes on. Also last child writes to the file of first child. The idea is similar to a chain.

    However, I am having problem with rewriting. Generally the chain idea results in a number loops for children so that for each turn child needs to read and write. Reading is OK, but writing to same file again is problem. How can I solve that?

    This the part for child processes:

    Code:
    	// Run child processes
    	if(getpid() != pid) {
    		if(cid <= N%K)
    				turn++;
    		int myturn = 1;
    		
    		while(myturn <= turn) {
    			int in;
    			
    			// Open pipes
    			if(myturn == 1) {
    				mypipe[cid-1].streamr = fdopen (mypipe[cid-1].fd[READEND], "r");
    				mypipe[cid%K].streamw = fdopen (mypipe[cid%K].fd[WRITEEND], "w");
    			}
    
    			// First element is read
    			fscanf(mypipe[cid-1].streamr, "%d", &in);
    			
    			if(in == -1) {
    				myturn = turn + 1;
    			}
    			else {
    				printf("%d\n", in);
    				
    				int out = in;
    				while(out != -1) {
    					if(fscanf(mypipe[cid-1].streamr, "%d", &out))
    						printf("I am %d, read %d-%d from mypipe[%d]\n", getpid(), out, in, cid-1);
    
    					if(out % in != 0) {
    						// !!! - Problem is here - !!!
    						// Cannot write for the second time
    						fprintf(mypipe[cid%K].streamw, "%d\n", out);
    					}
    				}
    				
    				// Write -1 to the end
    				fprintf(mypipe[cid%K].streamw, "%d\n", -1);
    				fclose(mypipe[cid%K].streamw);
    
    				myturn++;
    			}
    		}
    		exit(0);
    	}
    	
    	return 0;

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Code:
    		while(myturn <= turn) {
    			int in;
    			
    			close(mypipe[cid-1].fd[WRITEEND]);
    			close(mypipe[cid%K].fd[READEND]);
    			
    			FILE *streamw, *streamr;
    			
    			if(myturn < 2) {
    				streamw = fdopen (mypipe[cid%K].fd[WRITEEND], "w");
    				streamr = fdopen (mypipe[cid-1].fd[READEND], "r");
    			}
    
    			fscanf(streamr, "%d", &in);
    			
    			if(in == -1) {
    				myturn = turn + 1;
    			}
    			else {
    				int out = in;
    				while(out != -1) {
    					if(fscanf(streamr, "%d", &out))
    						printf("I am %d, read %d-%d from mypipe[%d]\n", getpid(), out, in, cid-1);
    
    					if(out % in != 0) {
    						printf("I am %d, write %d to mypipe[%d]\n", cid, fprintf(streamw, "%d\n", out), cid%K);
    					}
    				}
    				fprintf(streamw, "%d\n", -1);
    				fclose(streamw);
    
    				myturn++;
    			}
    		}
    		
    		printf("%d terminating\n", cid);
    		exit(0);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2010, 12:05 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM