0

I need to write a program that can read the relevant information from a file and output the maintenance needs as shown in the sample output on the following slides.

I have completed the code to read from the .txt file as seen in this screen shot:

read from .txt

This is the code I have completed for the above(with an alternate method in comments):

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

I am not sure how to complete the second task of reading in from the .xlsx file so that the result is as below:

read from .xlsx

  • You need to search for a `.xlsx` reading library, they are nothing but `xml` files, which you could also try to read and parse, I have written a library but for `.ods` documents, but I don't know if it would work with `.xlsx`. Why do you want to do it with c, and what is your final goal? There eare python libraries that would allow you to quickly achieve this, but if you need it in an already written application, then the library or parsing the xml is the way to go. – Iharob Al Asimi Jan 22 '15 at 13:39

3 Answers3

2
  • Solution 1: Search for a library that can enable you reading .xlsx file.
  • Solution 2: Write your own parser for .xlsx format. .xlsx is an open format. .xlsx is an ooxml format, so it is essentially a zip formatted compressed file. You can inflate the file and check the releavant section (document.xml if I remember correctly) and parse the ``.xml` file.
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • 2
    I think it's `content.xml` but yeah, this is the right approach one of these two. – Iharob Al Asimi Jan 22 '15 at 13:40
  • Thank @iharob for the correction. I worked on `ooxml` formats back in 2007 and 2008 and many things seem to have slipped now :) – Mohit Jain Jan 22 '15 at 13:42
  • If the OP doesn't care about the styles and such, formulas and other things present in these files, then parsing it would be very easy in fact. – Iharob Al Asimi Jan 22 '15 at 13:44
  • I am just learning C at the moment could you provide a code sample? –  Jan 22 '15 at 13:55
  • 3
    Did you try to rename your `file.xlsx` to `file.xlsx.zip` and unzip it? – Mohit Jain Jan 22 '15 at 14:00
  • 2
    Unzipping and reading file contents *with C* is definitely not for beginners. Are you sure reading .xlsx files is part of your current curriculum? That's a bit steep. – Jongware Jan 22 '15 at 14:38
  • Also parsing XML isn't for beginners. – alk Jan 22 '15 at 16:42
  • @alk Agree with you, but OP clearly mentioned `.xlsx` format. Otherwise as LPs suggested, csv would be a better format. And if OP renames `MaintenanceSchedule.txt` to `MaintenanceSchedule.csv` (as OP appears to be on Windows), it should solve the requirement. – Mohit Jain Jan 23 '15 at 04:43
  • thanks @MohitJain - i have attached the completed code below, could you recommend any improvements? –  Jan 23 '15 at 18:02
2

With C is better to access excel data transforming it to CSV file, and then access it as a TXT file.

LPs
  • 16,045
  • 8
  • 30
  • 61
0

so it seems the lecturer included the .xlsx file as means of showing the example of a tab delimited file. Here is the completed:

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

}

Does this look good to you guys? Is there any efficiency improvements to be suggested?