appending to/reading from text file

This is a discussion on appending to/reading from text file within the C Programming forums, part of the General Programming Boards category; ok i have to do a program with a menu, one option is to add a name to the list(in ...

  1. #1
    BR7
    BR7 is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    12

    appending to/reading from text file

    ok i have to do a program with a menu, one option is to add a name to the list(in a file), the second part is to list all names added. (a phonebook of sorts)that's the basics, i can write the names to the file fine, but i can't get them to be printed out, i'd appreciate any help.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    char lname[100000], fname[100000], num[100000];
    void entry(void);
    void list(void);
    int count=0, i=0;
    FILE *fptr;

    void main()
    {
    int choice=0, x=0;
    fptr=fopen("phonebook.dat", "a+");
    if(fptr==NULL){
    printf("error");
    exit(1);
    }
    while(x==0){
    printf("1.Enter a name and number.\n");
    printf("2.List all entries.\n");
    printf("3.Exit.\n");
    scanf("%d", &choice);

    switch(choice){
    case 1:
    entry();break;
    case 2:
    list(); break;
    case 3:
    x=1;
    fclose(fptr);break;
    }/*end switch*/
    }/*end while*/
    }/*end main*/

    void entry()
    {
    printf("Enter last name ");
    scanf("%s", &lname);
    printf("\nEnter first name ");
    scanf("%s", &fname);
    printf("\nEnter phone number ");
    scanf("%s",&num);
    fprintf(fptr, "%s, %s, %s\n", lname, fname, num);
    }

    void list()
    {
    /****************main problem(i think)******************/
    FILE *out_fptr;
    char string[100];
    out_fptr=fopen("phonebook.dat", "r");
    while(!feof(out_fptr)){
    fscanf(out_fptr, "%s", &string);
    if(feof(out_fptr))break;
    printf("%s", string);
    }
    }
    wasn't so sure about the out_fptr, that was just a guess cus it didn't work before
    Last edited by BR7; 02-22-2002 at 12:40 AM.

  2. #2
    Unregistered
    Guest
    Use this in your list function instead of fscanf. The problem with fscanf is that it will only read text up until it reaches a whitespace or newline, unless you use special delimiters.

    char sbuf[100];

    while ( fgets(sbuf,100,out_fptr) != NULL) printf("%s",sbuf);

  3. #3
    0x01
    Join Date
    Sep 2001
    Posts
    88

    don't touch your that part of your code( list() )... you missed something.

    Your problem lies in 'entry()'
    ----

    Code:
    void entry() 
    { 
    	printf("Enter last name "); 
    	scanf("%s", &lname); 
    	
    	printf("\nEnter first name "); 
    	scanf("%s", &fname); 
    	
    	printf("\nEnter phone number "); 
    	scanf("%s",&num); 
    	
          // You forgot to re- assign 'fptr' ... your in a diff. function
    	 fptr = fopen("phonebook.dat","a"); // add this
             if(!fptr)
                   printf("\n Error : Opening File");
    
    	fprintf(fptr, "%s, %s, %s\n", lname, fname, num); 
    	fclose(fptr); // and this
    }
    And you should take,
    Code:
    	fptr=fopen("phonebook.dat", "a+"); 
    	if(fptr==NULL)
    	{ 
    		printf("error"); 
    		exit(1); 
    	}
    out of main().

    weee!
    Last edited by knave; 02-22-2002 at 02:00 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no need to take anything out of main. They're using a global file pointer, so the second isn't needed. All you have to do is use rewind() to position the file pointer back to the beginning, and then display its contents.

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

  5. #5
    BR7
    BR7 is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    i'd never used rewind before, but that makes sense, thanks alot guys

  6. #6
    0x01
    Join Date
    Sep 2001
    Posts
    88
    I wasn't saying anything "NEEDS" to be taken out of main(), its just pointless to have this in main() if he already has the same thing in the entry() function....

    Code:
    	fptr=fopen("phonebook.dat", "a+"); 
    	if(fptr==NULL)
    	{ 
    		printf("error"); 
    		exit(1); 
    	}
    Last edited by knave; 02-22-2002 at 05:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21