LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2007, 04:06 PM   #1
joakim12
Member
 
Registered: Nov 2003
Location: Estonia
Distribution: Ubuntu 18.04
Posts: 82

Rep: Reputation: 15
Problems reading from file with fgets


Hi.

I have one little program code in C:
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void){

	FILE *fp;
	fp = fopen("TextFile", "r");

	if (!fp) {
		printf("Error opening file\n");
		return 0;
	};

	char *Line = malloc (200 * sizeof(char));

	fseek(fp, 0, SEEK_END);
	printf("File size: %li\n\n", ftell(fp));
	rewind(fp);

	while(!feof(fp)){
		fgets(Line,200,fp);
		printf("%s", Line);
		printf("Reading position: %li\n\n", ftell(fp));
	};
	return 0;
}
But it prints the last line of text from the file twice and I don't understand why. Isn't the file reading position in the end of file yet or what?
 
Old 10-06-2007, 04:53 PM   #2
SeRGeiSarov
Member
 
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 52

Rep: Reputation: 15
Why do you use !feof(fp). I think that !fgets(Line,200,fp) is enough.

P.S. Can I see the text file
 
Old 10-06-2007, 04:57 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Clear the buffer before each read (memset to zero).

fgets() reads to a newline or eof. When reading the last line it reads to the newline character, but it's not yet an eof. So it reads again, but the read is empty this time and the eof is reached.
 
  


Reply

Tags
fgets



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fgets read a whole file alaios Programming 4 08-21-2005 06:57 PM
fgets never stop reading from socket!? Thinking Programming 1 04-06-2005 09:38 AM
reading in string with fgets bostonbanana Programming 4 12-12-2004 02:10 PM
problems reading in fixed-length record file naijaguy Programming 1 08-24-2004 02:34 PM
fgets() problems in C AMMullan Programming 4 03-12-2004 04:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration