Like Tree1Likes
  • 1 Post By whiteflags

Fgets,finding end of file without using eof feof

This is a discussion on Fgets,finding end of file without using eof feof within the C Programming forums, part of the General Programming Boards category; hi i want to write my own fgets code. Code: #include <stdio.h> #include <ctype.h> #include <stdlib.h> //***********MY FGETS FUNCTION************** char ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    77

    Fgets,finding end of file without using eof feof

    hi
    i want to write my own fgets code.


    Code:
    #include <stdio.h> 
    #include <ctype.h> 
    #include <stdlib.h> 
    
    
    
    
    //***********MY FGETS FUNCTION**************
    char *Tomgets(FILE *filex/*,int length_of_array*/)
    {
    char *p;
    char c;
    int i=0;
    int len=-1;  //abc\0  0 1 2 3
    
    
    
    
        for(i=0;i<100;i++)//in here my problem:i dont know how to stop my loop,i dont want to  //use eof feof etc. 
    //What is the end of file character? '\0' no i tried but didnt worked.if i find the end of //file,i can 
    
    //define my size of pointer,so it is important.
    //For example if the end of file char='\000'  i can write this c!='\000' instead of 100 it //would work.
        { 
            c=(fgetc(filex));
            printf("%c",c);
            len++;
        }
    
    
    
    
    p=calloc(len,sizeof(char));
    for(i=0;i<len;i++) p[i]=c;
    p[i]='\0';
    return p;
     //printf("%s",p);
    }
     //****************************************
    int main(void) {
       int i=0;
       FILE *fp;
       char *content;
    
    
    
    
    
    
    
    
       if((fp=fopen("t.txt", "r")) == NULL) {
          perror("Error: unable to open the input file");
          return 0;
       }
       //content=calloc(1000,sizeof(char));
       //fgets(content,1000,fp);    
     
       content=Tomgets(fp);//if i do this, i dont have to do the memory allocation of content.
    
    
    
    
       printf("%s",content);
       printf("\n\n");
       fclose(fp);
       return 0;
    }
    any help make me happy.Thanks.
    Last edited by rac1; 12-26-2011 at 11:31 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    Why don't you want to use EOF? That is the standard way of detecting end of file conditions. In the pre-DOS world control Z was used by some operating systems to indicate the end of file, but most modern operating systems (Including MSDOS) do not use any character as an end of file marker. You are using fgetc() which returns EOF when the end of file is reached, so use that.

    Also the loop following your calloc() is not doing what you think it is.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    77
    [FONT=Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace]thanks Jim;
    you want to tell me this isn't it?
    [/FONT]content=(char *)calloc(1000,sizeof(char));
    but i dont know
    content=calloc(1000,sizeof(char)); is also work with the above code.
    okey i should use this


    for(i=0;c!=EOF;i++)
    {
    c=(fgetc(filex));
    printf("%c",c);
    len++;
    }

    if i do this everything will be okey?



    Last edited by rac1; 12-26-2011 at 12:11 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    I didn't say anything was wrong with your calloc call I said the loop following that call is probably incorrect.

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    77
    That is the code and look the output.I think i found what is the end of file

    Code:
    #include <stdio.h> 
    #include <ctype.h> 
    #include <stdlib.h> 
    
    
    
    
    //***********MY FGETS FUNCTION**************
    char *Tomgets(FILE *filex,char *filename)
    {
    char *p;
    char c;
    int i=0;
    int len=-1;  //abc\0  0 1 2 3
    
    
    
    
        for(i=0;c!=EOF;i++)
        { 
            c=(fgetc(filex));
            //printf("%c",c);
            len++;
        }
        fclose(filex);
    //-------------closed and reopened file to start fgetc from beginning to the end-------------
        filex=fopen(filename,"r");
        p=(char *)malloc(len*sizeof(char));
            for(i=0;i<len;i++)
            {
                c=(fgetc(filex));
                p[i]=c;
            }
            //p[i]='';
            printf("length_file=%d\n\n",len);
             printf("%c\n\n",p[i]);
    return p;
    }
     //****************************************
    int main(void) {
       int i=0;
       FILE *fp;
       char *content;
       char namex[]="t.txt";
    
    
    
    
    
    
       if((fp=fopen(namex, "r")) == NULL) 
       {
          perror("Error: unable to open the input file");
          return 0;
       }
       content=Tomgets(fp,namex);//if i do this, i dont have to do the memory allocation of content.
       printf("*%s*",content);
       printf("\n\n");
       fclose(fp);
       return 0;
    }


    The end of file is 4 times of Square symbol?
    Attached Images Attached Images  

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Code:
    for(i=0;c!=EOF;i++)
    {
        c=(fgetc(filex));
        printf("%c",c);
        len++;
    }
    When this loop finishes c will contain the value EOF and you don't get to use any of the previous values of c in the following loop in your code.

    Code:
    for(i=0;i<len;i++) p[i]=c;
    You should fill p as you get characters from filex.

    c is also the wrong data type. fgetc returns an integer. This is by design: the value of EOF is guaranteed to be outside the range of char.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    77
    i ordered my code , and made new one , could you look my previous post please?

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    I looked at your previous post, my advice is the same.

    The end of file is 4 times of Square symbol?
    The end of the file is the end of the file. If you did a correct job, you should not see anything on the screen that is not in the file.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    77
    Could you try this code?
    if the 2222 was the garbages of the memory,yes you are right and it is not the symbol of the end of file,
    while you trying on your machine, if it gives same result, i can say it is the symbol of the EOF.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    The following snippet:
    Code:
        for(i=0;c!=EOF;i++)
        {
            c=(fgetc(filex));
            //printf("%c",c);
            len++;
        }
    Why are you using a for loop? This loop really should be a while() loop. You never use the variable i so why even use it. Something more like:
    Code:
            while(fgetc(filex) != EOF);
                len++;
    Also you should read the FAQ as to why you should not use EOF to control a loop. Also this FAQ on EOF may also be useful.

    Also you should not be casting the return value of your malloc() call, also a FAQ entry


    Jim

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by rac1 View Post
    Could you try this code?
    if the 2222 was the garbages of the memory,yes you are right and it is not the symbol of the end of file,
    while you trying on your machine, if it gives same result, i can say it is the symbol of the EOF.
    No you can't. For one thing, I know better, and EOF is not actually stored in a file. It is only relevant to files from the digital dark age known as MS-DOS which I assume you don't want to support. It's older than I am.

    For another thing, if I fix your program I get this as the output.
    Code:
    C:\Documents and Settings\User\My Documents>a
    length of file = 55
    *alican 99 sirvan aq
    osman
    xzaaaaaa
    r
    r
    r
    t
    v
    t
    r
     a e t*
    
    C:\Documents and Settings\User\My Documents>
    There is no magic EOF value for anyone to see.
    rac1 likes this.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    77
    Thanks for helping Jim and Whiteflags.
    you are right.I understood that i must use feof or eof to save the file content onto an array.But in here as you see, if i used fgets, i shouldn't allocate my memory meanly.I should do for example
    char content[100];
    .
    .
    fgets(content,100,fp); // where is FILE *fp.
    i could get error, because the text file can have more than 100 chars, but it maynot be, so if there is 1 char, other 99 is unnecessary.Please dont say me ,"he is mad" i wrote my own fgets because of this.
    THank you very much again.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Well I'm not sure what prompted that response.

    fgets(content,100,fp); // where is FILE *fp.
    i could get error, because the text file can have more than 100 chars, but it maynot be, so if there is 1 char, other 99 is unnecessary.Please dont say me ,"he is mad" i wrote my own fgets because of this.
    I hope you accomplished your actual goal, whatever it is. While I can look at (a fixed version of) your function and say it is memory efficient, fgets isn't particularly memory inefficient either. In fact fgets works differently. The same call to fgets will store a line of at most 100 characters in content. Now, if we look at t.txt, most of the file content is on separate lines. While most of your lines are short, fgets has to also handle the longest lines in your file. If 100 characters could be the longest line in your file, then that isn't mean.

    That's not to say that writing something like what you wrote isn't valuable in its own right: there is a function in python called glob that returns the file content as an array of strings, one string for each line, but it seems like the idea for your whole function came from a misconception of how fgets works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with reading a file in C, with fgets()/sscanf()
    By ErickN in forum C Programming
    Replies: 3
    Last Post: 04-23-2011, 11:54 AM
  2. Reading a file using fgets
    By Ferris in forum C Programming
    Replies: 10
    Last Post: 12-06-2010, 03:31 PM
  3. File input with fgets
    By hellogamesmaste in forum C Programming
    Replies: 4
    Last Post: 08-30-2009, 03:09 AM
  4. read mix file: fscanf fgets
    By cfdprogrammer in forum C Programming
    Replies: 1
    Last Post: 03-20-2009, 12:38 PM
  5. Using fgets on a file
    By MasterAchilles in forum C Programming
    Replies: 14
    Last Post: 04-18-2008, 04:57 AM

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