Сделал так, ругается, говорит файл не создан. Может я вопрос неправильно поставил... Можно ли из программы создать файл и записать внего любое количество чисел, а затем обработать???
| Код | #include <stdio.h> #include <conio.h> char file[] = "f.txt"; int CountLines(){ // COUNT LINES IN FILE FILE *inp_file = fopen(file,"r"); float somevalue = 0.5f; FILE* file = fopen("file.txt", "w"); fprintf(file, "%f\n", somevalue); int not_emty_line = 0, lines = 0; do {not_emty_line = 0; while(fgetc(inp_file)!='\n' && !feof(inp_file)) not_emty_line=1; if(not_emty_line) lines++; }while(!feof(inp_file)); fclose(inp_file); return (lines); } int main(){ FILE *inp_file; //pointer to file float temp = 0, float somevalue = 0.5f; min_number = 0, max_number = 0, sum = 0, lines = 0; if((inp_file = fopen(file,"r")) == NULL) // if there is no file { perror(file); getch(); return 1; } lines = CountLines(); // how much lines (numbers) in file fscanf(inp_file,"%f",&max_number); // read first element and min_number = max_number; // it will be min and max fseek(inp_file, 0, SEEK_SET); // set cursor to begin for(int i=0;i<lines;i++) { fscanf(inp_file, "%f", &temp); // READ NUMBERS printf("%.2f\n",temp); // PRINT NUMBERS if(temp > max_number) max_number=temp; // search max if(temp < min_number) min_number=temp; // search min } sum = min_number + max_number; printf("\n%.2f + %.2f = %.2f",max_number,min_number,sum); fclose(inp_file); getch(); return 0; }
|
|