阅读在C文本文件,独立行到多个变量 [英] Reading in a text file in C, separate lines into multiple variables
问题描述
我目前正在对模拟各种CPU调度方法的程序。目前,我有程序要求输入:
I'm currently working on a program that simulates various CPU scheduling methods. Currently I have the program asking for input:
printf("Enter type of CPU scheduling algorithm (SJF, RR, PR_noPREMP, PR_withPREMP): ");
scanf("%s", typeOf);
printf("Enter number of processes: ");
scanf("%d", &numPro);
struct processStruct structs[numPro];
int burstTimes[numPro];
for (i = 0; i < numPro; i++) {
printf("Enter process number: ");
scanf("%d", &structs[i].pNum);
printf("Enter arrival time: ");
scanf("%d", &structs[i].arTime);
printf("Enter CPU burst time: ");
scanf("%d", &structs[i].cpuBur);
printf("Enter priority: ");
scanf("%d", &structs[i].prio);
}
在除了两个变量的typeof(一个int)和numPro(字符数组)我也使用的数据结构。
In addition to the two variables typeOf (an int) and numPro (a char array) I am also using a data structure.
下面是抱着各种参数的数据结构:
Here is the data structure that is holding the various parameters:
struct processStruct {
int pNum;
int arTime;
int cpuBur;
int prio;
int waitTim;
};
代替手工输入的,我可以喜欢用一个文本文件相同的信息作为程序的输入。该文本文件看起来是这样的:
Instead of manual input I could like to use a text file with the same information as input for the program. The text file would look something like this:
SJF
4
1 0 6 1
2 0 8 1
3 0 7 1
4 0 3 1
第一行是调度算法的名称。
第二行是工序数。
以下行由每个过程的信息。所以,1 0 6 1 =进程= 1,0 =到达时间,6 = CPU一阵时间,1 =优先
First line is the name of the scheduling algorithm. Second line is the number of processes. The following lines consists of information for each process. So 1 0 6 1 = Process = 1, 0 = Arrival Time, 6 = CPU burst time, 1 = Priority
我使用与C文本文件输入有没有人有关于如何我可以将数据从文本文件读入的变量和数据结构的想法很遗憾没有经验?
I unfortunately have little experience using text file input with C. Does anyone have ideas about how I could read in the data from the text file into the variables and data structure?
感谢您
编辑:一个的我有问题的是,该数据是不为每一行是相同的。如果这只是4个数字的行,那么这将是比较容易的。我需要的程序读取第一行到一个char阵列(串),第二进numPro变量然后将后面的行进的数据结构(每个处理)。
One of the issues I am having is that the data is not the same for each line. If it was just the rows of 4 numbers then it would be relatively easy. I need the program to read the first line into a char array (string), the second into the numPro variable then the subsequent lines into multiple instances of the data structure (one for each process).
推荐答案
该文件可以非常简单地用的fscanf()
因为除了第一行标识符一切都是读数。但是,你需要检查什么是从文件中读取的有效性。我刚才用退出(1)
的错误说明,这可能是比这更复杂(例如错误消息)。
The file can be read fairly simply with fscanf()
because everything except the first line identifier is a number. But you do need to check the validity of what is read from the file. I have just used exit(1)
on error for illustration, it could be more sophisticated than that (for example an error message).
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct processStruct {
int pNum;
int arTime;
int cpuBur;
int prio;
int waitTim;
};
struct processStruct structs[MAX];
int main(int argc, char** args)
{
FILE *fil;
char typeOf[4];
int numPro, i;
if ((fil = fopen("myfile.txt", "rt")) == NULL)
exit(1);
if(fscanf(fil, "%4s", typeOf) != 1)
exit(1);
if(fscanf(fil, "%d", &numPro) != 1)
exit(1);
if(numPro > MAX)
exit(1);
for(i=0; i<numPro; i++) {
if(fscanf(fil, "%d%d%d%d", &structs[i].pNum, &structs[i].arTime,
&structs[i].cpuBur, &structs[i].prio) != 4)
exit(1);
}
fclose(fil);
// test the result
printf("Type: %s\n", typeOf);
printf("Num: %d\n", numPro);
for(i=0; i<numPro; i++) {
printf("%d %d %d %d\n", structs[i].pNum, structs[i].arTime,
structs[i].cpuBur, structs[i].prio);
}
return 0;
}
程序输出:
Type: SJF
Num: 4
1 0 6 1
2 0 8 1
3 0 7 1
4 0 3 1
这篇关于阅读在C文本文件,独立行到多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!