um canīt understand why this code isnīt working...

This is a discussion on um canīt understand why this code isnīt working... within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stslib.h> int main (void){ char ch[20]; FILE *fp; if ((fp=fopen("./test.txt","w+"))==NULL) { fprintf(stderr,"cant open the fil!\n"); exit(1); ...

  1. #1
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259

    Question um canīt understand why this code isnīt working...

    Code:
    #include <stdio.h>
    #include <stslib.h>
    
    int main (void){
    
    
    char ch[20];
    FILE *fp;
    
    if ((fp=fopen("./test.txt","w+"))==NULL)
    {
               fprintf(stderr,"cant open the fil!\n");
               exit(1);
    }
    
    fprintf(fp,"text\n text\n");
    
    while (ch != EOF)
    {
    fgets(ch,sizeof(ch),fp);
    fprintf(stderr,"%s",ch);
    }
    :edited for code tags:
    Last edited by Shogun; 04-24-2003 at 09:38 AM.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>#include <stslib.h>
    hmm, what is this? maybe stdlib?

    >>if ((fp=fopen("./test.txt","w+"))==NULL)
    you sure you're open the correct path, maybe you mean ../test.txt?

    >>while (ch != EOF)
    you bett'a do something like this:
    Code:
    while (fgets(buf, sizeof(buf), fp) != NULL)
    >>fprintf(stderr,"%s",ch);
    sure you're writing in the correct stream? really mean to print in stderr or you want stdout?

  3. #3
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    >>#include <stslib.h>
    hmm, what is this? maybe stdlib?
    thats just a typo I dont write the code on the same comp as I post this on sicne it doesnīt have any interent in the real code itīs right.
    >>if ((fp=fopen("./test.txt","w+"))==NULL)
    you sure you're open the correct path, maybe you mean ../test.txt?
    nope I know I open the right path since I have done it be4
    >>while (ch != EOF)
    you bett'a do something like this:
    Code:
    while (fgets(buf, sizeof(buf), fp) != NULL)
    I`ll try that
    >>fprintf(stderr,"%s",ch);
    sure you're writing in the correct stream? really mean to print in stderr or you want stdout?
    I hope so my book tells me that thats the stream I should write to if I whant it to print to the screen...

    thx a ton for all the help
    Last edited by Shogun; 04-24-2003 at 10:28 AM.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>I hope so my book tells me that thats the stream I should write to if I whant it to print to the screen...

    I don't think so... your book I'm quite sure explained you that you should write to stderr when an error occur, like you did when opening the file, to print to the screen, use the stdout stream.

  5. #5
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    oh I see...cool, actouly my book doesnīt tell me what stderr does it just tells me to use that when I test my files...so I kinda figurd I should use it like print to screen. thx for clearafeing this for me
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    If you're trying to read back what you've just written, you'll have to "rewind" the file pointer first of all.

    After the fprintf(), the file pointer will be positioned at the end-of-file - just after the string you've written. Insert an ftell() just after the fprintf() to see the actual offset.

    To rewind, you'll need an fseek().

    e.g.

    x = fseek(fp, 0L, SEEK_SET);

    assuming you want to rewind to the BOF.

    hth

  7. #7
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    ah that makes sense and I have some vage memory of my book telling me this.... thx a ton for your help oh and what kind of variabel should x be? int?
    Last edited by Shogun; 04-24-2003 at 01:12 PM.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    int is ok, as far as I know, fseek returns int.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Yeah, fseek() returns 0 (zero) for success and -1 on error.

    You *really* should get into the habit of checking return values. Something like:

    Code:
    if (fseek(fp, 0L, SEEK_SET) != 0) {
        // oops
        perror("fseek");
        fprintf(stderr, "could not seek\n");
    }
    ... or some custom error handling.

    hth

  10. #10
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    I tryed it to day, I didnīt have to use any variabel at all
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  2. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  3. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  4. Working Code Samples Wanted
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 10:05 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM

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