Problem reading file

This is a discussion on Problem reading file within the C Programming forums, part of the General Programming Boards category; Hi, I am stuck with a weird problem that i am not able to understand. I am trying to make ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    8

    Problem reading file

    Hi,

    I am stuck with a weird problem that i am not able to understand.

    I am trying to make a utility in C language that extracts a part of an MP3 file and writes it to an output file.

    What i am currently doing is that i open the input file using _open() in binary mode and read the file byte by byte using _read(). However when it reads byte 1Ah (26 in decimal), _read() returns 0 indicating End Of File. This byte is the 103rd byte in the file, which is almost the start of file, so it is actually not the end of file. The file itself is of size 2.89 MB.
    Secondly, EOF is defined as -1 not 26. So why is _read() indicating EOF.

    By the way i have also tried fread() but it also has the same problem.

    I am using MS Visual Studio 2000 under Windows XP.


    Please suggest some solution as this problem is driving me crazy.

    Thanx

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    You have to use fopen() with binary mode "rb".

    I have no idea what _open and _read are on Windows, but they are probably fake and they most likely don't support opening in binary mode which is what you need.

    Secondly, EOF is defined as -1 not 26. So why is _read() indicating EOF.
    Because on Windows, if a file is opened in text mode, the presence of byte 0x1A is considered to be EOF.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ctrl-Z (0x1A) is used as an end-of-file marker in MS land, as I recall. C does give you (char)-1 when it reads EOF, but that's not because -1 is stored in the file; that's just how C does things so that everything works (kinda) the same across different platforms.

    fread should have worked, I would think -- care to provide details?

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Here's the code:
    Code:
    FILE *fp;
    	char data_byte;
    	int count=0;
    		
    	fp = fopen("song.mp3","r");
    
    	if(fp==NULL)
    	{
    		perror("Error Opening Source File: ");
    		return;
    	}
    
    	fseek(fp,(long)0,SEEK_SET);
    	
    	while (1)
    	{
    		fread(data_byte,1,1,fp);
    		if(feof(fp))
    			break;
    		count++;
    	}
    	
    	printf("byte count = %d\n",count);

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by coder_009 View Post
    What i am currently doing is that i open the input file using _open() in binary mode
    See, there's no reason to tell us this when it's not true. If you want to open a file in binary mode, you need to use "rb" as the mode string, not "r". If you don't, the file will open as a text file, which means weird things will happen with bytes like 0x0a (=\r) and 0x0d (=\n) and 0x1a (=EOF).

    EDIT TO ADD: I should mention, I'm not saying you deliberately lied to us or anything; but if you had shown source code the first time we would have known where the error is, instead of having to choose between several different things.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Quote Originally Posted by tabstop View Post
    I'm not saying you deliberately lied to us or anything;
    i did not lie. I also have the code that uses _open(), but since you guys stressed more on fopen, feof() so i posted this code.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    anyways here it is, please tell me if i am missing something here

    Code:
     	fd = _open("song.mp3",_O_RDONLY , _O_BINARY);

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Thanx guys ! problem is solved using fread(). Using "rb" in fopen works. But i'd also like to know about the _open();

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Try
    Code:
    fd = _open("song.mp3", _O_RDONLY | _O_BINARY);
    According to MS docs, _O_BINARY belongs in the second argument, not the third (which is not applicable for reading files). And of course, bit-flags must be OR'ed together for them both to be active.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    yeah, i got it. Thanx a lot man !!!

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    I don't know where _open is defined, but it's definitely not some fake. In fact, _open is called by fopen in MS's implementation.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

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