Thread: files+structs problem in the printing...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    81

    files+structs problem in the printing...

    I' ll be glad if anyone could help...my problem is the following :

    Question_1:
    when i run the program i press 1 see some stats, then i press 2 so, i typed name, email and telephone.
    after that i typed again 1 and doesn' t respond. So in the 2nd if i closed and opened the infile...i am not sure if it is ok, but i runs as i wish...is it correct?

    Question_2:
    having the r+ mod in the fclose fuction allows you to read and write and NOT append. Append means write something in the end of the file, whereas write erases everything into the file.txt and writes again in the begining. In this program everything into the file.txt does not erase...why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct Details{
    	char l_name[81];
    	char f_name[81];
    	char email[81];
    	int tel;
    };
    
    int main(void){
    	FILE *infile;
    	struct Details det;
    	int cont_num, cont_stats, ch;
    	
    		infile=fopen("lala.txt","r+");
    		if(infile==NULL){
    			printf("file didn' t found.\n");
    			exit(1);
    		}
    			printf("file found.\n");
    
    		fscanf(infile,"%d %d", &cont_num, &cont_stats);
    		printf("There are %d contacts.\n", cont_num);
    		printf("There are %d statistics in every contact.\n", cont_stats);
    
    		printf("To just see the contacts type '1'.\n");
    		printf("To apend them type '2',\n");
    		printf("To exit type '0'.\n");
    		scanf("%d", &ch);
    		do{
    			if(ch==1){
    				while(fscanf(infile,"%s %s %s %d", det.f_name, det.l_name, det.email, &det.tel)!=EOF){
    					printf("first name: %s\tlast name: %s\te-mail: %s\ttelephone: %d\t\n", det.f_name, det.l_name, det.email, det.tel);
    				}
    			}else if(ch==2){
    					printf("Type your first name:\n");
    						scanf("%s", det.f_name);
    					printf("Type your last name:\n");
    						scanf("%s", det.l_name);
    					printf("Type your e-mail:\n");
    						scanf("%s", det.email);
    					printf("Type your telephone:\n");
    						scanf("%d", &det.tel);
    					fprintf(infile,"%s %s %s %d", det.f_name, det.l_name, det.email, det.tel);
    
    					fclose(infile); // close the file and ->
    					infile=fopen("lala.txt","r+"); // open again the file....
    			}else{
    				printf("\nThank you for choosing my program.\n\n");
    				exit(0);
    			}
    			
    		printf("To just see the contacts type '1'.\n");
    		printf("To apend them type '2',\n");
    		printf("To exit type '0'.\n");
    		scanf("%d", &ch);		
    		}while(ch!=0); 
    		
    	return 0;
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) Check what happens during your last scanf in the do while loop. What is actually being read in ch?

    2) I think you want a+ not r+. Check out the differences:
    fopen - C++ Reference
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by claudiu View Post
    2) I think you want a+ not r+. Check out the differences:
    fopen - C++ Reference
    you are correct....it needs a+, for read and apend.

    Everytime it says "write" in the page, it means: delete everything in the file and add the things you type?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by nullifyed View Post
    you are correct....it needs a+, for read and apend.

    Everytime it says "write" in the page, it means: delete everything in the file and add the things you type?
    Yes, read what it says under "w". It explains what it means by "writing" in the rest of the file.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    81
    "Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "
    this is what it says. But my file already exists and it adds in the file what i type....whats wrong?

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I guess whenever option 1 is selected, you want to print everything from the file and for option 2, append to file?
    You don't need to close and re-open. Take a look at rewind() function.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by Bayint Naung View Post
    I guess whenever option 1 is selected, you want to print everything from the file and for option 2, append to file?
    You don't need to close and re-open. Take a look at rewind() function.
    I did a small research and it is just rewind(FILE *fp);...
    but in my case the problem is that i have the mode r+ and reads+apends....does not reads+writes as it should!!
    Last edited by nullifyed; 06-20-2010 at 10:29 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why haven't you figured out how to use a search engine yet?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structs and classes problem!!!!
    By faisal791 in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2010, 02:14 PM
  2. Basic problem structs
    By delete6145 in forum C Programming
    Replies: 1
    Last Post: 11-27-2007, 09:43 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. C: include files problem
    By threahdead in forum Linux Programming
    Replies: 4
    Last Post: 05-07-2004, 08:02 AM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM