Read list of numbers in txt file and store to array in C -
i have list of integers, 1 number per line , store each of these numbers in integer array utilize later in program.
for illustration in java this:
filereader file = new filereader("integers.txt"); int[] integers = new int [100]; int i=0; while(input.hasnext()) { integers[i] = input.nextint(); i++; } input.close();
how done in c?
give go. you'll much improve off if read man pages each of these functions (fopen(), scanf(), fclose()) , how allocate arrays in c. should add together error checking this. example, happens if integers.txt not exist or don't have permissions read it? if text file contains more 100 numbers?
file *file = fopen("integers.txt", "r"); int integers[100]; int i=0; int num; while(fscanf(file, "%d", &num) > 0) { integers[i] = num; i++; } fclose(file);
c arrays
No comments:
Post a Comment