C language tutorial: How to read numbers line by line

C language tutorial: How to read numbers line by line

In C language programming development, it is often necessary to process strings or text files and extract numbers from them. This article will introduce the method of reading numbers line by line to help beginners better understand and use it.

How to read numbers line by line in C language

I. Introduction

Numbers play an important role in computer programming and are used in a wide range of applications. When processing strings or text files, you need to extract the numbers and perform calculations or other operations. Reading numbers line by line is a common need, and we will introduce several ways to achieve it.

2. Use the sscanf function

The sscanf function is a commonly used string formatting function in C language. Its function is similar to the scanf function, which can extract data from a string in a specified format. For reading numbers line by line, we can use sscanf with the fgets function.

#include

int main() {

char line[100];

FILE *file = fopen(\data.txt\ \r\

while (fgets(line, sizeof(line), file)) {

int whether;

if (sscanf(line, \d\ &num) == 1) {

printf(\Read number: %d\

\ num);

}

}

fclose(file);

return 0;

}

In the above code, we open the text file named data.txt and use the fgets function to read the file content line by line and store it in the line character array. Then, we use the sscanf function to extract the numbers in the line and store them in the num variable. If the extraction is successful, we can proceed with subsequent operations on it.

3. Use strtol function

The strtol function is a string to integer conversion function in C language and has powerful functions. We can use the strtol function to read the numbers line by line and handle errors.

#include

#include

int main() {

char line[100];

FILE *file = fopen(\data.txt\ \r\

while (fgets(line, sizeof(line), file)) {

char *endptr;

long num = strtol(line, &endptr, 10);

if (line != endptr) {

printf(\Read number: %ld\

\ num);

}

}

fclose(file);

return 0;

}

In the above code, the first parameter of the strtol function is the string to be converted, and the second parameter is a pointer to the end of the string, which points to the next character of the converted string after the function is called. The third parameter is base, here it is decimal. By judging whether endptr is equal to line, we can judge whether the conversion is successful.

4. Use regular expressions

Regular expressions are a powerful pattern matching tool. In C language, support for regular expressions is provided through the regex.h header file. We can use regular expressions to match and extract numbers line by line.

#include

#include

int main() {

char line[100];

FILE *file = fopen(\data.txt\ \r\ regex_t regex;

regcomp(&regex, \0-9]+\ REG_EXTENDED);

while (fgets(line, sizeof(line), file)) {

regmatch_t match;

if (regexec(&regex, line, 1, &match, 0) == 0) {

int start = match.rm_so;

int end = match.rm_eo;

char num[end - start + 1];

strncpy(num, line + start, end - start);

num[end - start] = ‘\0’;

printf(\Read number: %s\

\ num);

}

}

regfree(&regex);

fclose(file);

return 0;

}

In the above code, we first use the regcomp function to compile the regular expression, and then use the regexec function in the loop to perform matching. If the match is successful, we can obtain the starting and ending positions of the match through the rm_so and rm_eo members of the regmatch_t structure to extract the number.

5. Summary

Reading numbers line by line is a common requirement in C language programming. This article introduces several methods to achieve this function. Use the sscanf function, strtol function or regular expressions to easily extract numbers and perform subsequent operations. I hope this article can help readers better understand the method of reading numbers line by line in C language, and can use it flexibly in actual projects.
Part of the code is transferred from: https://www.ktiao.com/c/2023-08/254255.html

Guess you like

Origin blog.csdn.net/qq_42151074/article/details/132271105