fopen and fclose functions

This is a discussion on fopen and fclose functions within the C Programming forums, part of the General Programming Boards category; Hi , How can i make use of fopen() and fclose() functions in c and use .doc ( word documents ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    14

    fopen and fclose functions

    Hi ,

    How can i make use of fopen() and fclose() functions in c and use .doc ( word documents ) to display the same on screen .

    Wht i mean to say is , I am able to display the text present in the files with the extention .txt but when i give the syntax as

    f1=fopen("c:\\copy.doc",r) it does'nt give an error but the contents of the files are not displayed

    pls help

    regards
    Anil

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Microsoft .doc files are not text files, they contain a lot of additional information as well (such as formatting) which is not displayable as text.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    14
    hi ,

    Thx for the reply ,

    But then whts the solution , how do i display those doc files on the screen .

    The whole purpose of doing this is
    in the word doc i have made use of "word Art" and increased font and have some design patters " which whenever a user executes my program that design pattern shd get executed first and then the program

    I guess this can be done via fopen ?

    regards
    Anil

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by Salem
    Microsoft .doc files are not text files, they contain a lot of additional information as well (such as formatting) which is not displayable as text.
    So in other words, Microsoft .doc files stored in binary format?

    If so, then it should be like

    Code:
    f1=fopen("c:\\copy.doc",rb);
    if(f1 == NULL)
     exit(1);
    not
    Code:
    f1=fopen("c:\\copy.doc",r);

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    the easiest solution is to let MS Word display the doc. If the name of the file is "MyArt.doc"
    Code:
    system("MyArt.doc");

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    14
    Hi ,

    had even tired that .
    it does'nt work

    wht could be the other possiblity
    pls help

    Thanks

    regards
    Anil

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Which OS and compiler do you have?
    Do you even have MS Word installed on your machine?

    If all you really want to do is view a word document without spending $$$ on MS Word, then there is a free viewer for word documents.
    http://www.microsoft.com/downloads/d...displaylang=en

    > So in other words, Microsoft .doc files stored in binary format?
    For the most part, yes.
    You can see your actual text using a hex editor, but there is a lot of other stuff in there as well. Being a closed format, you'll find it hard to figure out what it all means, or you'll have to spend lots of $$$ getting an API from Microsoft to allow you to manipulate word documents from programs.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    14
    Ohh , I guess i have got all of u a bit confused ,

    my requirement is i need to open a word doc in C in a much similar manner we open a txt file

    This is my code
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>
    
    void main()
    {
    int i;
    char a[100];
    char ch;
    FILE *fp1;
    clrscr();
    fp1=system("c:\\tic.doc","rb");
    while(1)
    {
    if(ch==EOF)
    break;
    else
    ch=fgetc(fp1);
    printf("%c",ch);
    }
    
    }
    in the tic.txt file i have written "tic tac to" and when i run i am able to see "tic tac to" as the output .

    Now when i create a file tic.doc and write some text in increased font and with 1 picture in it and in the program i mention tic.doc instead of tic.txt i am able to see some
    ascii values and junk charecters insead of the required output .


    Kindly suggest

    Regards
    Anil

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If so, then it should be like

    Code:
    f1=fopen("c:\\copy.doc",rb);
    if(f1 == NULL)
     exit(1);
    not
    Code:
    f1=fopen("c:\\copy.doc",r);
    You need double quotes around your file modes: "rb". It's a string.

    Do not use void main -- use int main() and see the FAQ if you don't know why.

    DOC files have tons of formatting in them. You can't hope to display them properly unless you know how to remove the formatting. Stick with .TXT files.

    Code:
    fp1=system("c:\\tic.doc","rb");
    Use fopen(), not system().
    Code:
    fp1=fopen("c:\\tic.doc","rb");
    ch isn't initialized, and you check it for EOF. Turn on the warnings.

    What's a[] for? And i?
    Code:
    #include <dos.h>
    What compiler are you using . . . Turbo C?

    my requirement is i need to open a word doc in C in a much similar manner we open a txt file
    As maybe you can see, you can't do this.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    There is much to it: write your own Word. Here's the format.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Perhaps learning to program with a decent compiler would be a start.
    There's a good handful of mistakes already.

    It's a DOS program for heavens sake, what on earth were you planning to do with fonts and images for example?

    > Ohh , I guess i have got all of u a bit confused ,
    Not at all, you're attempting to decode the internal format of a word document - which I'm saying is a shed load of hard work if you don't know the format of Word documents.

    Try http://www.openoffice.org/product/writer.html
    It's open source, but I'm sure you could dig your way through all that source to find the bits which interest you.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    With code like this?
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>
    
    void main()
    {
    int i;
    char a[100];
    char ch;
    FILE *fp1;
    clrscr();
    fp1=system("c:\\tic.doc","rb");
    while(1)
    {
    if(ch==EOF)
    break;
    else
    ch=fgetc(fp1);
    printf("%c",ch);
    }
    
    }
    Well, maybe, but first you should look at this program:
    Code:
    #include <stdio.h>
    
    int main(void) {  /* main returns int, see the FAQ */
        int c;  /* EOF is an int value, you can't store it in a char */
    
        for(;;) {  /* same as while(1) */
            c = getchar();  /* getchar() is the same as getc(stdin) */
            if(c == EOF) break;  /* check for EOF before you print it */
            putchar(c);  /* same as printf("%c", c) */
        }
    
        return 0; /* not required (even for int main), but you should have it */
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > return 0; /* not required (even for int main), but you should have it */
    This only works in new C++ compilers.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    > return 0; /* not required (even for int main), but you should have it */
    This only works in new C++ compilers.
    It's part of the C99 standard.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Where does it say that?

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

Similar Threads

  1. fclose slow
    By Rmantingh in forum C Programming
    Replies: 4
    Last Post: 04-08-2010, 11:01 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fopen, fclose
    By Denethor2000 in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2004, 10:11 PM
  4. fclose()
    By GanglyLamb in forum C Programming
    Replies: 4
    Last Post: 11-11-2002, 07:45 AM
  5. close and fclose
    By Ian in forum C Programming
    Replies: 4
    Last Post: 12-13-2001, 04:31 PM

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