[英]Reading in a text file in C, separate lines into multiple variables
我目前正在开发一个模拟各种 CPU 调度方法的程序。 目前我有程序要求输入:
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(一个 char 数组)这两个变量之外,我还使用了一个数据结构。
这是保存各种参数的数据结构:
struct processStruct {
int pNum;
int arTime;
int cpuBur;
int prio;
int waitTim;
};
代替手动输入,我想使用与程序输入具有相同信息的文本文件。 文本文件看起来像这样:
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 = 优先级
不幸的是,我几乎没有使用 C 语言输入文本文件的经验。有没有人知道如何将文本文件中的数据读入变量和数据结构?
谢谢
编辑:我遇到的问题之一是每行的数据都不相同。 如果它只是 4 个数字的行,那么它会相对容易。 我需要程序将第一行读入 char 数组(字符串),第二行读入 numPro 变量,然后将后续行读入数据结构的多个实例(每个进程一个)。
您将需要使用fscanf和fprintf而不是scanf和printf来执行来自/到文件的 I/O,而不是标准输入/输出。
这些被广泛记录。 您将使用FILE *变量和fopen & fclose 。 您甚至可以使用stdin 、 stdout和stderr作为控制台的句柄。
使用fscanf()
可以相当简单地读取文件,因为除了第一行标识符之外的所有内容都是数字。 但是您确实需要检查从文件中读取的内容的有效性。 我刚刚在错误中使用了exit(1)
进行说明,它可能比这更复杂(例如错误消息)。
#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 读取文件中的行的最佳方法是使用getline()
函数。 它比scanf()
可靠得多,并且可以自动分配所需的内存。
这是我建议的代码:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char **argv)
{
FILE *inputfile;
if (argc < 2)
{
fprintf (stderr, "error: missing file name in the arguments\n");
exit (EXIT_FAILURE);
}
inputfile = fopen (argv[1], "r");
if (!inputfile)
{
perror ("myprogram");
exit (EXIT_FAILURE);
}
/* Getting first line */
char *line = NULL;
size_t line_size = 0;
if (!getline(&line, &line_size, inputfile))
{
fprintf (stderr, "error: failed to read first line\n");
exit (EXIT_FAILURE);
}
/* Getting the size of the matrix */
unsigned int size; /* Size of the matrix */
if (sscanf (line, "%zd", &size) != 1)
{
fprintf (stderr, "error: first line has wrong format\n");
exit (EXIT_FAILURE);
}
/* Getting the content of the matrix */
int matrix[size][size];
for (unsigned int i = 0; i < size; i++)
{
if (!getline (&line, &line_size, inputfile))
{
fprintf (stderr, "error: input file has wrong format\n");
exit (EXIT_FAILURE);
}
/* Copying the items of the line to the matrix */
char *ptr = line;
for (unsigned j = 0; j < size; j++)
{
matrix[i][j] = atoi(strtok(ptr, " "));
ptr = NULL;
}
}
free(line);
return EXIT_SUCCESS;
}
当心,我没有试过这个代码...所以,如果有什么地方不能正常工作,我会修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.