Problem with File I/O

This is a discussion on Problem with File I/O within the C Programming forums, part of the General Programming Boards category; This code is from a book and is supposed to open & display the contents of a file :: Code: ...

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Problem with File I/O

    This code is from a book and is supposed to open & display the contents of a file ::
    Code:
    #include<stdio.h>
    void main()
    {
    FILE *fp;
    char ch;
    clrscr();
    fp=fopen("H_World.c","r");
    while(1)
    {
    ch=fgetc(fp);
    if(ch==EOF)
    	break;
    printf("&#37;c",ch);
    }
    fclose(fp);
    getch();
    }
    This file called H_World.C is located in C:\TC
    My first question is that how does the compiler know that it has to search in C:\TC??

    Now I tried to experiment something.........
    **1.)I made a readme.txt file in C:\TC and in the fopen argument wrote "C:\\TC\\readme.txt"
    It worked !!
    **2.)Now i made a folder called NEW in C:\ and put this readme file in the NEW folder,made the argument "C:\\NEW\\readme.txt".This also worked!
    **3.)I put the readme file in F:\ drive....this also worked.
    **4.)Now i created a new folder in F drive ,put the file in new folder,made the necessary changes in the code but it returned a "Null Pointer Assignment"

    Please explain me how does this fopen() works,where does it search for the files(The book i read says it searches for the file on the entire Hard Drive but i dont think so)and why does it give error messege in experiment no.4?

    Thanks...........
    Last edited by Salem; 12-16-2007 at 06:19 AM. Reason: Remove overuse of bold

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    I'm guessing your fossil compiler doesn't understand spaces in filenames, or perhaps long filenames.

    Try using a compiler which is actually compatible with your OS, and not just compatible with an emulation layer provided by your OS.

    Oh, and the bugs.
    1. main returns int, not void
    2. ch should be declared as int, not char. This is so you can compare with EOF properly.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    And indent that mess.
    fopen searches the local directory (if no path is specified) or the path specified in the filename.
    Oh yes, to add to Salem's comments, void main is undefined; read my signature for more info.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    To make it more clear, and check file location. Follow Salem's and Elysia

    Code:
    #include<stdio.h>
    
    /*  Main should return a value
    void main() */
    int main
    {
       FILE *fp;
       char ch;
       /* non standard function, dont use it
       clrscr(); */
       
       fp=fopen("H_World.c","r");
       /* Check return value of fopen */
       
       if(fp == NULL)
       {
             printf("Error: File cannot be opened\n");
             return 1;
       }
       
       while(1)
       {
           ch = fgetc(fp);
           if(ch==EOF)
              break;
           printf("&#37;c",ch);
       }
       
       fclose(fp);
       
       /* getch(); */
       getchar();
       /* Should always return a value */
       return 0;
       
    }
    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 08:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 03:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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