files concept

transfernly's Avatar, Join Date: Dec 2009
Go4Expert Member
hi....i am writing a code which reads the contents of file....it has first name, lastname and phone number.....now i want ot read all the first names into one array second into another and phone numbers into third array....i tried but i could read only line by line.....please help out...my code is here


my output should be like this
Code:
array1:firstnames
array3:lastnames
array3:phonenumbers
Code:
#include <stdio.h> 
#include <string.h>


int main()
 {
  char c[50];  /* declare a char array */
  FILE *file;  /* declare a FILE pointer  */
  
  

  file = fopen("in.txt", "r"); 
 

  if(file==NULL)
 {
    printf("Error: can't open file.\n");
  
    return 1;
  }
  else {
    printf("File opened successfully. Contents:\n\n");
    
    while(fgets(c, 50, file)!=NULL) 
    { 
      
     printf("String: %s", c);   
    
     
    }

    printf("\n\nNow closing file...\n");
    fclose(file);
    return 0;
  }

Last edited by shabbir; 15Dec2009 at 20:04.. Reason: Code blocks
0
Gene Poole's Avatar, Join Date: Nov 2009
Contributor
Are all the fields (firstname, lastname, phonenumber) on a single line in the file? Are they separated by something (comma, space)? You may want to try fscanf().

I've modified your code slightly (with some of the original lines commented out):

Code:
#include <stdio.h>
#include <string.h>


int main()
{
  /* char c[50]; not needed */ /* declare a char array */
  char lname[50],fname[50],pnumber[50];  /* adjust these to the max possible */
  FILE *file; /* declare a FILE pointer */



  file = fopen("in.txt", "r");


  if(file==NULL)
  {
    printf("Error: can't open file.\n");

    return 1;
  }
  else {
    printf("File opened successfully. Contents:\n\n");

    /* while(fgets(c, 50, file)!=NULL) */
    while(fscanf(file,"%s %s %s\n",lname,fname,pnumber)!=EOF)
    {

      /* printf("String: %s", c); */
      printf("String: %s %s %s\n",lname,fname,pnumber );


    }

    printf("\n\nNow closing file...\n");
    fclose(file);
    return 0;
  } 
}
0
transfernly's Avatar, Join Date: Dec 2009
Go4Expert Member
thanks for u r help.....

but my solution should be like

it should print me

when i print fname array:

john
milan
anand

lname array:
peter
soman
raj

numbers array:
123456
678978
987654


here the file in.txt contains
john peter 123456
milan soman 678978
anand raj 987654



thanks in advance
0
Gene Poole's Avatar, Join Date: Nov 2009
Contributor
Then instead of the printf() in the while loop, fill in an array:

Code:
  /* adjust for max expected conditions: */
  fnamearr[10][50];
  lnamearr[10][50];
  phnumarr[10][50];

  int i,index;

/* ... */

    index=0;
    while(fscanf(file,"%s %s %s\n",lnamearr[index],fnamearr[index],pnumarr[index])!=EOF)
    {
      index++;
    }
    
    for(i=0;i<index;i++)
    {
      printf("Last name: %s\n",lnamearr[i]);
    }

    for(i=0;i<index;i++)
    {
      printf("First name: %s\n",fnamearr[i]);
    }

    for(i=0;i<index;i++)
    {
      printf("Phone: %s\n",phnumarr[i]);
    }
0
transfernly's Avatar, Join Date: Dec 2009
Go4Expert Member
thank you very much