ras123
August 13th, 2010, 06:29 PM
Hi,
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <string>
lines()
{
int currentLine;
char line[1024];
//string str;
FILE *stream = fopen("file.txt", "r"); // Open file read only
if(!stream)
return FILE_OERR; // File open error
currentLine = 0;
while(!feof(stream))
{
fgets(line, sizeof(line), stream); // Read a line
currentLine++;
}
return currentLine;
}This function returns 7, instead of 6 (no of lines in the file.txt - attached with this), Why ? On debugging, I found that lines array, get two times (6th and 7th looping) the last line of file!!
Please let me know the error
Thanking You
Ras
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <string>
lines()
{
int currentLine;
char line[1024];
//string str;
FILE *stream = fopen("file.txt", "r"); // Open file read only
if(!stream)
return FILE_OERR; // File open error
currentLine = 0;
while(!feof(stream))
{
fgets(line, sizeof(line), stream); // Read a line
currentLine++;
}
return currentLine;
}This function returns 7, instead of 6 (no of lines in the file.txt - attached with this), Why ? On debugging, I found that lines array, get two times (6th and 7th looping) the last line of file!!
Please let me know the error
Thanking You
Ras