在C中的文本文件中读取,将行分成多个变量
我目前正在开发一个模拟各种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()
可以非常简单地读取文件,因为除第一行标识符之外的所有内容都是数字。 但您确实需要检查从文件中读取的内容的有效性。 我刚刚在出错时使用exit(1)
进行说明,它可能比那更复杂(例如错误消息)。
#include #include #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
节目输出:
Type: SJF Num: 4 1 0 6 1 2 0 8 1 3 0 7 1 4 0 3 1
您将需要使用fscanf和fprintf而不是scanf和printf来从/到文件而不是标准输入/输出执行I / O.
这些被广泛记录。 您将使用FILE *变量和fopen & fclose 。 您甚至可以使用stdin , stdout和stderr作为控制台的句柄。
使用C读取文件中的行的最佳方法是使用getline()
函数。 它比scanf()
更可靠,并且可以自动分配所需的内存。
这是我建议的代码:
#define _POSIX_C_SOURCE 200809L #include #include #include 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; }
请注意,我没有尝试过这段代码......所以,如果某些东西不能正常工作,我会解决它。