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;
}