[英]C programming: Read lines of numbers from a file and store it in an array
I'm kinda new to programming so sorry for a stupid question.我对编程有点陌生,很抱歉提出一个愚蠢的问题。 I've been tasked to read first two lines (numbers) and store them as variable n, and the other one in m.
我的任务是读取前两行(数字)并将它们存储为变量 n,另一行存储在 m 中。 Other numbers bellow those two lines need to be read and store it in a dynamic array (malloc to be exact).
需要读取这两行下面的其他数字并将其存储在动态数组中(确切地说是 malloc)。 Each line has exactly one number.
每行只有一个数字。 I'm having trouble trying to accomplish this.
我很难做到这一点。
Here's my code so far:到目前为止,这是我的代码:
#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;
}
File ulazna.txt has:文件 ulazna.txt 有:
3
3
This works as intended but when I write:这按预期工作,但是当我写时:
3
3
1
2
3
4
5
6
7
8
9
My program prints random numbers from this file.我的程序从此文件中打印随机数。
So, I need to read 3 and 3 into n and m.所以,我需要将 3 和 3 读入 n 和 m。 Then, from 1 to 9 into the array list.
然后,从 1 到 9 进入数组列表。
Thanks for help in advance:)提前感谢您的帮助:)
If you want to dynamically allocate memory for the list, you would have to count the number of lines in the file first.如果要为列表动态分配 memory,则必须先计算文件中的行数。
#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;
}
If you already know that the number of lines in the file is going to be 11, the array has a length of 9 and there is no need to dynamically allocate memory.如果您已经知道文件中的行数将是 11,则数组的长度为 9,无需动态分配 memory。 But because your assignment requires it, I have implemented
malloc
.但是因为你的任务需要它,我已经实现
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.