c - C 编程:从文件中读取数字行并将其存储在数组中
问题描述
我对编程有点陌生,很抱歉提出一个愚蠢的问题。我的任务是读取前两行(数字)并将它们存储为变量 n,另一行存储在 m 中。需要读取这两行下面的其他数字并将其存储在动态数组中(确切地说是 malloc)。每行只有一个数字。我很难做到这一点。
到目前为止,这是我的代码:
#include <stdio.h>
#define MAX_LINE 5
int main() {
FILE* in;
FILE* out;
int n, m, list = (int*)malloc(9 * sizeof(int));
in = fopen("ulazna.txt", "r");
out = fopen("izlazna.txt","w");
if ((in && out) == NULL)
{
printf("Error opening the file...");
return -1;
}
while (fscanf(in, "%d\n", &m) != EOF)
{
fscanf(in, "%d\n", &n);
fscanf(in, "%d\n", &m);
printf("First two: %d %d", n, m);
}
//free(list);
fclose(in);
fclose(out);
return 0;
}
文件 ulazna.txt 有:
3
3
这按预期工作,但是当我写时:
3
3
1
2
3
4
5
6
7
8
9
我的程序从此文件中打印随机数。
所以,我需要将 3 和 3 读入 n 和 m。然后,从 1 到 9 进入数组列表。
提前感谢您的帮助:)
解决方案
动态数组大小
如果要为列表动态分配内存,则必须先计算文件中的行数。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, m;
/* open the file for reading text using "r" */
FILE *file_in = fopen("ulazna.txt", "r");
if (!file_in) { /* same as `file_in == NULL` */
printf("Failed to open file\n");
return EXIT_FAILURE;
}
int lines = 0; /* number of lines */
int c; /* return value of `fgetc` is of type `int` */
/* go through each character in the file */
while ((c = fgetc(file_in)) != EOF) {
/* increment the number of lines after every line break */
if(c == '\n')
++lines;
}
/* reset pointer to start of file */
rewind(file_in);
/* check the return value of `fscanf` */
if (fscanf(file_in, "%d\n", &n) != 1 ||
fscanf(file_in, "%d\n", &m) != 1) {
printf("File contains invalid line\n");
return EXIT_FAILURE;
}
printf("First two: %d %d\n", n, m);
/* no need to cast `malloc`, because it returns `void *` */
int *list = malloc(lines * sizeof(int));
for (int i = 0; i < lines - 2; ++i) {
/* check the return value of `fscanf` */
if (fscanf(file_in, "%d\n", &list[i]) != 1) {
printf("File contains invalid line\n");
return EXIT_FAILURE;
}
printf("%d\n", list[i]);
}
free(list); /* always free memory allocated by malloc */
fclose(file_in);
return EXIT_SUCCESS;
}
固定数组大小
如果您已经知道文件中的行数将是 11,则数组的长度为 9,无需动态分配内存。但是因为你的任务需要它,所以我已经实现了malloc
.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, m;
/* open the file for reading text using "r" */
FILE *file_in = fopen("ulazna.txt", "r");
if (!file_in) { /* same as `file_in == NULL` */
printf("Failed to open file\n");
return EXIT_FAILURE;
}
/* check the return value of `fscanf` */
if (fscanf(file_in, "%d\n", &n) != 1 ||
fscanf(file_in, "%d\n", &m) != 1) {
printf("File contains invalid line\n");
return EXIT_FAILURE;
}
printf("First two: %d %d\n", n, m);
/* the array has a fixed size of 9 */
const int size = 9;
int *list = malloc(size * sizeof(int));
/*
* If you do not have to use malloc, remove the
* above line and uncomment the following line:
*
* int list[size];
*/
for (int i = 0; i < size; ++i) {
/* check the return value of `fscanf` */
if (fscanf(file_in, "%d\n", &list[i]) != 1) {
printf("File contains invalid line or has to few lines\n");
return EXIT_FAILURE;
}
printf("%d\n", list[i]);
}
fclose(file_in);
return EXIT_SUCCESS;
}
推荐阅读
- react-native - 反应本机:没有调试模式的时刻不适用于特定时间值
- c++ - 从二叉搜索树中删除节点时遇到问题
- node.js - Web App 调用失败,并带有自定义 API 的“...错误代码 INTERNAL”
- ios - 如何为 Swift 3.0 专门安装 firebase pod
- matlab - 如何在 MATLAB 中获取或创建 .fig 文件
- javascript - Js过滤嵌套数组
- python-3.x - 提取 2 个字符 [ ] 之间的多个子字符串并在 Python 中替换
- multidimensional-array - ndarray:有没有一种有效的方法来交换 2 行/列/等。在不同的数组中?
- javascript - 汉堡菜单不可见
- hibernate - net:: ERROR_ABORTED 404(NOT FOUND) 在 netbeans 8.2 中找不到资源