files concept

Discussion in 'C' started by transfernly, Dec 15, 2009.

  1. transfernly

    transfernly New Member

    Joined:
    Dec 15, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Dec 15, 2009
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    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;
      } 
    }
    
     
  3. transfernly

    transfernly New Member

    Joined:
    Dec 15, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    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]);
        }
    
    
     
  5. transfernly

    transfernly New Member

    Joined:
    Dec 15, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    thank you very much
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice