hi guys, I found this code on the web but unable to integrate it into my code, can any1 help?. The problem is, it doesnt read till the eof, instead it stops when the condition is erroneously made.

#declare Line_Char_Buffer_Size 4000
#declare INPUT_FILE_NAME "Countries.txt"
void readData ()
{
	FILE * pFile;
	NoOfRecordsRead = 0;
   	char buffer [Line_Char_Buffer_Size];

	pFile = fopen (INPUT_FILE_NAME , "r");
   
	if (pFile == NULL) 
		perror ("Error opening file 'Countries.txt' !");
	else
	{
		while ( !feof (pFile) )
     	{
			char* aLine = get_line (buffer, Line_Char_Buffer_Size, pFile);

			if (aLine != NULL)
			{
//				printf ("%d] aLine => %s\n", NoOfRecordsRead, aLine);
				globalCountryDataArray [NoOfRecordsRead++] = createCountryRecord (aLine);
			}
     	}

     fclose (pFile);
	
	}
}

Countries.txt

BH,Bahrain,BA,BH,BHR,48.00,Manama,Middle East,Bahraini Dinar,BHD,645361.00
BI,Burundi,BY,BI,BDI,108.00,Bujumbura,Africa,Burundi Franc,BIF,6223897.00
BJ,Benin,BN,BJ,BEN,204.00,Porto-Novo ,Africa,CFA Franc BCEAO,XOF,6590782.00
BM,Bermuda,BD,BM,BMU,60.00,Hamilton,North America,Bermudian Dollar,BMD,63503.00
BN,Brunei Darussalam,BX,BN,BRN,96.00,Bandar Seri Begawan,Southeast Asia,Brunei Dollar,BND,
BO,Bolivia,BL,BO,BOL,68.00,La Paz /Sucre,South America,Boliviano,BOB,8300463.00
BR,Brazil,BR,BR,BRA,76.00,Brasilia,South America,Brazilian Real,BRL,174468575.00

The output of program will stop at BN,Brunei Darussalam,BX,BN,BRN,96.00,Bandar Seri Begawan,Southeast Asia,Brunei Dollar,BND, and not continue further. How do i modify it such a way that it will read the entire text? Thanks in advance

Use while((fgets(bufferName, sizeof(bufferName), filePointerName)) != NULL) {

instead.

Testing for the end of the file has become quite unpopular (and deservedly so), for just this reason (among others). Don't use it.

Hi, I tried the method u suggested. However it doesnt even display everything, it instead omits it from displaying. This is what i wrote

void readData ()
{
	FILE * pFile;
	NoOfRecordsRead = 0;
   	char buffer [Line_Char_Buffer_Size];

	pFile = fopen (INPUT_FILE_NAME , "r");
   
	if (pFile == NULL) 
		perror ("Error opening file 'Countries.txt' !");
	else
	{
		while ((fgets(buffer, Line_Char_Buffer_Size,pFile))!= NULL)
		{
		char* aLine = get_line (buffer, Line_Char_Buffer_Size, pFile);

			if (aLine != NULL)
			{
				printf ("%d] aLine => %s\n", NoOfRecordsRead, aLine);
				globalCountryDataArray [NoOfRecordsRead++] = createCountryRecord (aLine);
			}
     	}
			
     	}

     fclose (pFile);
	
	
}

What does get_line() do? Does that function also read a line from the file? If it does then your program is reading two lines at a time before doing anything with them.

void readData ()
{
    FILE * pFile;
    NoOfRecordsRead = 0;
    char buffer [Line_Char_Buffer_Size];

    pFile = fopen (INPUT_FILE_NAME , "r");
   
    If (pFile == NULL) 
         perror ("Error opening file 'Countries.txt' !");
    else
    {
      while ( fgets(buffer, Line_Char_Buffer_Size,pFile) != NULL)
      {
         printf ("%d] aLine => %s\n", NoOfRecordsRead, buffer);
         globalCountryDataArray [NoOfRecordsRead++] = createCountryRecordaLine);
      }
      fclose (pFile);
    }
			
}

u have read line in c !!

readline will read a line from the terminal and return it,
using prompt as a prompt. If prompt is null, no prompt is
issued.

This question has already been answered. Start a new discussion instead.