string - C Program to get file name and range of numbers from user generate numbers from a specified range and print to file -
i writing program few things user , write results file. program file name, number of numbers generate, lowest , highest numbers generate, write file named user.
the program doing 2 things not correct:
it generating numbers outside user specified range
it adding ? end of file name, tried using strlen remove last character of string , error: incompatible implicit declaration of built-in function 'strlen'.
#include <stdio.h> #include <stdlib.h> int main() { char s[100]; file *fout; int i, x, len; int h, l, q, a; printf("please enter file name: "); fgets(s, sizeof(s), stdin); printf("how many numbers should generate: "); scanf("%d", &q); printf("lowest number generate: "); scanf("%d", &l); printf("highest number generate: "); scanf("%d", &h); fout = fopen(s, "wb"); = h - l; if ( fout == null) { printf("that file not available\n"); exit(1); } fprintf(fout, "%d\n", q); (i=0; < q; ++i) { x = (rand() % l) + a; fprintf(fout, "%d ", x); } fclose(fout); return 0; }
concerning file name: remove newline character
'\n'(or'\x0a') @ end ofsafterfgets():if( s[strlen(s)-1] == '\n' ) s[strlen(s)-1] = '\0';for completeness: on windows had remove carriage return + newline
'\r'+'\n'(or'\x0d'+'\x0a')- concerning wrong numbers: should
x = (rand() % a) + l;(modulus range + lowest, not other way round)
Comments
Post a Comment