Hi,

I am using fgets in my program to count the number of lines in file. The number of columns in each line is entered by the user. But the number of lines are not known. After counting the number of lines in the file i am again using it to store each line of the file in a two dimensional array.

The problem is the number of lines in my file is 4. however the number of lines counted by fgets() is 8. Following is my code.

fscanf(inFile2, "%d", &numCols);
    fscanf(inFile2, "%c", &nullchar);
    
    int linenumber = 0;
    char line[numCols];
    
	while (fgets(line, (numCols+1), inFile2) != NULL)
	{
		fgets(line, (numCols), inFile2);
		if(line[0] == 'E')
		break;
		linenumber++;
	}
	fclose(inFile2);
	
	fprintf(stderr, "numCols = %d, Linenumber = %d", linenumber);
    FILE *inFile3 = fopen(tName, "r");

    for(int ll=0; ll<linenumber; ll++)
	{
		fgets(line, (numCols+1), inFile3);
        for(l = 0; l < strlen(line); l++)
        {
            if(line[l] == '0') 
            {
                test_vector[ll][l] = 0;
            }
            
            if(line[l] == '1') 
            {
                test_vector[ll][l] = 1;
            }
            
            if(line[l] == 'X') 
            {
                test_vector[ll][l] = 2;
            }
            
        }
   }
    
    fclose(inFile3);

-----------------------------------------------------------

My file looks something like this

5
10101
10100
00010
10101
END

The value of linenumber is not 4.

Thanks

1)Why are you using fgets for one, this is c++ so getline would work and is easier to understand.

2) Reduce your program so at first it just reads in each line and outputs a line counter

so should i use getline to count the number of lines.

It doesn't matter, if you use fgets correctly you can achieve the same thing, and it would be perfectly valid to use in c++.

I would do something like:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  ifstream read("c:/test.txt");
  
  string line;
  int lineCounter = 0;
  while ( getline ( read, line, '\n' ) )
  {
    cout << line << endl;
    lineCounter++;
   }
   cout << "total lines:" << lineCounter <<endl;
   read.close();
}

ok..but i am not able to understand why it is counting double the lines and what is wrong in my code.

ok..but i am not able to understand why it is counting double the lines and what is wrong in my code.

So you need to debug it, step through your program bit by bit, add a few couts or printfs here and there.

thanks..is there a way to give a delimiter for fgets() because i think in my code it is counting the newline character for the number of lines?

thanks..is there a way to give a delimiter for fgets() because i think in my code it is counting the newline character for the number of lines?

By definition, then your program DOES NOT count lines.

There should be a way to get fgets() to choose a delimiter.

hi,

if i do not knwo the file name and use getline() as follows i get an error. could you please let me know what is wrong.

FILE *inFile2 = fopen(tName, "r");

while(getline(inFile2, line, '\n'))
	{
		if(line[0] == 'E')
		break;
		linenumber++;
	}

Thanks

can someone suggest me the correct way to count the number of lines in the file using fgets() and then read the number of lines in a 2 dimensional array. I am not using vectors because of better execution time (speed) of my program. Also, the first row in the file gives the number of columns in the file. So i am fscanf. I do not know how to use getline() with fscanf ()

Please help

This article has been dead for over six months. Start a new discussion instead.