Find the answer to your Linux question:
Results 1 to 3 of 3
I am using embedded Linux. File open is successful. The file has all the permissions. It is present in current directory too. The size is about 27KB. But s.st_size says ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    4

    In correct File is empty

    I am using embedded Linux. File open is successful. The file has all the permissions. It is present in current directory too. The size is about 27KB. But s.st_size says it is zero. Any thoughts.

    inputFileName = "abc.mp3";

    FILE* inFile = NULL;
    inFile = fopen(inputFileName, "r+b");
    fprintf(stderr, " inside current dir r+b my_mp4creator new:%s \n",inputFileName);
    if (inFile == NULL) {
    fprintf(stderr,
    "%s: can't open file %s: %s\n",
    ProgName, inputFileName, strerror(errno));
    return NULL;
    }

    struct stat s;
    if (fstat(fileno(inFile), &s) < 0) {
    fprintf(stderr,
    "%s: can't stat file %s: %s\n",
    ProgName, inputFileName, strerror(errno));
    return NULL;
    }

    if (s.st_size == 0) {
    fprintf(stderr,
    "%s: file %s is empty\n",
    ProgName, inputFileName);
    return NULL;
    }
    Thank you in advance.

  2. #2
    Just Joined!
    Join Date
    Jun 2010
    Posts
    10
    Hi,

    I haven't verified this yet but AFAIK defining
    Code:
    FILE * infile =  NULL;
    is your offence.
    Defining a NULL pointer is valid, but it doesn't point to anything. This means that opening the file won't fail, but will point to nothing so any operations on that file will essentially do nothing.

    try this:
    Code:
    /*stuff
    FILE * inFile;
    if (inFile !=NULL){
    /*more stuff*/
    that will then test to see if the pointer is correctly set up before it operates on the file.


    /*Hope that helps*/

  3. #3
    Just Joined!
    Join Date
    Jun 2010
    Posts
    10
    Hi,

    I haven't test this, but I think defining
    Code:
    FILE * infile =  NULL;
    is your offence.
    defining a NULL pointer is valid, but it doesn't point to anything. This means that opening the file won't fail, but will point to nothing so any operations on that file will essentially do nothing.

    ...come to think of it, there is a large number of return(NULL) going on.
    If you are returning to main() I would suggest returning an int. It means you know in what state the program quit. Also, gcc will call BS on defining main() as anything other than int

    the program below produces the following warnings on my machine (GCC 4)

    Code:
    #include <stdio.h>
     
    void main(void)
    {
    	;
     return NULL;
    }
    gcc test.c
    test.c: In function 'main':
    test.c:7: warning: 'return' with a value, in function returning void
    test.c:5: warning: return type of 'main' is not 'int'
    Hope that helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...