[C] Prüfen ob der Pointer am Ende einer Datei steht

H4ckHunt3r

Erfahrenes Mitglied
Ich habe im moment durch mein Studium eine Aufgabe,
welche von mir verlangt eine PGM Datei (Graustufen Bild) zu verarbeiten.
Im Header der Datei sind 4 Werte darunter Größe und Breite des Bildes in Pixel.
Für jeden Pixel des Bildes ist ein Grauwert (0-255) angegeben.

Ich lese nun Größe und Breite ein und lege ein 2 Dimensionales Array mit der entsprechenden größe an,
dann lese ich die Grauwerte mit Hilfe von 2 verschachtelten for-Schleifen in dieses 2-Dimensionale Array ein,
jedoch muss ich noch Prüfen ob alles eingelesen oder das Ende der Datei frühzeitig erreicht wurde.

Und da liegt mein Problem:
Ich dachte ich könnte prüfen ob der Filepointer am Ende der Datei ist,
weis allerdings nicht wie ich dies prüfen kann
und finde leider auch im web keine Lösung.


Hoffe hier kann mir jemand bei dem Problem Helfen.
 
Ja, natürlich hier bitte mein aktueller Code:
C:
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>



/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  Hauptprogramm
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{

	char *path = "dreifach.pgm";
	char magic[10];
	int width;
	int height;
	int greyscaleMax;


	FILE	*pgmInput;										/* input-file pointer */
	char	*pgmInput_file_name = path;		/* input-file name    */

	pgmInput	= fopen( pgmInput_file_name, "r" );
	if ( pgmInput == NULL ) {
		fprintf ( stderr, "couldn't open file '%s'; %s\n",
				pgmInput_file_name, strerror(errno) );
		exit (EXIT_FAILURE);
	}
	
	
	
	/*-----------------------------------------------------------------------------
	 *  Read Header Informations (MagicNumbers, Width, Height, GreyScaleMax)
	 *-----------------------------------------------------------------------------*/
	fscanf(pgmInput, "%s", magic);

	fscanf(pgmInput, "%d", &width);
	fscanf(pgmInput, "%d", &height);
	fscanf(pgmInput, "%d", &greyscaleMax);


	/*-----------------------------------------------------------------------------
	 *  PGM Bild Einlesen
	 *-----------------------------------------------------------------------------*/
	int picture[width][height];
	int countWidth, countHeight;
	
	for(countHeight = 0; countHeight < height; countHeight++)
	{
		for(countWidth = 0; countWidth < width; countWidth++)
		{
			fscanf(pgmInput, "%d", &picture[countWidth][countHeight]);
		}
	}



	if( fclose(pgmInput) == EOF ) {			/* close input file   */
		fprintf ( stderr, "couldn't close file '%s'; %s\n",
				pgmInput_file_name, strerror(errno) );
		exit (EXIT_FAILURE);
	}



	return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
 
Zuletzt bearbeitet:
Ich würde einfach den Rückgabewert von fscanf prüfen.
cplusplus.com hat gesagt.:
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.
Schau mal, ob du es alleine hinbekommst ;)

Du kannst immer gerne nochmal nachfragen.
 
Ouh mann.. schon min. 10mal gemacht und doch kommt man immer noch nicht auf die idee den Rückgabewert zu prüfen..
Vielen Dank für die Hilfe.

Hier nochmal den fertigen Code für die Nachwelt:
C:
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>



/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  Hauptprogramm
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{

	char *path = "dreifach.pgm";
	char magic[10];
	int width;
	int height;
	int greyscaleMax;


	FILE	*pgmInput;										/* input-file pointer */
	char	*pgmInput_file_name = path;		/* input-file name    */

	pgmInput	= fopen( pgmInput_file_name, "r" );
	if ( pgmInput == NULL ) {
		fprintf ( stderr, "couldn't open file '%s'; %s\n",
				pgmInput_file_name, strerror(errno) );
		exit (EXIT_FAILURE);
	}
	
	
	
	/*-----------------------------------------------------------------------------
	 *  Read Base Informations (MagicNumbers, Width, Height, GreyScaleMax)
	 *-----------------------------------------------------------------------------*/
	fscanf(pgmInput, "%s", magic);
	printf("Magic Numbers: %s\n", magic);

	fscanf(pgmInput, "%d", &width);
	fscanf(pgmInput, "%d", &height);
	fscanf(pgmInput, "%d", &greyscaleMax);
	printf("Breite: %d\nHoehe: %d\nGrey-Max: %d\n", width, height, greyscaleMax);

if(magic[0] == 'P' && magic[1] == '2')
{
	/*-----------------------------------------------------------------------------
	 *  PGM Bild Einlesen
	 *-----------------------------------------------------------------------------*/
	int picture[width][height];
	int countWidth, countHeight, result;
	
	for(countHeight = 0; countHeight < height; countHeight++)
	{
		for(countWidth = 0; countWidth < width; countWidth++)
		{
			result = fscanf(pgmInput, "%d", &picture[countWidth][countHeight]);
			if(result != 1)
			{
				printf("ERROR: File Header seems to be broken (to less pixels)!\n");
			}
		}
	}

	int test = fscanf(pgmInput, "%d", &test);
	if(test > 0)
	{
		printf("ERROR: File Header seems to be broken (to much pixels)!\n");
		exit(EXIT_FAILURE);
	}
}

	if( fclose(pgmInput) == EOF ) {			/* close input file   */
		fprintf ( stderr, "couldn't close file '%s'; %s\n",
				pgmInput_file_name, strerror(errno) );
		exit (EXIT_FAILURE);
	}



	return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
 
Zurück