我正在尝试读取模拟CPU调度的文本文件,执行调度算法,并输出输出。
即进程ID号,到达时间,突发时间
1 0 3
2 4 6
...
如何使用扫描的行数指定要创建的结构的大小?我可以将MAX定义为扫描的行数吗?我可以扫描输入文件并输出文件,但是,我将结构的MAX变量定义为10000。问题是它会将正确的输出打印到文件中,但最多打印10000到0 0 0输入行停止后的第几行。这是我的功能。
#include<stdio.h>
#include<stdlib.h>
#define MAX 10000
typedef struct
{
int pid;
int arrTime;
int burTime;
int finTime;
int waitTime;
int turnTime;
}Process;
Process pTable[MAX];
void readTable(char *fileName, Process pTable[MAX])
{
int i;
FILE *fileIN = fopen(fileName, "r+");
while(!feof(fileIN))
{
fscanf(fileIN, "%d %d %d", &pTable[i].pid, &pTable[i].arrTime, &pTable[i].burTime);
i++;
}
fclose(fileIN);
}
void printTable(char *fileName, Process pTable[MAX])
{
FILE *fileOUT = fopen(fileName,"w+");
for(int i=0; i < MAX;i++)
{
fprintf(fileOUT, "%d %d %d\n",pTable[i].pid, pTable[i].arrTime, pTable[i].burTime);
}
}
int main(int argc, char **argv)
{
readTable(argv[1], pTable);
printTable(argv[2], pTable);
}
这是我为缩短的输入文件提供的输出。
1 0 3
2 4 6
3 9 3
4 12 8
5 13 11
6 18 19
7 19 2
8 23 4
9 28 1
10 31 3
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
最佳答案
我会计算文件的行数:
int lines = 0;
while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}
然后使用
malloc
在堆上动态创建结构。怎么做?
该解决方案是回答关于动态分配数组的特定问题。
#include<stdio.h>
#include<stdlib.h>
typedef struct PROCESS
{
int pid;
int arrTime;
int burTime;
int finTime;
int waitTime;
int turnTime;
} process_t;
process_t** pTable;
int numLines = 0;
int getNumLines(char* fileName) {
int lines = 0;
int ch;
FILE *fp = fopen(fileName, "r+");
while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}
return lines;
}
void readTable(char *fileName)
{
/** Create process table **/
pTable = malloc(sizeof *pTable);
numLines = getNumLines(fileName);
//TODO check numLines > 0
numLines++; //because we counted from 0!
int i;
for(i = 0; i < numLines; i++) {
pTable[i] = malloc(sizeof(*pTable));
if(!pTable[i]) {
printf("Can not allocate memory for pTable!\r\n");
exit(-1);
}
}
/** Do your stuff **/
FILE *fileIN = fopen(fileName, "r+");
i = 0; //
while(i < numLines) //feof is problematic!
{
fscanf(fileIN, "%d %d %d", &pTable[i]->pid, &pTable[i]->arrTime,
&pTable[i]->burTime);
i++;
}
fclose(fileIN);
}
void printTable(char *fileName)
{
FILE *fileOUT = fopen(fileName,"w+");
//numLines must have been filled in a previous function
int i;
for(i=0; i < numLines;i++)
{
fprintf(fileOUT, "%d %d %d\n", pTable[i]->pid, pTable[i]->arrTime,
pTable[i]->burTime);
}
}
int main(int argc, char **argv)
{
readTable(argv[1]);
printTable(argv[2]);
return 0;
}
什么是最好的?
如Mr. Fabre所述,如果现在您的MAX值始终大于输入文件中的预期行,则可以。在这种情况下,您只需将在
readTable
函数中读取的行数传递给printTable
函数,然后将其替换为正在执行循环条件的MAX。这是使用足够大的MAX的解决方案:
#include<stdio.h>
#include<stdlib.h>
#define MAX 10000 //large enough number
typedef struct
{
int pid;
int arrTime;
int burTime;
int finTime;
int waitTime;
int turnTime;
}Process;
Process pTable[MAX];
//returns number of lines read
int readTable(char *fileName, Process pTable[MAX])
{
int i = 0; //always init your variables or else you get garbage results
FILE *fileIN = fopen(fileName, "r+");
while(!feof(fileIN))
{
fscanf(fileIN, "%d %d %d", &pTable[i].pid, &pTable[i].arrTime, &pTable[i].burTime);
i++;
}
fclose(fileIN);
return i; //number of processed lines
}
void printTable(char *fileName, Process pTable[MAX], int numLines)
{
FILE *fileOUT = fopen(fileName,"w+");
for(int i=0; i < numLines;i++)
{
fprintf(fileOUT, "%d %d %d\n",pTable[i].pid,
pTable[i].arrTime, pTable[i].burTime);
}
}
int main(int argc, char **argv)
{
int linesProcessed = readTable(argv[1], pTable);
printTable(argv[2], pTable, linesProcessed);
}
关于c - 如何将struct MAX大小定义为从文本文件扫描的行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39945108/