如何将保存在.txt文件中的进程值保存到c中的4个不同数组中
带有以下信息的给定processs.txt如何将保存在.txt文件中的进程值保存到c中的4个不同数组中,c,memory-management,scheduling,virtual-memory,taskscheduler,C,Memory Management,Scheduling,Virtual Memory,Taskscheduler,带有以下信息的给定processs.txt 0 4 96 30 3 2 32 40 5 1 100 20 20 3 4 30 如何将文件的每一行保存为c中4个不同数组的元素? i、 e 这是我的代码,我已经加载了.txt文件,但不知道如何将值存储在整数数组中。我的代码生成垃圾值 如何编写程序 #include <stdio.h> int main(int argc, char *argv[]) { FILE *fp; char *fil
0 4 96 30
3 2 32 40
5 1 100 20
20 3 4 30
如何将文件的每一行保存为c中4个不同数组的元素?
i、 e
这是我的代码,我已经加载了.txt文件,但不知道如何将值存储在整数数组中。我的代码生成垃圾值
如何编写程序
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
char *filename;
char ch;
// Check if a filename has been specified in the command
if (argc < 2)
{
printf("Missing Filename\n");
return(1);
}
else
{
filename = argv[1];
printf("Filename : %s\n", filename);
}
// Open file in read-only mode
fp = fopen(filename,"r");
// If file opened successfully, then print the contents
if ( fp )
{
printf("File contents:\n");
while ( (ch = fgetc(fp)) != EOF )
{
printf("%c",ch);
}
}
else
{
printf("Failed to open the file\n");
}
// Saving the contents of the file in a Number Array
int numberArray[16];
int i;
for (i = 0; i < 16; i++)
{
fscanf(fp, "%d", &numberArray[i]);
}
for (i = 0; i < 16; i++)
{
printf("Number is: %d\n\n", numberArray[i]);
}
return(0);
}
您必须将指针fp重置为文件的开头。因为while循环:while ch=fgetcfp!=EOF,指针fp位于文件末尾
int numberArray[16];
int i;
fseek(fp, 0, SEEK_SET);
fscanf的for循环应替换为while循环:
现在,如果要将numberArray分隔为4个数组arr1、arr2、arr3和arr4。您可以将numberArray格式0到3的前4个元素分配给arr1,然后将4到7的下4个元素分配给arr2,依此类推 请阅读-总结是,这不是一个理想的方式来解决志愿者,可能会适得其反获得答案。请不要将此添加到您的问题中。此问题需要缩小一些范围。如果在写入/访问数组元素时遇到困难,则可以删除有关调度程序仿真的内容。读者很乐意帮助解决一些小的、有重点的问题,但不是为你完成整个项目。阅读更多。使用一些源代码编辑器编写程序,如或许多其他编辑器。StackOverflow不是一个“做我的作业”网站
int numberArray[16];
int i;
fseek(fp, 0, SEEK_SET);
int numberArray[16];
int i = 0;
fseek(fp, 0, SEEK_SET);
while(fscanf(fp, "%d", &numberArray[i]) == 1 && i < 16)
{
i++;
}
// Then when you print the value from 0 to i
for (int j = 0; j < i; j++)
{
printf("Number is: %d\n\n", numberArray[j]);
}
fclose(fp);