Thread: read from file into Array

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Question read from file into Array

    Hi I am actually a c++ developer but I am trying this prog in c.
    It is not completed but I am trying to read data from a file.
    There are3 columns seperated by spaces.
    I read the data from a file and each time a new row is read it must overwrite the data that was in the arrays.
    I am getting a segmentation fault.
    The compiler indicates that everything is running smooth until EOF is encountered.All the printf in the loop are printed but not those after the loop.

    Thanks for any advice
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    //look up if user exist in the sruct array which receive data from a file
    
    
    
    struct CellOwner{
      char name[30];
      char Sname[30];
      char msisdn[30];
         }owner1;
    
    struct CellOwner AllOwners[40];
    
    int main( )
     {
      
      char thename[30];
      char theSname[30];
      char themsisdn[30];
      int col=1;
      int a,i=0;
    
      char c;
      FILE *fp;
      fp = fopen("CellOwners.txt","r");    
      
      printf("\nEnter user name\n:%s");
      
      char *user="John";   
     
      while ((c=getc(fp))!=EOF)
      {
         
         if ((c!=' ')&&(c!='\n'))   //c is a character
         {
           if (col==1)
            thename[i++]=c;
            else
           if (col==2)
            theSname[i++]=c;
            else
           if (col==3)
             themsisdn[i++]=c;
             printf("\nc!=' ':%s");
          }
         
         else
         
         if (c==' ')  //einde van col=1 of col=2
         {
    
           if (col==1)
              thename[i]='\0';else
           if (col==2)
              theSname[i]='\0';
              ++col;
              i=0;
              printf("\nc=='   '\n:%s");
           }      
            
            else
            
            if (c=='\n' && col==3)//end of owner info
            {   
              printf("\nc=='End of line ':%s");
              themsisdn[i]='\0';
              printf("\ntheMsisdnTerminator\n%s");
              col=1;
              i=0;                                                                       //AllOwners[a++] =owner1;   
            }
             printf("\nEnd loop\n%s");
            
         } 
     
        
       printf("\nl-------l\n%s");
       themsisdn[i]='\0';
       printf("\n");
       return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    EOF cannot fit into a char. Use an int.


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

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Thanks quzah.I have done what you said ,but I am still getting the segmentation fault.

    Code:
    int c;
      FILE *fp;
      fp = fopen("CellOwners.txt","r");    
      
      printf("\nEnter user name\n:&#37;s");
    
    ---------------------------------------------
    
    
    End loop
    
    c!=' ':
    
    End loop
    
    c!=' ':
    
    End loop
    
    c!=' ':
    
    End loop
    Segmentation Fault (core dumped)

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well the variables that index arrays, like i, could use some range checking. Make sure it stays within the 0 - 29 range of your current arrays. It's very likely that you are indexing outside of the array, which would explain the seg fault.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    printf("\nEnter user name\n:&#37;s");
      
      char *user="John";
    What are you trying to do here exactly? If you include format specifiers %stuff, you have to have the appropriate arguements. Also, printf is for output, like cout, while scanf is its conterpart, like cin is to cout. (Well that's one method of input/output anyway.)


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

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    The program is not completed .I will modify the program later and alow a user name to be received from keyboard input.All I want to get working at this stage is to get data into arrays and then overwrite this data when a new row is encounterd.
    I will have a array of structs into which I will store this info before it is overwritten.

    I know the prog seems weird as there are easier ways to do this.I am busy learning the language so I am trying to include as many different things as I can.
    The printf is not giving the problem even if I remove it I get the same error.
    Thanks for the help.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    start counter at zero
    do
        read a character
        if newline
            counter = 0
        else
        if is not a space and isn't EOF
            put where it is supposed to go
            increment counter
    while character read isn't EOF
    Make sure you don't wanter off the ends of your arrays.


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

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Its runnin but

    My program is running but not the way it should.
    I am having some difficulty with reading a string from the keyboard.

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    //look up if user exist in the sruct array which receive data from a file
    
    
    
    struct CellOwner{
      char name[30];
      char Sname[30];
      char msisdn[30];
         }owner1;
    
    struct CellOwner AllOwners[40];
    
    int main( )
     {
      
      char thename[30];
      char theSname[30];
      char themsisdn[30];
      int col=1;
      int a,i=0;
    
      int c;
      FILE *fp;
      fp = fopen("CellOwners.txt","r");    
      
      
      while ((c=getc(fp))!=EOF)
      {
         
       if ((c!=' ')&&(c!='\n'))   //c is a character
         {
           if (col==1)
            thename[i++]=c;
            else
           if (col==2)
            theSname[i++]=c;
            else
           if (col==3)
             themsisdn[i++]=c;         
          }     
         else
       if (c==' ')  //einde van col=1 of col=2
         {
             if (col==1)
              {
                 thename[i]='\0';
                 strncpy(AllOwners[a].name,thename,i);
                 ++col;
              }
             
             else
             if (col==2)
              {
                 theSname[i]='\0';
                 strncpy(AllOwners[a].Sname,theSname,i);
                 ++col;
              }
            i=0; 
         }      
      else
        if (c=='\n' && col==3)//end of record
             {   
             
              themsisdn[i]='\0';
              strncpy(AllOwners[a].msisdn,themsisdn,i);
              col=1;
              i=0;
              a++ ;                                                           
             }
       }
         
           
       themsisdn[i]='\0';
       strncpy(AllOwners[a].msisdn,themsisdn,i);
       
        printf("%s","Enter the customer's name\n");
          //get name
       
       int customer;
    
      customer = getchar();
    
      while(customer != EOF)
        {
        putchar(customer);
       
        customer = getchar();
        }
      
      printf("%d",customer);
       
        int nm=0;
        for(nm=0;nm<20;nm++){
        if (AllOwners[nm].name==customer)
           printf("%d,%s",customer,"is in the List");
        }
      
       printf("\n");
        int j=0;
        for(j=0;j<20;j++)
        printf("%s,%s,%s\n",AllOwners[j].name ,AllOwners[j].Sname,AllOwners[j].msisdn);
       return 0;
      
     
    }
      
    
    /* gcc testp.c
     gcc testp.c -o testp
     testp*/
    This is what happen when I run it:

    -bash-3.00$ testp
    Enter the customer's name
    JohnJohn-1
    John,Smit,2772133143
    Piet,Botha,270192389
    Carl,vermak,12345890
    ,,
    ,,
    ,,

    It should indicate that John is in the list but it aint.Can anyone tell me why?

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    I am using ctrl+d for EOF

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, you are using a without initializing it.


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

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    ?
    int a,i=0;

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, you've declared it. Now where did you initialize it?


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 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. Read from file to an array
    By brooklyn1126 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 08:32 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM