fopen question?

This is a discussion on fopen question? within the C Programming forums, part of the General Programming Boards category; I have a set of data(integer values) in excel stored on my desktop and the format and the full path ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    fopen question?

    I have a set of data(integer values) in excel stored on my desktop and the format and the full path is "C:\Documents and Settings\USER\Desktop\data.xls" I'm trying to read it and store in an array, but it's not working please can you figure out what's wrong with my code?
    Code:
    #include<stdlib.h>
    #include<stdio.h>
     main(){
            int rows =6;
            int cols=6;
            int i,j;
            int a[rows][cols];
            FILE *infile;
            infile=fopen("C:\Documents and Settings\USER\Desktop\data.xls", "r");
            for(i=0; i<rows; i++)
            {
                     for(j=0; j<cols; j++)
                     {
                      fscanf(infile, "%lf", &a[i][j]);
                     }
            }
              if ((infile=infile=fopen("C:\Documents and Settings\USER\Desktop\data.xls", "r"))== NULL)
              {
              printf("Cannot open file. \n");
              exit(1);
              }
              for(i=0; i<rows; i++)
              for(j=0; j<cols; j++)
              printf("%f\t", a[i][j]);
              printf("\n");
     }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need double the backslashes, as they are an escape in C, so to make one backslash, you need one to form the escape-char, and one to say "this is a backslash".

    Otherwise how does the compiler know the difference between "C:\n" (C:<newline>) and "C:\n" (C:<backslash>n) - it can't. So you need to do "C:\\n" to make the "file n in C:'s root.

    --
    Mats

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    if ((infile=infile=fopen("C:\Documents and Settings\USER\Desktop\data.xls", "r"))== NULL)
    * Why are you opening it twice!?
    * And what do you think 'infile=infile=fopen' will achieve?
    * You also don't close it before your program ends...
    * More so, have you researched the exel file format!? All I can say is 'lol', no.
    * main must return a value, see the FAQ. Try being explicit with your declarations ie, 'int main(void) {'...
    Last edited by zacs7; 08-02-2007 at 05:01 AM.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Quote Originally Posted by matsp View Post
    You need double the backslashes, as they are an escape in C, so to make one backslash, you need one to form the escape-char, and one to say "this is a backslash".

    Otherwise how does the compiler know the difference between "C:\n" (C:<newline>) and "C:\n" (C:<backslash>n) - it can't. So you need to do "C:\\n" to make the "file n in C:'s root.

    --
    Mats
    Thanks I've inserted double slash it's now giving me an out put but not the values I have in my spread sheet. It's giving me 0.00000 0.00000 all through. What else could I be missing?

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    http://chicago.sourceforge.net/devel/docs/excel/ <- reading excel files in various languages
    http://sc.openoffice.org/excelfileformat.pdf <- big fat doc on the excel format

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Quote Originally Posted by zacs7 View Post
    * Why are you opening it twice!?
    * And what do you think 'infile=infile=fopen' will achieve?
    * You also don't close it before your program ends...
    * More so, have you researched the exel file format!? All I can say is 'lol', no.
    * main must return a value, see the FAQ. Try being explicit with your declarations ie, 'int main(void) {'...
    That's a typographical error, I've corrected it and used int main, but I'm still getting 0.0000 values.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Sailors
    That's a typographical error, I've corrected it and used int main, but I'm still getting 0.0000 values.
    Of course you are!
    Research the Excel and BIFF formats.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    int a[rows][cols];
    
    ...
    
    for(i=0; i<rows; i++)
    {
        for(j=0; j<cols; j++)
        {
            fscanf(infile, "%lf", &a[i][j]);
        }
    }
    
    ...
    
    for(i=0; i<rows; i++)
        for(j=0; j<cols; j++)
            printf("%f\t", a[i][j]);
    printf("\n");
    Assuming for the moment you had a simple file to read through, the above parts in red would still be a problem for you. Your array/matrix a contains integers and yet you are using %lf/%f when reading and writing these values.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Thanks but that's still doesn't solve the problem

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Probably because you don't know what you're doing. Do you think Excel files are simply just rows and columns of numbers in a plain file?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Correct, because (I didn't think about that at the beginning) the XLS file is not a text-file that you can just read data from with fscanf(). It has a complex data structure with header information and data in various places - you have two choices:
    1. Save your Excel spreadsheet in a text-format, e.g. comma-separated (.CSV).
    2. Use a library that is capable of reading this format.

    If you have small amounts of data and/or you want to be able to look at the data with a text-editor before/after the read, you're better of with the first form. But some types if data, such as graphs and special stuff may "disappear" if you do that.

    The second option allows you to read "all" the content in an .xls file, but it's harder to figure out what's going wrong when it doesn't work, because you can't trivially look at the data inside the .xls if your .xls read functions aren't doing what they should.

    --
    Mats

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by MacGyver View Post
    Probably because you don't know what you're doing. Do you think Excel files are simply just rows and columns of numbers in a plain file?
    Didn't listen to me, why should he listen to you?

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by zacs7 View Post
    Didn't listen to me, why should he listen to you?
    Because you're just "cool" but I'm a "deathray engineer".


  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    54
    hoping to stay on topic but I have a similar problem ..

    I am trying to open a txt document in my program and enter a large integer into the program ..

    I have found
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        FILE *f;
        f = fopen("c:/dev-cpp/programs/file.txt", "r");
        if( f == NULL ){
            printf("does NOT work\n");
            }
            else{
                 printf("working\n");
                 }
        
        int fclose(FILE *f);
        system("PAUSE");// for use with dev-cpp
        return 0;
        
    }
    
    my problem is finding out information on how to enter the int contained into the txt document into an array ..
    
    Im pretty sure I have to use a string and then convert the string to an integer but any help at all appreciated ..

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depending on the format of your text-file, you have at least two choices:
    1. Read a line in as a string (using fgets() for example), and parse the string - you could use sscanf() for the parsing.
    2. Use fscanf() to read directly from the file.

    You could of course also read char by char with fgetc() and "parse on the fly".

    If you give a small example of your text-file, it may be easier to recommend one method over another.

    --
    Mats

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

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen question
    By DramaKing in forum C Programming
    Replies: 17
    Last Post: 12-19-2001, 10:09 PM

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