For programs that process files that contain one or more integer values on each line. The following ʻinput.txt` is given. Read the file and put the integer values in an array.
--One or more spaces are inserted between the numbers recorded on the same line. --The range of the number of lines is from 1 line to 10000 lines --The range of integer values is from 0 to 1,000,000 $ (= 10 ^ 6) $. --The number of characters in each line is less than 80 characters (79 characters or less)
Example
input.txt
10 20 30 40 50
12 3 4 5 6
100 333 440 303 202 202 22
1 11 11 11 1 1 11 1111
111111 222222 999999
12 22 33 444
99
:
10000 111111
I wrote it down because there was almost no information on the file input when the number of elements for each line and the space between characters were different on the net. I wrote it for my own memo and for beginners of the same level, so it may be written in various strange ways. This question is one of the college transfer exam questions. It was a little difficult, so I will write it down as a memo. Please tell me a better writing method, algorithm, if there is m (> _ <) m
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10000
int main(){
FILE *fp;
//Variables etc. will be added here ☆
if((fp = fopen("./input.txt", "r")) == NULL) exit(1);
//File processing here ☆
fclose(fp);
return 0;
}
I will write only the part of the comment with ☆ after </ strong>
Get file data line by line using fgets function
main.c
char data[80];
unsigned int i;
for(i = 0; i < N && fgets(data, sizeof(data), fp) != NULL; i++){
//This will store the string on each line in data
//At this point, the string contains spaces
printf("data [%d] %s\n", i, data);
}
/*output
data [0] 10 20 30 40 50
data [1] 12 3 4 5 6
:
data [N] ???
*/
Get numbers separated by whitespace using strtok function The strtok function passes the first pointer of the string first, then NULL the second and subsequent times. Type conversion from char to int with atoi function
main.c
char data[80];
int num[N][80] = {{0}}; //The numbers in the file are stored here
char *te, *ee;
unsigned int i, j, k;
int temp = 0;
for(i = 0; i < N && fgets(data, sizeof(data), fp) != NULL; i++){
te = data; //String pointer
j = 0;
//A character is entered in ee, and the end of the character is'\0'Replace with
while((ee = strtok(te, " \n")) != NULL) {
num[i][j] = atoi(ee); //Convert to number and store in array
te = NULL; //strtok()Store NULL for the second and subsequent calls to
j++;
}
}
You can manipulate the numerical value by playing with num as follows. Since the number of elements is not acquired, only data at a specific position, maximum value, minimum value, linear search, etc. can be performed. If you can get the number of elements well in the above stage, you may be able to get the average and standard deviation in the data row by row. If you stop at the point of 0, you can get the number of elements itself, but this time it is abandoned because there is 0 in the range of integer values.
main.c
//Display only the first integer value in the i-th row of the array
printf("%d\n", num[i][0]);
//Show maximum value in row i of array
temp = num[i][0];
for(k = 1; k < (sizeof(num[i]) / sizeof(int)); k++){
if(temp < num[i][k]) {
temp = num[i][k];
}
}
printf("max = %d \n", temp);
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10000
int main() {
FILE *fp;
char data[80];
int num[N][80] = {{0}};
char *te, *ee;
unsigned int i, j, k;
int temp = 0;
if((fp = fopen("./input.txt", "r")) == NULL) exit(1);
for(i = 0; i < N && fgets(data, sizeof(data), fp) != NULL;i++){
te = data;
j = 0;
while((ee = strtok(te, " \n")) != NULL){
num[i][j] = atoi(ee);
j++;
te = NULL;
}
printf("first = %d\n", num[i][0]);
temp = num[i][0];
for(k = 0; k < (sizeof(num[i]) / sizeof(int)); k++){
printf("%d ", num[i][k]); //Show all
if(temp < num[i][k]){
temp = num[i][k];
}
}
printf("\n");
printf("max = %d \n", temp);
//printf("\n");
}
fclose(fp);
return 0;
}
--Since the array has a fixed length, 0 is entered except for the entered numbers. (In this case, the number of arrays is fixed at 80) ← Dynamic array? Should I do it? --As a process, you cannot find anything other than searching for the maximum and minimum values. ← If you can get a good number of elements, you can get an average etc. --If there is a certain rule in the numerical sequence of input.txt, read it with fscanf (). ――Usually, the number of elements is fixed or the order of data is fixed, so there may be almost no such situation. ――In the first place, is it possible to count the number of elements by looking at the line breaks on any line and separating the elements and spaces? (I do not know the method)
In the comments below
--To @fujitanozomu for ideas on getting the number of elements in each row
--@ angel_p_57's implementation idea using strtol ()
I told you! informative. Thank you