Thread: Unable to open a file for reading

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Question Unable to open a file for reading

    Hi
    I am a beginer to C Programming.I was trying to open a .txt file for reading but couldnt do that this is my code..Help me out.

    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    FILE *fp;
    clrscr();
    fp=fopen("raghu.txt","r");
    if(fp==NULL)
    printf("ERROR IN OPENING THE FILE");
    else
    {
    printf("hi");
    fprintf(fp,"\n HI");
    }
    fgetc(fp);
    fclose(fp);
    getch();
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    1. You didn't ask a question.
    2. You're checking if fp is NULL, but if it is, you use it anyway.
    3. You are trying to write and read on a file you're opening for reading.
    4. You're using non-standard code.


    So your question is?

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    I did not get the 4 point.."You're using non-standard code." can you tell me more about that ,Mac

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should write
    int main()
    explicitely

    and not ignore the return value of fgetc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by raghuveerd View Post
    I did not get the 4 point.."You're using non-standard code." can you tell me more about that ,Mac
    conio.h is not a standard header
    all function declared in it - are not standard

    using them - makes code not portable - You cannot compile your code on differrent compiler / OS.


    You should avoid posting such code - it makes hard to compile it by the people you ask for help... Not many people will take your code, remove non-portable parts just to compile and see what is the problem...

    You can avoid such code in your application at all (better) or just remove it before posting a sniplet of the code to the board
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    i made some changes ..but still cant read anything
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    FILE *fp;
    clrscr();
    fp=fopen("raghu.txt","r");
    if(fp==NULL)
    printf("ERROR IN OPENING THE FILE");
    else
    {
    printf("hi");
    fprintf(fp,"\n HI");
    }
    
    fclose(fp);
    getch();
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you still opening for reading - and trying to write
    don't close file if you failed to open it - close only if openign was successful
    indent your code properly
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    I am sorry but i have a questions..Is this forum not intended for beginers in C Programming. I want to know if i am really posting silly questions wasting time of experienced programmers.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by raghuveerd View Post
    I am sorry but i have a questions..Is this forum not intended for beginers in C Programming. I want to know if i am really posting silly questions wasting time of experienced programmers.
    Of course it's a place for new C programmers to ask questions.

    The problem, however, is that you didn't ask a question. You dumped code here with a blanket "doesn't work" message. Of course it doesn't work. If it worked, you wouldn't be asking for help. It's irritating to try to help solve a problem with no information on said problem.

    As far as your code goes, you're still opening a file for reading, but you're trying to write to it. You've been alerted about this like twice before. This will not work, and could result in very weird behavior.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > fp=fopen("raghu.txt","r");
    This is for reading from a file.

    > fprintf(fp,"\n HI");
    This is for writing to a file.

    Basically, decide what you want to do, then edit the code accordingly.

    For example, fgets() and fscanf() are file reading functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    The thing i want to do is to print the text in raghu.txt on the screen. Can i do that!

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, you can. Use fgets().

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    read it in with the functions Salem wrote, then printf it out.

  14. #14
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Thanks a lot..i got it now..actually i was confused with the what fprintf and fscanf does...
    thanka a lot Mac,Salem,Rob,vart

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Now i can print the text in my file on to the screen..is the right way to do it

    Code:
    #include<stdio.h>
    
    int main()
    {
    FILE *fp;
    char *str;
    clrscr();
    fp=fopen("raghu.txt","r");
    if(fp==NULL)
        printf("ERROR IN OPENING THE FILE");
    else
        {
         printf("hi,this is the text in your file\n");
         fgets(str,20,fp);
         }
    printf("&#37;s",str);
    fclose(fp);
    
    getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM