Thread: Number of lines in a text file

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    53

    Number of lines in a text file

    Hi all,
    I'm having to write a program in C because I need to use some numerical libraries.

    I need to read a text file which contain x number of lines with each line having two doubles. For testing I had a dummy file which I knew the length of and hard coded into the program. But now I want to give it any file with having to hard code the number of lines into the program and recompile. Below is the sample code I'm using.

    My question is how do I figure out the number of lines in a file. and two is my code below good or should I rework it to run better.
    Thanks

    Code:
    FILE *fp;
        char str[80];
        char *endpt;
        double ld ;
        double AllData[1566];
        double Xarray[783];
        double Yarray[783];
        int i = 0;
        int j = 0;
        if (( fp = fopen("./a/data.dat","r"))==NULL)
        {
              printf("File not Opened\n");     
         }
    	printf("File  Opened\n");
    	while(fscanf(fp,"%lf",&ld)!=EOF)
              {
                AllData[i]=ld;        
    	     //   printf("%s\n",str);
    	        i++;
    	      }
    	  j =0;
    	  for(i=0;i<1566;i+=2)
    	  {
    	  	Xarray[j]=AllData[i];
    		j++;
    	  }
    	  j=0;
    	  for(i=1;i<1566;i+=2)
    	  {
    	  	Yarray[j]=AllData[i];
    		j++;
    	  }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    if (( fp = fopen("./a/data.dat","r"))==NULL)
        {
              printf("File not Opened\n");     
         }
    Once you come to know the file is not opened, just exit from the main. If you still contine with following the code you will end up with the Seg Fault.

    Code:
    while(fscanf(fp,"&#37;lf",&ld)!=EOF)
    At this point, I would have used fgets to read each line and then tokensize the string.

    My question is how do I figure out the number of lines in a file. and two is my code below good or should I rework it to run better.
    You culd keep on reading the lines from the files using fgets till u reach EOF. And keep on allocation memory using malloc and store the read values into the array.

    This way you dont have to worry about hard coding the no of row in the files.

    Code:
    fclose(fp);
    And you forgot to close the file.


    ssharish2005

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    53
    Quote Originally Posted by ssharish2005 View Post
    [code]
    Code:
    while(fscanf(fp,"%lf",&ld)!=EOF)
    At this point, I would have used fgets to read each line and then tokensize the string.


    You culd keep on reading the lines from the files using fgets till u reach EOF. And keep on allocation memory using malloc and store the read values into the array.

    This way you dont have to worry about hard coding the no of row in the files.
    Can you provide some code. I don't really understand, tokensize and malloc.

    I didn't use fclose(fp) because I read that is the file isn't open and you call that command then you can damage the file system.

    Thanks

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    you can search the file for ( '\n' ) or ( '\n\r' ) - etc. to find the end of a line. increment some int variable accordingly.
    Last edited by simpleid; 08-13-2007 at 09:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM