Hi. I am having some problems with files. I am trying to read some contents from a file, transfer them to an array and then read the contents of the array. The problem that I am facing is that the output is not that is suppose to be. The file contains binary, and as an output I am getting only 0s. I cannot find the problem....can please someone help me? Thanks a lot.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define SIZE 1000


int i;
void CallingFile(void);

int main(void)
{
	int Array[SIZE]={0};
	int j;
	
	FILE *cfPtr;
	
	if((cfPtr = fopen("Error Pattern 2.dat","r") ) == NULL)
		{
			printf("File cannot be opened\n");
		}/* end if */

		else
			{
				//printf("Contents of file are:%d\n", cf);
				
				for (j=0;j<SIZE;j++){
					fscanf(cfPtr,"%d", &Array[j]);
					printf("%d",Array[j]);
				}
			}
			
		while(!feof(cfPtr))
		{
				printf("Contents of file are:%d\n", cfPtr);
				fscanf(cfPtr,"%d", &Array);	
		}// end while 
	
	fclose(cfPtr);
	
	
	
	
	
	return 0;
	
}

The file was read on lines 28-30, why are you trying to read it again on lines 34-37? And line 37 will just read all the numbers into the same element of Array.

My guess is that on lines 34-37 you need to just display the contents of the array on the console screen ??? Use printf(), not scanf() to do that, and you don't need the file pointer at all there.

Edited 6 Years Ago by Ancient Dragon: n/a

yes I have understood. Now that I have removed the while statement, I am still encountering the same problem (printing only 0s). Could it be that as an output I am getting only the first byte of the frame, or am I displaying the array incorrectly? Thanks a lot.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define SIZE 1000


int main(void)
{
	int Array[SIZE]={0};
	int j;
	
	FILE *cfPtr;
	
	if((cfPtr = fopen("Error Pattern 2.dat","r") ) == NULL)
		{
			printf("File cannot be opened\n");
		}/* end if */

		else
			{
				//printf("Contents of file are:%d\n", cf);
				
				for (j=0;j<SIZE;j++){
					fscanf(cfPtr,"%d", &Array[j]);
					printf("%d",Array[j]);
				}
			}
			
	/*	while(!feof(cfPtr))
		{
				printf("Contents of file are:%d\n", cfPtr);
				fscanf(cfPtr,"%d", &Array);	
		}*/
	
	fclose(cfPtr);
	
	return 0;
	
}

yes I have understood. Now that I have removed the while statement, I am still encountering the same problem (printing only 0s). Could it be that as an output I am getting only the first byte of the frame, or am I displaying the array incorrectly? Thanks a lot.

Cleaning the code and fool-proof it a bit.

#include <stdio.h>
 /* Unless you're doing something else this is the only header file needed */

#define SIZE 1000

int main(void)
{
	int Array[SIZE]={0};
	int j;
	
	FILE *cfPtr;
	
	if ((cfPtr = fopen("Error Pattern 2.dat","r") ) == NULL)
	{
		printf("File cannot be opened\n");
                /*
                 * Need to handle the error and stop here if you want
                 * to put the fclose() at the end of the code
                 */
                 return 0;
	}/* end if */ /* <--the if doesn't end here, there's a possible else */
	else
	{
		for (j = 0; j < SIZE; j++) {
                        /*
                         * Need to check that fscanf was successful
                         * Need to stop if it did not succee.
                         * Need to stop the for loop count if failed
                         */
			if ( fscanf (cfPtr,"%d", &Array[j]) != 1 ) {
                               /* failed to read more numbers */
                               fprintf(stderr, "Ending...\n");
                              /* stop counter  */
                               i = 100; /* get out of the loop */
                        } else {
                               /* Hopefully most of the time there's something read */
			      printf ("%d\n",Array[j]); /* adding a new line after read to ensurer proper display */
		}
	}
			
	fclose(cfPtr);
        return 0;
}

The file contains binary

if((cfPtr = fopen("Error Pattern 2.dat","r") ) == NULL)
					fscanf(cfPtr,"%d", &Array[j]);

Binary file needs to be opened in binary mode ("rb"), and may not be read with fscanf(). Use fread() instead.

Beware the portability issues.

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