-5

I have been working on the following bit of code and am having trouble with my file handling operations.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
    int lineCount=0;
    char line[200];
    char *lineArray[lineCount];
    char *CityName[20];
    double longitudinal[10];
    double latitudinal[10];
    int serialno[10];
    char *token;
    const char j=' ';
    int x=0,p=0;
    FILE *file;

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

    if(file==NULL)
    {
        printf("file is not opened properly\n");
        return -1;
    }

    //below one to give total number of lines.
    while ((fgets(line,sizeof(line),file)) != NULL) 
    {
        lineCount++;
    }  

    lineArray = (char *)malloc(sizeof(char *)*lineCount);
    rewind(file);
    printf("The total number of cities in the file is: %d\n",(lineCount-1));

    fgets(line,sizeof(line),file);//moves file pointer to beg of 2nd line
    while ((fgets(line,sizeof(line),file)) != NULL) 
    {
        lineArray[p]=malloc(strlen(line));//1st bunch of memory allocated
        strcpy(lineArray[p],line);
        printf("%s\n",lineArray[p]);
        token = strtok(lineArray[p],j);

        //printf("%s\n",token);
        serialno[p]=atoi(token);
        printf("%d\n",serialno[p]);

        x=1;

        /* walk through other tokens */
        while( token != NULL ) 
        {
            //printf( " %s\n", token );

            if((x%4)==1)
            {
                //longitudinal[p] =malloc(strlen(token));
                longitudinal[p] =atof(token);
            }
            else if((x%4)==2)
            {
                //latitudinal[p]=malloc(strlen(token));
                latitudinal[p]=atof(token);
            }
            else if((x%4)==3)
            {
                CityName[p] = malloc(strlen(token));
                strcpy(CityName[p],token);
                printf("%s\n",CityName[p]);    
            }

            token = strtok(NULL, j);
            x++;
        }   //end of inner while 

        p++;

    }//end of outer while
}//end of main

The file that I am using is:

City_No   Latitude  Longitude  City_Name

1         12.58      77.38     Bangalore

2         14.18      74.55     JogFalls

3         15.09      76.55     Bellary

4         26.48      84.33     Bettiah

5         25.37      85.13     Patna

6         19.18      84.51     Berahampur

7         20.15      85.51     Bhuvneshwar

8         25.30      90.30     Shillong

The problem is that I have been trying this for the past few days and I keep getting errors. I am not getting anywhere with debugging and cannot figure out where I am going wrong.

embedded_guy
  • 1,939
  • 3
  • 24
  • 39
  • 3
    Please format your code properly, and try to ask a specific question, preferably using a *minimal* example that illustrates the problem. – Paul R Jun 09 '14 at 09:23
  • 1
    Then what's your question? Someone to debug your code (and proper format your question too)? Please narrow to a more specific issue... – Adriano Repetti Jun 09 '14 at 09:24
  • 1
    Please give clear description of what you are trying to do, where your code is going wrong and also format code so that it is readable. – Kartik_Koro Jun 09 '14 at 09:24
  • i m new to tis site and i think above 1 is understandable – VishalTejaswi Jun 09 '14 at 09:26
  • what o/p i want is from file read values and store in 3 different arrays.1 for city name,another for latitude and last 1 for longitudes.I m getting error in this line-- lineArray = (char *)malloc(sizeof(char *)*lineCount); – VishalTejaswi Jun 09 '14 at 09:32

1 Answers1

1
  • The array is better declared and allocated like this:

    char **lineArray;.

    lineArray = (char **)malloc(sizeof(char *) * (lineCount-1));

  • strtok take a string as second argument.

    Replace const char j=' '; by const char *j=" ";

  • Get rid of the first strtok:

    lineArray[p]=malloc(strlen(line));//1st bunch of memory allocated
    strcpy(lineArray[p],line);
    printf("%s\n",lineArray[p]);
    //token = strtok(lineArray[p],j);
    //printf("%s\n",token);
    //serialno[p]=atoi(token);
    //printf("%d\n",serialno[p]);
    x=1;
    
darkundar
  • 26
  • 3
  • Please [don't cast the result of malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Paul R Jun 09 '14 at 12:20