Processing when the number of numerical elements in the C language file is not determined for each line

problem

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

environment

note

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

Read file

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>

Read a string line by line

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] ???
*/

Stores space-separated strings in an array (+ converts to a number)

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++;
    }
}

Numerical processing

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);

Whole code

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;
}

Disadvantages, points to keep in mind, etc.

--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)

Addendum / Acknowledgments

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

Recommended Posts

Processing when the number of numerical elements in the C language file is not determined for each line
[C language] Calculation when the exponent is negative in BIGNUM of OpenSSL
Get the number of files and file name in the folder in C language
Differences in the behavior of each LL language when the list index is skipped
Check the processing time and the number of calls for each process in python (cProfile)
Google search for the last line of the file in Python
Get the number of occurrences for each element in the list
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
Change the processing when the button on RecyclerView is pressed for each list
Trends and countermeasures when there is a possibility of reading binaries in the scene of line-oriented processing in C
Check the size of the structure in C language
When the project is not displayed in eclipse
The mystery of integer literals in C language
C language code for finding the Fibonacci number
Organize file descriptors, file pointers, and file handles in the C language environment of Windows
Processing when an ID that does not exist in the database is entered in the URL
How to display the modification date of a file in C language up to nanoseconds
A function that swaps the elements of a dynamically allocated string array in C language
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
Output the specified table of Oracle database in Python to Excel for each file
Iloilo summary used in the field of C language
A brief summary of loop processing in C language
Get the size (number of elements) of UnionFind in Python
When the selected object in bpy.context.selected_objects is not returned
Path specification when including header file in C language
Find the maximum value, minimum value, and number of elements when the integer N is on the first line of the standard input and N integers follow.
[Natural language processing] I tried to visualize the remarks of each member in the Slack community
For those who want to know the skeleton of C language in an amakudari manner
Conversion from char type to int type in #c language (any number of digits is acceptable)
Behavior in each language when coroutines are reused with for
Numerical approximation method when the calculation of the derivative is troublesome
The story of making an Invaders game in C language
The image display function of iTerm is convenient for image processing.
[Rails] When the layout change of devise is not reflected
The idea of cutting off when the error is not resolved
Three implementations of the memmove function in C language freeBSD
Get the number of specific elements in a python list
Check when the C language if statement does not work
Behavior when each is executed in the reverse order Range
Check the operation of Python for .NET in each environment
The behavior of the C language character array is too mysterious
[C language] Calculate the motion of charged particles in a uniform magnetic field when there is an electric field using the Runge-Kutta method.
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows
About the error that occurs when using free () for the area not allocated by C language malloc
One of the methods when you want to pass an array to a function in C language.
When running annotation processing in the IDE, the annotation information whose Retention is SOURCE may not be obtained.
[Python] Let's reduce the number of elements in the result in set operations
"Utstring" which is convenient for manipulating character strings in C language
The C language sample of SAMURAI ENGINEERING SCHOOL is too dangerous.
When the processing after conditional branching is redundant-12 [C # refactoring sample]
I tried to rewrite the pre-built executable file in C language.
Lecture memo for the second half of the year (C language) Part 1
[Golang] "package exec is not in GOROOT" when executing the test
Why is distributed representation of words important for natural language processing?
The story of functional and function pointer types in C language
The story that inheritance behavior is quite different in each language
Dockerfile with the necessary libraries for natural language processing in python
for, continue, break Explain the flow of iterative processing in Python3-Part 1
[For beginners in C language] List of origins of standard functions and names encountered in the early stages of learning
100 language processing knocks for those who do not understand the meaning of problem sentences Chapter 8 Machine learning
[Question] In sk-learn random forest regression, an error occurs when the number of parallels is set to -1.