I am attempting to put integers from a text file to an array. I am new to C. I have already found the number of lines and opened the file. I do not understand why this does not work:

for(i = 0; i <= numberlines; i++)
{
	fscanf(f, "%d", &z[i]);
}

The numbers in the text file are arranged as so:
4
251
3
-45
124


I want to scan all the numbers into array z[] except for the first one. I would paste my whole program attempt, but the remote desktop won't let me copy and paste back on to my original computer... I mean I just did it a couple of minutes ago.. it's being werid. But anyways back to the problem..

I want to do this without the use of "get" or "put", because my understanding of those functions are limited.

THANK YOU !

So what is it doing instead?

during debugging, z does not take on any of the values from the text file

I would guess the file did not open properly.

I finally got to the remote desktop to let me copy and paste... please take look. I would really appreciate it. THANK YOU

// I, Sarena Meas, cerify that this is my own work
// and have not collaborated with anyone else.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int FindBig(int x[], int n);
int FindSml(int x[], int n);
float FindAvg(int x[], int n);

void main()
{
	FILE *f, *g;
	char j;
	int z[100], newline=0, big, small, i, x;
	float avg;
	f = fopen("numbers-inp.txt", "rt");
	if (f == NULL)
	{
		printf("Error opening file - exiting...\n");
		exit(1);
	} 

	g = fopen("numbers-out.txt", "wt");
	if ( g == NULL)
	{
		printf("Error opeing file - exiting... \n");
		exit(1);
	}
	// below is the retrieving the number of lines copied from another source..
	char check[81];
	char temp[81];
	while (fgets(temp, sizeof(temp), f) != NULL)
	{
		fscanf(f, "%s", &check);
		if (check != "\n")
		{
			newline++;
		}
	}
	// my attempt to sacn into an array..
	for(i = 0; i <= newline; i++)
	{
		fscanf(f, "%d", &x);
		z[i] = x;
	}

	big = FindBig(z, newline);
	small = FindSml(z, newline);
	avg = FindAvg(z, newline);
	fprintf(g, "Biggest: %d\nSmallest: %d\nAverage: %d\n", big, small, avg);
	fclose(f);
	fclose(g);
}

int FindBig(int x[], int n)
{
	int big, i;
	big = x[0];
	for(i=0; i<n; i++)
	{
		if(x[i]>big)
			big = x[i];
	}
	return big;
}

int FindSml(int x[], int n)
{
	int sml, i;
	sml = x[0];
	for(i=0; i<n; i++)
	{
		if(x[i]<sml)
			sml = x[i];
	}
	return sml;
}


float FindAvg(int x[], int n)
{
	float avg;
	int sum, i;
	sum = 0;
	for(i=0; i<n; i++)
		sum = sum + x[i];
	avg = sum / (float)n;
	return avg;
}
while (fgets(temp, sizeof(temp), f) != NULL)
	{
		fscanf(f, "%s", &check);
		if (check != "\n")
		{
			newline++;
		}
	}

First of all, newline is a very bad name for a counter. Maybe numLines would make more sense.
Use fgets() instead of fscanf() to read strings.
And isn't check a character array? All you are comparing is the address of check with the address of the constant string "\n". They will never be equal.
What are you trying to accomplish with this test?

This article has been dead for over six months. Start a new discussion instead.