how to use fopen()?

Discussion in 'C Programming' started by Taras, Oct 4, 2003.

  1. Taras

    Taras Guest

    I wrote following program:

    #include <stdio.h>

    main()
    {
    FILE *file;

    file=fopen(d:\readme.txt,"r");
    if (file==NULL)
    printf("**** it's not working");
    else
    printf("finaly");
    }


    My problem is that the only programs' result is : "**** it's not working"
    What do I wrong?
     
    Taras, Oct 4, 2003
    #1
    1. Advertising

  2. In article <>, Taras wrote:
    > I wrote following program:
    >
    > #include <stdio.h>
    >
    > main()


    int main(void)

    > {
    > FILE *file;
    >
    > file=fopen(d:\readme.txt,"r");
    > if (file==NULL)
    > printf("**** it's not working");
    > else
    > printf("finaly");


    fclose(file);
    return 0;

    > }
    >
    >
    > My problem is that the only programs' result is : "**** it's not working"


    I doubt it's even that, you have a syntax error on the line

    file=fopen(d:\readme.txt,"r");

    Try writing it as

    file = fopen("d:/readme.txt", "r");

    instead, or escape the backslash:

    file = fopen("d:\\readme.txt", "r");


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 4, 2003
    #2
    1. Advertising

  3. Taras

    Fao, Sean Guest

    "Taras" <> wrote in message
    news:...
    > I wrote following program:
    >
    > #include <stdio.h>
    >
    > main()
    > {
    > FILE *file;
    >
    > file=fopen(d:\readme.txt,"r");


    file=fopen("d:\readme.txt", "r");

    > if (file==NULL)
    > printf("**** it's not working");
    > else
    > printf("finaly");
    > }
    >
    >
    > My problem is that the only programs' result is : "**** it's not working"
    > What do I wrong?
     
    Fao, Sean, Oct 4, 2003
    #3
  4. Taras

    Fao, Sean Guest

    "Fao, Sean" <-WANT-NO-SPAM> wrote in message
    news:rKEfb.3997$...
    >
    > "Taras" <> wrote in message
    > news:...
    > > I wrote following program:
    > >
    > > #include <stdio.h>
    > >
    > > main()
    > > {
    > > FILE *file;
    > >
    > > file=fopen(d:\readme.txt,"r");

    >
    > file=fopen("d:\readme.txt", "r");
    >
    > > if (file==NULL)
    > > printf("**** it's not working");
    > > else
    > > printf("finaly");


    /* Oops, I missed this */
    fclose(file);
    return 0;

    > > }
     
    Fao, Sean, Oct 4, 2003
    #4
  5. Taras

    Fao, Sean Guest

    "Taras" <> wrote in message
    news:...
    > I wrote following program:
    >
    > #include <stdio.h>
    >
    > main()
    > {
    > FILE *file;
    >
    > file=fopen(d:\readme.txt,"r");
    > if (file==NULL)
    > printf("**** it's not working");
    > else
    > printf("finaly");
    > }


    Ok, this is the last time that I fix code when I've only had 2 hours of
    sleep. Ignore everything I've written up to this point and read what the
    other poster wrote.
     
    Fao, Sean, Oct 4, 2003
    #5
  6. Taras

    Guest

    On 4-Oct-2003, Andreas Kahari <> wrote:

    > In article <>, Taras wrote:
    > > I wrote following program:
    > >
    > > #include <stdio.h>
    > >
    > > main()

    >
    > int main(void)
    >
    > > {
    > > FILE *file;
    > >
    > > file=fopen(d:\readme.txt,"r");
    > > if (file==NULL)
    > > printf("**** it's not working");
    > > else
    > > printf("finaly");

    >
    > fclose(file);
    > return 0;
    >
    > > }



    Do we *have* to point out the int main(void) return 0 etc things ?
     
    , Oct 4, 2003
    #6
  7. "" <> wrote in
    news: on Sat 04 Oct 2003
    01:11:21p:

    >
    > Do we *have* to point out the int main(void) return 0 etc things ?


    Yes. Any other declaration for main() is nonstandard and is erroneous. For
    example, void main() is incorrect despite its popularity among the less
    intelligent of the book and tutorial writers.
     
    August Derleth, Oct 4, 2003
    #7
  8. Taras

    Mike Wahler Guest

    <> wrote in message
    news:...

    >
    > Do we *have* to point out the int main(void) return 0 etc things ?


    No, of course you don't have to.
    But those who want to convey correctness will.
    I seems that many instructors don't, through
    ignorance, apathy, or whatever.


    -Mike
     
    Mike Wahler, Oct 4, 2003
    #8
  9. Taras

    Taras Guest

    Andreas Kahari <> wrote in message news:<>...
    > In article <>, Taras wrote:
    > > I wrote following program:
    > >
    > > #include <stdio.h>
    > >
    > > main()

    >
    > int main(void)
    >
    > > {
    > > FILE *file;
    > >
    > > file=fopen(d:\readme.txt,"r");
    > > if (file==NULL)
    > > printf("**** it's not working");
    > > else
    > > printf("finaly");

    >
    > fclose(file);
    > return 0;
    >
    > > }
    > >
    > >
    > > My problem is that the only programs' result is : "**** it's not working"

    >
    > I doubt it's even that, you have a syntax error on the line
    >
    > file=fopen(d:\readme.txt,"r");
    >
    > Try writing it as
    >
    > file = fopen("d:/readme.txt", "r");
    >
    > instead, or escape the backslash:
    >
    > file = fopen("d:\\readme.txt", "r");



    Thank you , now it at last writes finally!
     
    Taras, Oct 5, 2003
    #9
  10. In article <>,
    Taras wrote:
    > Andreas Kahari <> wrote in message
    > news:<>...

    [cut]
    >> Try writing it as
    >>
    >> file = fopen("d:/readme.txt", "r");
    >>

    >
    > Thank you , now it at last writes finally!



    Reads, I hope.


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 5, 2003
    #10
  11. Andreas Kahari <> scribbled the following:
    > In article <>,
    > Taras wrote:
    >> Andreas Kahari <> wrote in message
    >> news:<>...

    > [cut]
    >>> Try writing it as
    >>>
    >>> file = fopen("d:/readme.txt", "r");

    >>
    >> Thank you , now it at last writes finally!


    > Reads, I hope.


    No, "writes" is correct. The OP's code:
    ---------------------------------------------------------------
    #include <stdio.h>

    main()
    {
    FILE *file;

    file=fopen(d:\readme.txt,"r");
    if (file==NULL)
    printf("**** it's not working");
    else
    printf("finaly");
    }
    ----------------------------------------------------------------
    See the last printf() statement.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
     
    Joona I Palaste, Oct 5, 2003
    #11
  12. In article <blp52m$cmm$>, Joona I Palaste wrote:
    > Andreas Kahari <> scribbled the following:
    >> In article <>,
    >> Taras wrote:
    >>> Andreas Kahari <> wrote in message

    [cut]
    >>>> file = fopen("d:/readme.txt", "r");
    >>> Thank you , now it at last writes finally!

    >> Reads, I hope.

    > No, "writes" is correct. The OP's code:

    [cut]
    > See the last printf() statement.



    No, that is possibly "displays", but I was concentrating more on
    the fopen(), with the "r" mode.


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 5, 2003
    #12
  13. Taras

    Micah Cowan Guest

    Andreas Kahari <> writes:

    > In article <blp52m$cmm$>, Joona I Palaste wrote:
    > > Andreas Kahari <> scribbled the following:
    > >> In article <>,
    > >> Taras wrote:
    > >>> Andreas Kahari <> wrote in message

    > [cut]
    > >>>> file = fopen("d:/readme.txt", "r");
    > >>> Thank you , now it at last writes finally!
    > >> Reads, I hope.

    > > No, "writes" is correct. The OP's code:

    > [cut]
    > > See the last printf() statement.

    >
    >
    > No, that is possibly "displays", but I was concentrating more on
    > the fopen(), with the "r" mode.
    >


    You were concentrating wrong. And recall that the C standard has
    no notion of "displays". He said what he meant, but he should
    have used quotations, and the code's spelling should have been
    fixed.

    ...now it at last writes, "finally"!

    -Micah
     
    Micah Cowan, Oct 6, 2003
    #13
  14. Taras

    Ashish Guest

    "Fao, Sean" <-WANT-NO-SPAM> wrote in message
    news:rKEfb.3997$...
    >
    > "Taras" <> wrote in message
    > news:...
    > > I wrote following program:
    > >
    > > #include <stdio.h>
    > >
    > > main()
    > > {
    > > FILE *file;
    > >
    > > file=fopen(d:\readme.txt,"r");

    >
    > file=fopen("d:\readme.txt", "r");


    file = fopen("d:\\readme.txt", "r");

    :)
     
    Ashish, Oct 6, 2003
    #14
  15. "Micah Cowan" <> wrote in message
    news:...
    ....
    > You were concentrating wrong. And recall that the C standard has
    > no notion of "displays".


    5.2.2 Character display semantics

    It does have *some* notion.

    --
    Peter
     
    Peter Nilsson, Oct 6, 2003
    #15
  16. Taras

    goose Guest

    August Derleth <libertarian232003**@onewest.net> wrote in message news:<Xns940A9EF6CD3CFlibertarianonewestne@63.223.6.93>...
    >
    > Yes. Any other declaration for main() is nonstandard and is erroneous.


    unless, of course, its documented by the implementation.

    > For
    > example, void main() is incorrect despite its popularity among the less
    > intelligent of the book and tutorial writers.



    goose,
    sometimes it is documented ;-)
     
    goose, Oct 7, 2003
    #16
  17. Taras

    Dan Pop Guest

    In <> (goose) writes:

    >August Derleth <libertarian232003**@onewest.net> wrote in message news:<Xns940A9EF6CD3CFlibertarianonewestne@63.223.6.93>...
    >>
    >> Yes. Any other declaration for main() is nonstandard and is erroneous.

    >
    >unless, of course, its documented by the implementation.


    And if the implementation in question claims C99 conformance. In C89,
    it doesn't make any difference whether the implementation documents other
    main intefaces or not.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 7, 2003
    #17
  18. Taras

    goose Guest

    (Dan Pop) wrote in message news:<blu57q$kr0$>...
    > In <> (goose) writes:
    >
    > >August Derleth <libertarian232003**@onewest.net> wrote in message news:<Xns940A9EF6CD3CFlibertarianonewestne@63.223.6.93>...
    > >>
    > >> Yes. Any other declaration for main() is nonstandard and is erroneous.

    > >
    > >unless, of course, its documented by the implementation.

    >
    > And if the implementation in question claims C99 conformance. In C89,
    > it doesn't make any difference whether the implementation documents other
    > main intefaces or not.
    >


    I'm not sure i follow. the c99 standard says "or some other
    implementation-defined manner" after stating the legal return
    values and arguments. does the c89 standard say differently with
    regard to this ? I would like to see a quote (or perhaps a link).


    tia
    goose,
    (I dont have the c89 standard, i *do* have c99 and the n869 file)
     
    goose, Oct 7, 2003
    #18
  19. On Sat, 04 Oct 2003 08:55:51 -0700, Taras wrote:

    > I wrote following program:
    >
    > #include <stdio.h>
    >
    > main()
    > {
    > FILE *file;
    >
    > file=fopen(d:\readme.txt,"r");
    > if (file==NULL)
    > printf("**** it's not working");
    > else
    > printf("finaly");
    > }


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

    int main(void)
    {
    FILE *file;
    file = fopen ("d:/reame.txt", "r");
    if (file == NULL)
    printf ("f*** it's not working\n");
    else {
    printf ("finally\n");
    fclose (f);
    }
    return EXIT_SUCCESS;
    }

    I'd say \n is nice if you wish to have portable output, also EXIT_SUCCESS
    is far better than guessing some popular value.

    Regards

    Zygmunt Krynicki
     
    Zygmunt Krynicki, Oct 7, 2003
    #19
  20. Taras

    Dan Pop Guest

    In <> (goose) writes:

    > (Dan Pop) wrote in message news:<blu57q$kr0$>...
    >> In <> (goose) writes:
    >>
    >> >August Derleth <libertarian232003**@onewest.net> wrote in message news:<Xns940A9EF6CD3CFlibertarianonewestne@63.223.6.93>...
    >> >>
    >> >> Yes. Any other declaration for main() is nonstandard and is erroneous.
    >> >
    >> >unless, of course, its documented by the implementation.

    >>
    >> And if the implementation in question claims C99 conformance. In C89,
    >> it doesn't make any difference whether the implementation documents other
    >> main intefaces or not.
    >>

    >
    >I'm not sure i follow. the c99 standard says "or some other
    >implementation-defined manner" after stating the legal return
    >values and arguments. does the c89 standard say differently with
    >regard to this ?


    Yes. Otherwise, I wouldn't have mentioned C99 conformance.

    >I would like to see a quote (or perhaps a link).


    2.1.2.2 Hosted environment

    A hosted environment need not be provided, but shall conform to the
    following specifications if present.

    "Program startup"

    The function called at program startup is named main. The
    implementation declares no prototype for this function. It can be
    defined with no parameters:

    int main(void) { /*...*/ }

    or with two parameters (referred to here as argc and argv, though any
    names may be used, as they are local to the function in which they are
    declared):

    int main(int argc, char *argv[]) { /*...*/ }

    If they are defined, the parameters to the main function shall obey
    the following constraints:

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 8, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nonee
    Replies:
    2
    Views:
    2,659
    Neredbojias
    Oct 25, 2005
  2. Dave O'Hearn
    Replies:
    10
    Views:
    626
    Dave O'Hearn
    Nov 25, 2003
  3. Kushal Agarwal

    fopen / OPEN_MAX

    Kushal Agarwal, Sep 17, 2004, in forum: C++
    Replies:
    2
    Views:
    611
    Greg Comeau
    Sep 17, 2004
  4. Mister Zimbu
    Replies:
    2
    Views:
    523
    Mister Zimbu
    Oct 27, 2004
  5. Michel Rouzic
    Replies:
    4
    Views:
    1,837
    Michel Rouzic
    Apr 28, 2008
Loading...

Share This Page