Thread: Appending content in the middle of a text file

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    3

    Smile Appending content in the middle of a text file

    Hi, I am doing a forum in C as my mini project. The idea is like any other forum. A user enters a query and other users reply to it. But it creates a problem here. After adding queries, if I wish to reply to query in between then it adds a redundant entry in file. 1st entry is the original query without reply and 2nd entry is the same query now with a reply added.
    I want only 1 entry of the query along with replies.

    Here is the code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include "var.c"
    int check_count();
    void add_query();
    void add_reply();
    void view_all();
    void main()
    {
    	int user_typ,op_typ;
    	clrscr();	
    	printf("\nWELCOME\n");	
    	
    	recsize=sizeof(q);
    	while(op_typ!=0)
    	{
    		printf("\n\n1.Add a new entry\n2.View all entries\n3.Reply to an entry\n0.Exit");
    		printf("\nEnter your choice here");
    		scanf("%d",&op_typ);
    		switch(op_typ)
    		{
    			case 1:
    			{
    				add_query();
    
    
    			}
    			break;
    
    
    			case 2:
    			{
    				view_all();
    			}
    			break;
    
    
    			case 3: add_reply();
    			break;
    		}
    
    
    	}
    }
    
    
    //Check the count of query to be posted
    int check_count()
    {
    	FILE *qp;
    	qp=fopen("queries.txt","rb+");
    	while(fread(&q,sizeof(q),1,qp)==1)
    	{
    	}
    	return q.qid+1;
    }
    
    
    //add a new query
    void add_query()
    {
    	FILE *fp;
    	int query_no,u_cat;
    	fp=fopen("queries.txt","ab+");
    	if(fp==NULL)
    	{
    		printf("\nError in creating/opening file");
    	}
    	query_no=check_count();
    	q.qid=query_no;
    	printf("\nEnter query no. %d: ",q.qid);
    	fflush(stdin);
    	gets(q.question);
    	printf("\nselect which category you belong to:\n1.Student\n2.Professor\n3.Expert");
    	printf("\nEnter your input here: ");
    	scanf("%s",q.user_cat);
    	fseek(fp,0,SEEK_END);
    	fwrite(&q,sizeof(q),1,fp);
    	fclose(fp);
    	printf("\nYou entered: %s ",q.question);
    	printf("\nCategory: %s ",q.user_cat);
    }
    
    
    //Display queries posted
    void view_all()
    {
    	FILE *fp;
    	fp=fopen("queries.txt","rb+");
    	clrscr();
    	if(fp==NULL)
    		printf("\nCannot open file");
    	while(fread(&q,sizeof(q),1,fp)==1)
    		printf("\nQuery No.%d\nQuery: %s\nCategory: %sReply: %s\n\n",q.qid,q.question,q.user_cat,q.reply);
    	fclose(fp);
    }
    
    
    void add_reply()
    {
    	FILE *fp,*fr;
    	char another,ch;
    	int query_id;
    	another='Y';
    	fp=fopen("queries.txt","a+");
    	fr=fopen("replies.txt","w+");
    	if(fp==NULL)
    	{
    		printf("\nCannot open file");
    	}
    	if(fp==NULL)
    		printf("\nCannot open file");
    	while(another=='Y')
    	{
    		printf("\nEnter the query no. to which you wish to add a reply: ");
    		scanf("%d",&query_id);
    		while(fread(&q,sizeof(q),1,fp)==1)
    		{
    			if(q.qid==query_id)
    			{
    				printf("\nQuery: %s",q.question);
    				printf("\nEnter your reply here: ");
    				fflush(stdin);
    				gets(q.reply)                         //The problem is here. Adds duplicate entries
    				fseek(fp,- sizeof(q),SEEK_CUR);
    
    
    				fwrite(&q,sizeof(q),1,fp);
    				break;
    			}
    		}
    		printf("\nAdd another reply?(Y/N)");
    		fflush(stdin);
    		another=getche();
    	}
    }
    This is the file var.c where I have declared the structure:
    Code:
    #include<stdio.h>
    #include<conio.h>
    struct query
    	{
    		int qid;
    		char question[3000];
    		char user_cat[20];
    		char reply[50][500];
    	};
    	struct query q;
    	long int recsize;
    Kindly advise.
    Thanx

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Maybe read K&R2, do the exercises, then start over from scratch?

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    How would you do it on paper? Learn to think, come up with a solution and then turn your solution into "code".

  4. #4
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    first thing to do dont use fgets and fflush

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. do not include c files
    2. FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    3. increase your warning level - on line 16 you are using not0initialized variable
    4. after line 19 you need fflush(stdout) since the line is not ended with \n - it is possible you will not see the output immediately
    5. do not use global variables without need.
    6. FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    7. FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    8. FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com
    9. Post minimal compilable example illustrating the problem - where is too much code not related to it.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    Thanx. But I am still confused regarding the appending at specific position to a file concept.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The issue is that you need to insert text into the middle of a file, not append to the file. You may want to start off by trying to do this with a large ram buffer that contains the entire text file.

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    Is there a way to insert data in between without the exact byte of insertion?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can't "insert data in between" you can only replace existing data. If you want to insert something you will need to create a new file, write the information that you want before your "new" data then write your "new" data followed by the rest of the data the original file contained.

    Jim

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What do you mean by "byte of insertion"?

    Have you tried:

    - Open existing file ("file 1")
    - Create new file ("file 2")
    - While there's no line to insert, copy line from "file 1" to "file 2"
    - When there is a line to insert, add that line to "file 2"
    - While not at the end of file, copy line from "file 1" to "file 2"
    - Close "file 1" and "file 2"
    - Delete "file 1"
    - Rename "file 2" to "file 1"

    You could also read the entire file into memory, add the new line where it belongs, and overwrite the existing file with the updated data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. appending a charecter n the middle of a string
    By shivam1992 in forum C Programming
    Replies: 13
    Last Post: 12-12-2011, 06:02 PM
  2. Appending Random Text File.
    By jackirl in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2011, 04:15 PM
  3. appending text to existing text file
    By kpax in forum C Programming
    Replies: 8
    Last Post: 05-28-2008, 08:46 AM
  4. Appending to a text file
    By mike_g in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2007, 10:35 AM
  5. appending to/reading from text file
    By BR7 in forum C Programming
    Replies: 5
    Last Post: 02-22-2002, 05:02 PM