【问题标题】:How to define the struct MAX size to lines scanned from text file如何将 struct MAX 大小定义为从文本文件扫描的行
【发布时间】:2017-02-18 02:20:59
【问题描述】:

我正在尝试读入一个模拟 CPU 调度的文本文件,执行调度算法并打印输出。

即进程id号、到达时间、突发时间

1 0 3
2 4 6
...

如何根据扫描的行数分配要创建的结构的大小?我可以将 MAX 定义为扫描的行数吗?我能够扫描输入文件并输出文件,但是,我将结构的 MAX 变量定义为 10000。问题在于它会在文件中打印正确的输出,但会打印 0 0 0 到 10000输入行停止后的行。这是我的函数。

#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

【问题讨论】:

  • 首先从readtable返回i。并检查 fscanf 返回值:如果它是 != 3 那么你到达了文件末尾。
  • 输出表格时,写的是实际读取的行数,而不是最大容量。

标签: c struct printf scheduling scanf


【解决方案1】:

我会计算文件的行数:

int lines = 0;
while(!feof(fp))
{
  ch = fgetc(fp);
  if(ch == '\n')
  {
    lines++;
  }
}

然后使用malloc在堆上动态创建struct。

怎么做? 该解决方案旨在回答您关于动态分配数组的具体问题。

#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);
}

【讨论】:

  • 如果最大值发生变化怎么办?就像可以使用另一个最大数量一样,我只想扫描并为扫描的行分配足够的内存。我收到带有更正代码的分段错误。
  • @BradVanBuren 该代码对我有用,也许您可​​以将您的输入文件上传到我可以测试的地方? MAX 的想法是您选择一个任意大的数字,该数字始终大于您在数据文件中的预期行数。
猜你喜欢
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多