0

我需要编写一个程序,该程序可以从文件中读取相关信息并输出维护需求,如以下幻灯片上的示例输出所示。

我已经完成了从 .txt 文件中读取的代码,如下面的屏幕截图所示:

从 .txt 读取

这是我为上述完成的代码(在注释中使用了另一种方法):

#include <stdio.h>
#include <stdlib.h>

#include <stdio.h>
#include <stdlib.h>

int main() {

FILE *cfPtr = fopen("C:\\Users\\David O'Dea\\Desktop\\Test\\MaintenanceSchedule.txt","r");

int line = 0;

char input[255];
while (fgets(input, 255, cfPtr))
{
    //line++;
    printf("%s", input);
}
printf("\n\nEnd of program\n");

fclose(cfPtr);

return 0;
}

//int main() {
//    FILE *cfPtr = fopen("C:\\Users\\David O'Dea\\Desktop\\Test\\MaintenanceSchedule.txt","r");
//C:\\Users\\David O'Dea\\Desktop\\Test\\MaintenanceSchedule.txt

//char c;
//f = fopen("C:\\Users\\David O'Dea\\Desktop\\Test\\MaintenanceSchedule.txt", "rt");

//while ((c = fgetc(f)) != EOF){
//  printf("%c", c);
//}

//fclose(f);
//return 0;
//}

我不确定如何完成从 .xlsx 文件中读取的第二个任务,因此结果如下:

从 .xlsx 读取

4

3 回答 3

2
  • 解决方案 1:搜索可以让您阅读.xlsx文件的库。
  • 解决方案 2:编写自己的.xlsx格式解析器。.xlsx是一种开放格式。.xlsx是一种ooxml格式,所以它本质上是一个zip格式化的压缩文件。您可以扩充文件并检查相关部分(document.xml如果我没记错的话)并解析“.xml”文件。
于 2015-01-22T13:39:05.707 回答
2

使用 C 更好地访问将其转换为 CSV 文件的 excel 数据,然后将其作为 TXT 文件访问。

于 2015-01-22T13:45:31.797 回答
0

因此,讲师似乎将 .xlsx 文件作为显示制表符分隔文件示例的手段。这是完成的:

#include "stdafx.h"
#include <stdio.h>
#include "string.h"

void printService(char *type, char *reg, char serviceType, char *month);

void main()
{ 
    char months[12][12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    char line[201];
    char type[41], reg[41];
    char service[12];
    int i;

    FILE *ptr;  

    /*
    ptr = fopen( "c:\\path\\to\\file.txt", "r" );

   if (ptr == NULL ) 
   {
      printf( "File could not be opened\n" );
   } 
   else 
   { 
        puts("File Contents");
        while(!feof(ptr))
        {
            fgets(line,200,ptr);
            puts(line);
        }

      fclose( ptr ); 
   } 
   */

   ptr = fopen( "c:\\path\\to\\file.txt", "r" );

   if (ptr == NULL ) 
   {
      printf( "File could not be opened\n" );
   } 
   else 
   { 
            fgets(line,200,ptr);
            fgets(line,200,ptr);
            while(strcmp(type,"END"))
            {
                type[0] = '\0';
                fscanf(ptr, "%s\t%s\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\n",type,reg,&service[0],&service[1],&service[2],&service[3],
                &service[4],&service[5],&service[6],&service[7],&service[8],&service[9],&service[10],&service[11]);

                if(strcmp(type,"END"))
                {
                for(i=0;i<12;i++)
                {
                    printService(type,reg,service[i], months[i]);
                }
                }
            }
        }

      fclose( ptr ); 

   } 

void printService(char *type, char *reg,char serviceType, char *month)
{
    char service[41];

    if(serviceType != '-')
    {
        if (serviceType == 'S') strcpy(service,"Service Due");
        if (serviceType == 'I') strcpy(service,"Inspection Service Due");
        if (serviceType == 'C') strcpy(service,"Commercial Vehicle Test Due");
        if (serviceType == 'T') strcpy(service,"Two-year Tacho Check Due");

        printf("%s registration %s, in %s has %s \n",type, reg,month, service);
    }

}

这对你们来说好看吗?有什么提高效率的建议吗?

于 2015-01-23T13:49:33.937 回答