Thread: help with reading files

  1. #1
    Unregistered
    Guest

    Question help with reading files

    Im trying to read from ONE file a list of a last name, first name, street address, and phone number (xxx-xxx-xxxx), instead of having to do it the way it is below. Any suggestions on how to read it into the program, and then print it, would be a great help. Thanks.

    #include<stdio.h>
    #include <string.h>
    #include <stdlib.h>
    struct node {

    char fname[25];
    char lname[25];
    char address1[25];
    char address2[25];
    char phone[25];
    char type[25];
    struct node *next;
    };
    void search(struct node *);
    //void show_list(struct node *);
    void lastNamesearch(char *);


    int main()
    {

    ////////HERE IS WHERE I NEED HELP, THANKS///////////////////////////
    FILE *firstName,*lastName,*address1,*address2,*phone,*t ype;
    struct node *pstart,*pprev,*pnow, *result;
    char lastname[25];
    char cont = 'Y';


    firstName=fopen("firstName.txt","w");
    lastName=fopen("lastName.txt","w");
    address1=fopen("address1.txt","w");
    address2=fopen("address2.txt","w");
    phone=fopen("phone.txt","w");
    type=fopen("type.txt","w");


    if(firstName==NULL)
    {printf("Error opening file");
    exit(0);}
    if(lastName==NULL)
    {printf("Error opening file");
    exit(0);}
    if(address1==NULL)
    {printf("Error opening file");
    exit(0);}
    if(address2==NULL)
    {printf("Error opening file");
    exit(0);}
    if(phone==NULL)
    {printf("Error opening file");
    exit(0);}

    fprintf(lastName," %s\n %s\n %s\n %s\n %s\n","Austin","Miller","Harris","Georgh","Gray");

    fprintf(firstName," %s\n %s\n %s\n %s\n %s\n","David","James","Troy","Noel","Brain");
    fprintf(address1," %s\n %s\n %s\n %s\n %s\n","417 N.Grant Street","213 S.Grant Street",
    "3123 college Avenue","2756 Third street",
    "2345 Tenth Street");

    fprintf(address2," %s\n %s\n %s\n %s\n %s\n","Bloomington, IN - 47405","Bloomington, IN - 47408",
    "Bloomington, IN - 47409","Bloomington, IN - 47403",
    "Bloomington, IN - 47405");
    fprintf(phone," %s\n %s\n %s\n %s\n %s\n","812-3359277","812-3359247","812-3355277",
    "812-3354277","812-3355275");
    fprintf(type," %s\n %s\n %s\n %s\n %s\n","Friend","Family","Business",
    "Friend","Family");



    fclose(lastName);
    fclose(firstName);
    fclose(address1);
    fclose(address2);
    fclose(phone);
    fclose(type);

    firstName=fopen("firstName.txt","r");
    lastName=fopen("lastName.txt","r");
    address1=fopen("address1.txt","r");
    address2=fopen("address2.txt","r");
    phone=fopen("phone.txt","r");
    type=fopen("type.txt","r");
    pstart=pprev=NULL; /*the list is empty*/


    while (!feof(lastName))
    { pnow = (struct node *)malloc(sizeof(struct node));
    if (pnow == NULL)
    printf("Unable to allocate memory");
    if (pstart == NULL)
    pstart = pnow;
    else
    pprev->next = pnow;
    fgets(pnow->lname,25,lastName);
    fgets(pnow->fname,25,firstName);
    fgets(pnow->address1,25,address1);
    fgets(pnow->address2,25,address2);
    fgets(pnow->phone,25,phone);
    fgets(pnow->type,25,type);
    pnow->next = NULL;
    pprev = pnow;
    ////////////THAT IS THE END, FOR NOW//////////////////////////////
    }

    search(pstart);
    //lastNamesearch(lastname);

    //printf("%s",lastname);
    //show_list(pstart);
    //result = (struct node *)malloc(sizeof(struct node));

    //result = search(pstart,lastname);
    //printf("%s",result->fname);

    return 0;

    }

    void search(struct node *ps)
    { struct node *p;
    char searchLName[25];
    p = ps;
    printf(" Please enter the Last name to be searched ");
    gets(searchLName);

    while (p!= NULL)
    { if ( strcmp( searchLName, p->lname )==0)
    printf("%s",p->fname);
    //return p;

    p = p -> next; }


    return ;//NULL;
    }



    /*void lastNamesearch(char searchLName[25])
    {


    printf(" Please enter the Last name to be searched ");
    gets(searchLName);

    return;

    }
    */
    void firstNamesearch(char searchFName[25])
    {



    printf(" Please enter the First name to be searched ");
    gets(searchFName);


    return;

    }

    /*
    void show_list(struct node *ps)
    { printf("Here is the list\n\n");
    do {
    printf("%s\n%s %s\n %s\n %s\n %s\n",ps->type,ps->lname,ps->fname,ps->address1,ps->address2,
    ps->phone);
    ps = ps->next;
    }while (ps != NULL);
    return;
    }
    */

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    If I understand you correctly. At the moment you have all of the fields split into several files... And you want to use one file instead?

    Write the records to the file using a new line for each record.
    ie
    Adams,John, Kent St
    Sarah,Mathews,Fleet St

    Read the file back a line at a time with fgets, into an array that contains the entire record. Then split that string into the sperate fields, using a tokenize function, either your own, or something like strtok.

    Search the board or the web for strtok if you need to see some code

    Bob's your uncle .
    PS. Use code tags.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM