fscanf()每行只读取最后一个整数

fscanf()每行只读取最后一个整数,c,arrays,scanf,C,Arrays,Scanf,我试图使用fscanf读取文件并将其内容存储在数组中。每行包含四个整数值,需要将它们放在四个不同的数组中。我的代码读取每一行,但在所有四个数组中存储每一行的最终整数值 我尝试过使用fgets(),这似乎会导致更多的问题。更改fscanf()调用中的格式也没有帮助 为什么要跳过每行的前三个值 代码: 输出: p_id: 2 cpu_burst: 2 io_burst: 2 priority: 2 p_id: 1 cpu_burst: 1 io_burst: 1 priority: 1 p_id

我试图使用
fscanf
读取文件并将其内容存储在数组中。每行包含四个整数值,需要将它们放在四个不同的数组中。我的代码读取每一行,但在所有四个数组中存储每一行的最终整数值

我尝试过使用
fgets()
,这似乎会导致更多的问题。更改
fscanf()
调用中的格式也没有帮助

为什么要跳过每行的前三个值

代码:

输出:

p_id: 2
cpu_burst: 2
io_burst: 2
priority: 2

p_id: 1
cpu_burst: 1
io_burst: 1
priority: 1

p_id: 5
cpu_burst: 5
io_burst: 5
priority: 5

p_id: 4
cpu_burst: 4
io_burst: 4
priority: 4

p_id: 0
cpu_burst: 0
io_burst: 0
priority: 0

p_id: 2
cpu_burst: 2
io_burst: 2
priority: 2

p_id: 5
cpu_burst: 5
io_burst: 5
priority: 5

p_id: 5
cpu_burst: 5
io_burst: 5
priority: 5

p_id: 1
cpu_burst: 1
io_burst: 1
priority: 1

p_id: 1
cpu_burst: 1
io_burst: 1
priority: 1

您的程序中有未定义的行为

int process_count, p_id[process_count], io_burst[process_count], cpu_burst[process_count];
...
process_count = atoi(argv[2]);

该代码在初始化之前正在使用
process\u count

您的程序中有未定义的行为

int process_count, p_id[process_count], io_burst[process_count], cpu_burst[process_count];
...
process_count = atoi(argv[2]);
该代码在初始化之前正在使用
进程\u计数。

这是一个错误:

int process_count, p_id[process_count],
process\u count
是一个未初始化的变量,因此它不能用于数组维度(或任何其他真正的维度)

要解决此问题,您可以将代码更改为:

int process_count = atoi(argv[2]);

if ( process_count < 1 )
    exit(EXIT_FAILURE);    // or similar

int p_id[process_count], io_burst[process_count], priority[process_count], cpu_burst[process_count];
int进程计数=atoi(argv[2]);
if(进程计数<1)
退出(退出失败);//或类似
int p_id[进程计数]、io_突发[进程计数]、优先级[进程计数]、cpu_突发[进程计数];
这是一个错误:

int process_count, p_id[process_count],
process\u count
是一个未初始化的变量,因此它不能用于数组维度(或任何其他真正的维度)

要解决此问题,您可以将代码更改为:

int process_count = atoi(argv[2]);

if ( process_count < 1 )
    exit(EXIT_FAILURE);    // or similar

int p_id[process_count], io_burst[process_count], priority[process_count], cpu_burst[process_count];
int进程计数=atoi(argv[2]);
if(进程计数<1)
退出(退出失败);//或类似
int p_id[进程计数]、io_突发[进程计数]、优先级[进程计数]、cpu_突发[进程计数];

如果先将数组归零,会发生什么情况?在使用
process\u count
指定数组的大小之前,需要设置该值。很可能您的数组都是零长度的(尽管这不可靠,因为此时未初始化
process\u count
)。如果您首先将数组归零,会发生什么情况?您需要设置
process\u count
的值,然后再使用它来指定数组的大小。很可能你的数组都是零长度的(虽然这不可靠,因为
process\u count
在这一点上没有初始化)。哇,这就成功了。我真不敢相信我花了多少时间来修复它。谢谢。哇,真是妙不可言。我真不敢相信我花了多少时间来修复它。谢谢