NEED SOME HELP

I'm new to linux and im trying to compile my code, but I have no idea what im doing....Please HELP (im going gray and bald)

PS This is in C not C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <string.h>

#include <stdio.h>





int main(int argc, char **argv)

{

	char filename[32];

	FILE *fp;

	int x, high=-1000000, low=100000, sum=0;

	if (argc < 1) 

	{

		printf("Usage: assgn0203 filename\n");

		exit(1);

	}

	else

	{

		filename = argv[1];

		fopen(filename,"rt");

		if (fp = NULL)

		{

			printf("DNE");

			exit(1);

		}

		else

		{

			while(!feof(fp))

			{

				fscanf(fp,"%d",&x);

				if (x < low)

					low = x;

				if (x > high)

					high = x;

				sum = sum + x;

			}

			fclose(fp);

		}

	}

	return 0;

}
Last edited on
The "filename" should be declared as:
char *filename;

Remember, a string in C (often called a C-string) is just an array of chars. If you plan to copy the array, use strcpy(). If you just need to reference the array (as you do here), then use =.

Hope this helps.
For some reason the * didnt show up when i did my copy. I made the modification to the code to reflect the strcpy(), but still cant get it to compile.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string.h>
#include <stdio.h>


int main (int argc, char **argv)
{
	char filename[20];
	char buffer[100];
	FILE *fp;
	double x, high=-1000000, low=100000, sum=0, counter = 0;
	if (argc < 1) 
	{
		fprintf(stderr,"Usage: assgn0203 filename\n");
		exit(1);
	}
	else
	{
		strcpy(filename,argv[1]);
		fp = fopen(filename,"rt");
		if (fp = NULL)
		{
			printf("DNE");
			exit;
		}
		else
		{
			while(!feof(fp))
			{
				if (fgets(buffer,99,fp) == NULL) break;
				sscanf(buffer,"%lf",&x);
				if (x < low)
					low = x;
				if (x > high)
					high = x;
				sum = sum + x;
			}
			fclose(fp);
			printf("The avaerage is %lf \n",sum=(sum/counter));
			printf("The highest value is : %lf \n", high);
			printf("The lowest value is : %lf \n", low);
		}
	}
	return 0;
}
Last edited on
That is because you did not obey my advice and now your code has a dangerous bug (what if the user specifies a filename containing more than 19 characters?).

I don't know why you introduced the "buffer" variable... Unless there is much more to your code than is posted here...
Sorry.....Im sooooo new to C. Im not sure what to change to fix it.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
\
#include <string.h>
#include <stdio.h>


int main (int argc, char **argv)
{
	char *filename;
	FILE *fp;
	double x, high=-1000000, low=100000, sum=0, counter = 0;
	if (argc < 1) 
	{
		fprintf(stderr,"Usage: assgn0203 filename\n");
		exit(-1);
	}
	else
	{
		filename=argv[1];
		while(!feof(fp))
		{
			if ((fp = fopen(filename,"rt"))== NULL)
			{
				printf("DNE");
				exit(-1);
			}
			sscanf(fp,"%lf",&x);
			if (x < low)
				low = x;
			if (x > high)
				high = x;
			sum = sum + x;
		}
		fclose(fp);
		printf("The avaerage is %lf \n",sum=(sum/counter));
		printf("The highest value is : %lf \n", high);
		printf("The lowest value is : %lf \n", low);
	}
	return 0;
}
Better, but a few things are still moved around with respect to your first post. Also, something I missed the first time:
if (argc < 2)
(Because argc includes the name of the program.)

Move the stuff that opens the file out of the loop. (You have to open the file before you can test for EOF.)

Don't forget to turn sscanf() back into fscanf(). Also, you forgot to increment 'counter' in the loop.

In printf(), don't use %lf. Just use %f.

You accidentally misspelled "average".

Very nicely done, otherwise. I hope this helps.
Last edited on
Cool....thx

It works
Topic archived. No new replies allowed.