I have a following code, where I tried to find out largest number from .txt file.
In void function "maks" I successfully load numbers in array, variable "broj" is used to determine how many numbers from file to read, from which I look for a largest number. I'm constantly getting SIGSEGV fault, I'm not sure what's the problem.
Input should be like:
./program1 list.txt 5 max (where list.txt is name of a file, 5 is number of numbers to be read from file, max to find largest number)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void maks(int broj, FILE *file)
{
int i = 0, polje[i], max;
for(i = 0; i<=broj; i++)
fscanf(file, "%d", &polje[i]);
max = polje[0];
for (i = 0; i<= broj; i++){
// printf("%d\t", polje[i]);
if(polje[i] > max){
max = polje[i];
}
}
printf("Najveci broj:\n%d\n", max);
}
int main(int argc, char *argv[])
{
if(argc != 4){
fprintf(stderr, "Greska na ulazu.\n");
return 0;
}
char *buf = argv[2];
int broj = atoi(buf);
FILE *file;
file = fopen(argv[1], "r");
if(strcmp(argv[3], "max") == 0)
maks(broj, file);
return 0;
}