在C中的文本文件中读取,将行分成多个变量

时间:2015-11-28 17:32:42

标签: c file text input text-files

我目前正在开发一个模拟各种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变量,然后将后续行读入数据结构的多个实例(每个进程一个)。

3 个答案:

答案 0 :(得分:2)

您需要使用 fscanf fprintf 而不是 scanf printf 来执行I / O操作/到文件而不是标准输入/输出。

这些都被广泛记录。您将使用 FILE * 变量和 fopen &amp;的 FCLOSE 即可。您甚至可以使用 stdin stdout stderr 作为控制台的句柄。

答案 1 :(得分:2)

使用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

答案 2 :(得分: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;
}

请注意,我没有尝试过这段代码......所以,如果某些内容无法正常运行,我会修复它。

相关问题