Reading xls files

This is a discussion on Reading xls files within the C Programming forums, part of the General Programming Boards category; Code: void the_file_name(char *filename){ int length; do { printf("Enter a filename with .xls at the end please. "); fflush(stdout); scanf("%s", ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void the_file_name(char *filename){
        int length;
    
        do {
          printf("Enter a filename with .xls at the end please. ");
          fflush(stdout);
          scanf("%s", filename);
          length = strlen(filename);
        }
        while(filename[len-1] != 's' || filename[length-2] != 'l' ||
            filename[len-3] != 'x' || filename[length-4] != '.');
    //FIINISHED
    }
    shouldn't you check that it ends with ".csv" if that is the file format?

    And you could use strcmp(&filename[len-4], ".csv") != 0 to check if it's a mis-match. Of course, I personally prefer that applications just take a filename as I type it - or at least, if it's got an extension, that it's not adding another one - say I saved my file as "list.txt", but it's really a .csv file, why should your application NOT work with that?

    Not sure if this is just a place-holder, but "%a" is not a standard format specifier:
    Code:
    fscanf(fp, "%a", &a)
    It is generally not a great idea to read the file to find out how many lines it has, then allocate memory, and then read the file again. If you fell you MUST do this, why not use fgets() with a long buffer - at least then you will be reading reasonably efficiently, where as fscanf() is a pretty processor intensive way to read a file. [No, you probably won't be able to measure the difference on a file less than a few megabytes, but if no one tells you until you are wondering why your mega-huge file takes ages to read, how are you going to know the right/wrong way to do it?]

    There are two ways I'd prefer to do it:
    Method A:
    * Have a linked list of projects (or a fixed array of "too large", e.g. 300 projects as a maximum).
    * Read in a line of the file, parse the name and score. Check the project list. If it's in the list, check if the score is higher: Replace, otherwise don't do anything. If it's not in the list, it's obviously a new highest score, so add it to the list.

    Method B:
    * Start with mallocing a "person array" decent number of person slots, say 100. Set a size variable to 100.
    * While not end of file:
    * Read a line.
    * check if there is space in "person array", then put it in the person array. If person array is full, use realloc to double it's size (set size variable to 2 x size), and insert it in one of the new free slots.

    A note on style, you are inconistent in your file naming, choose one or the other:
    Code:
    readFileIntoArray
    read_the_file_to_array

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    haha, sorry, I was typing it from memorization. I'm at work at the moment, hehe. Also working with textedit at the moment, to see how it will hold me up. Soon I'll be home and then the real deal starts, thanks for your insight and your quick responses. Been very helpful.

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by rdrast View Post
    Microsoft does post the details for their file formats, check here:
    http://www.microsoft.com/interop/doc...ryformats.mspx
    Aren't you a handy person

  4. #19
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Wouldn't it be a lot simpler to use a Windows oriented approach such as MFC's CDatabase class?

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BobS0327 View Post
    Wouldn't it be a lot simpler to use a Windows oriented approach such as MFC's CDatabase class?
    Except that means understanding how to use MFC, which is a different kettle of fish.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    So after I read the xls, how do I make it read the columns, there are total of 8 columns, One for the names, and 7 for the 7 projects. I was thinking of just going through the excel file and just saying finding the highest score on each project, but I was wondering if I can write code for this, so here we are ...
    Yes, you can write a csv parser but it's not a trivial task.

    An abbreviated description of a csv structure is as follows:

    Each record is one line (with exceptions)
    Fields are separated with commas
    Leading and trailing space-characters adjacent to comma field separators are ignored
    Fields with embedded commas must be delimited with double-quote characters
    Fields that contain double quote characters must be surounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.
    A field that contains embedded line-breaks must be surounded by double-quotes
    Fields with leading or trailing spaces must be delimited with double-quote characters
    Fields may always be delimited with double quotes
    The first record in a CSV file may be a header record containing column (field) names
    Post a few lines of your csv file so that we can see what you're working with.

    BTW, the previouse code example I posted used an ODBC driver to query the excel file. ODBC also has a generic text driver that would make parsing a csv file extremely simple.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  2. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 12:40 PM
  3. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 12:58 PM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 08:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21