Thread: Problem with File I/O

  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 07:19 AM. Reason: Remove overuse of bold

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    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, 09:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04: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, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM