top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

C: Why "while( !feof( file ) )” is always wrong

+3 votes
132 views

Lot of material is there on net, but everywhere it is bulky to read, can someone provide crisp and to the point answer.

posted Oct 7, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
one question what is "file" in  < feof(file) >
it is a file name or a file descriptor?

1 Answer

+2 votes
feof(fd) ; 
/*
    where fd is a valid file descriptor of some file having some data in it. 
    fd = fopen(file_name);
*/

As we know that <feof()> will return set value (mostly positive value) if end of file occurs.
As per your statement :

while (!feof(file)) {
}

There might be two things which is causing it to always fails:
1: Your file reaches the end of file so it return positive value, on which applying ! which converted it to 0 (zero).

2: Or you are passing a file name or an invalid file descriptor as "file" which causing it error, and return -1
and again applying not (!) on -1 it becomes 0 (zero) which again make while() fails.

answer Oct 13, 2014 by Arshad Khan
Thanks Arshad makes sense :)
Thanks for the appreciation.   :)
Similar Questions
+4 votes

Code to write data in to file example.txt.. which is working fine

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}

File : filelimit.c below which takes input as the compiled binary of above code
In this code i am setting the file size limit of process to 10 bytes and the above code is trying to write more than 10 bytes. According to below code if a process is trying to write more than 10 bytes to file it should send the signal SIGXFSZ (see more about this here)

It is not sending here... Why??

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

void handler(int sig)
{
    if(sig == SIGXFSZ)
        printf("Inside handler with correct signal\n");
    else
        printf("Inside handler with wrong signal\n");
}


int main(int argc, char const *argv[])
{
    pid_t pid;
    pid = fork();

    if(pid == 0)
    {
        signal(SIGXFSZ, handler);

        char command[256];
        scanf("%s", command);

        int ret;

        struct rlimit rl;
        rl.rlim_cur = 10;
        rl.rlim_max = 10;
        setrlimit (RLIMIT_FSIZE, &rl);

        char *envp[] = { NULL };
        char *argv[] = { command , NULL };
        ret = execve(command, argv, envp);

        while(1);
    }
    else
    {
        printf("%d - %d\n", getpid(), pid);
        signal(SIGXFSZ, handler);
        while(1);
    }
    return 0;
}
+2 votes

I am creating 500 binary files with one minute old time stamp. I am using windows API but it's very slow, so how can I improve the performance.

+4 votes

I have written print("%s", __LINE__); I get crashed. Can someone please explain why it is happened ?

+2 votes

Lets say I have a file 1.c.

#include<stdio.h>
main()
{
    while(1);
}

If I compile this using, cc 1.c i will get an executable i.e. a.out
If I run this it will be in infinite loop, while this process is running I am able to delete a.out

Where in same case if I start some movie/video and then if I try to delete it then i will not be allowed to.

Why ?

+1 vote

As far as I know, CORE file contains all the information till Segmentation fault. Then, while debugging core file why we are using executable file(binary) with it.?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...