[英]Reading from file into array and performing operations with numbers in the array
嗨,我需要从文件中读取数字并制作一个仅包含重复数字的新文件。
我遇到的问题是,当我读入数组时,这些数字没有分开。
因此,我想问一下如何从带空格的文件中读取数字,或者如何知道数字何时结束?
因为如果我这样阅读,我将无法再使用这些数字进行任何操作。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *file = fopen("w.txt", "r");
int integers[100];
int i = 0;
int num;
while (fscanf(file, "%d", &num) > 0)
{
integers[i] = num;
i++;
printf("%d", integers[i]);
}
fclose(file);
system("pause");
return 0;
}
在尝试打印刚刚读入的数字后 ,我将增加索引计数器:
while(fscanf(file, "%d", &num) > 0) {
integers[i] = num;
printf("%d\n", integers[i]); /* Printing a \n after each number puts it on a new line. */
i++;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.