Thread: help file concept

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    help file concept

    hi,

    i have doubt in c in file concept ,
    let put my example.

    i have created example
    # include<stdio.h>
    FILE *fileptr;
    fileptr = fopen("c:\emp.txt","r");
    c=fgetc(fileptr);
    while(c!=EOF)
    {
    printf("%c",c);
    }
    }

    it is not show anu error but it not reading content of emp.txt my using turbo c.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Your code is not going to compile because you do not have a main() function. If you're not getting any errors, you haven't shown us all your code because what you've posted up there is incomplete.

    I'm guessing this program is supposed to read the contents of the file "emp.txt" and display them on the screen. Let me guess- it just prints the first letter over and over again endlessly? The reason for this is that the fgetc function is outside your while loop, and so it reads the first character but never reads another one. Put it inside the while loop, before the printf statement, and see what happens.
    Last edited by TheBigH; 03-31-2011 at 10:58 PM. Reason: ...wait a minute...

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Quote Originally Posted by TheBigH View Post
    Your code is not going to compile because you do not have a main() function. If you're not getting any errors, you haven't shown us all your code because what you've posted up there is incomplete.

    I'm guessing this program is supposed to read the contents of the file "emp.txt" and display them on the screen. Let me guess- it just prints the first letter over and over again endlessly? The reason for this is that the fgetc function is outside your while loop, and so it reads the first character but never reads another one. Put it inside the while loop, before the printf statement, and see what happens.
    #include<stdio.h>
    void main()
    {
    char c;
    FILE *fileptr;
    fileptr=fopen("C:\emp.txt","r");
    c=fgetc(fileptr);
    while(c!=EOF)
    {
    printf("%c",c);
    c=fgetc(fileptr);
    }
    getch();
    }


    this correct code it is not running, please help me.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There's a lot wrong with your code...
    For example: You don't even check to make sure the file is opened before you start trying to read it.

    Plus your compiler is way way out of date. The code it produces won't even run on a 64 bit operating system (eg win7 or vista). You really should update to something from this millenium.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    File won't open because it has to be "C:\\emp.txt" (double backslash)

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    File won't open because it has to be "C:\\emp.txt" (double backslash)
    Nice catch! ... I looked right at that and didn't see it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2010, 12:05 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM