Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-25-2003, 10:46 PM   #1 (permalink)
Danish
Registered User
 
Join Date: Mar 2003
Posts: 12
Danish is on a distinguished road
Send a message via AIM to Danish
Binary I/O file reading (0x1a trouble)

My problem: I need to know how to read a block of n bytes from a file and copy them to a buffer in memory. I am trying to use open() and read(), and it works just fine as long as there is no 0x1a in the file. But I have them in there. Am I using the wrong function for this? I mean, getc() works, but I don't really want to use a for loop just to get the contents of a file (efficiency goes down the crapper). I'm thinking there's a flag that I could use for open that would override this, but lseek() sees past the 0x1a's for the eof. I'm really confused right now, so any help is appreciated. Thanks.

I'll post the answer, of course, if I find it.

- Dane

Edit: Okay, the problem I was having is that I was assuming getc() was the only way to get binary data. fread() does that too. I wonder if there's a way to use those lower-level functions, though? Well, either way, I'm happy.
Danish is offline   Reply With Quote
Old 05-26-2003, 08:34 AM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
here is a program that uses read(2) to read binary data. it has no problems with 0x1a or any other character, and is much faster than multiple getc's.

Code:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
  int len, fd, i;
  unsigned char buf[1024];

  fd = open("in.txt", O_RDONLY);
  if(fd == -1)
  {
    perror("open");
    return 1;
  }

  len = read(fd, buf, sizeof(buf));
  if(len == -1)
  {
    perror("read");
    // should probably check errno here
    return 1;
  }
  if(len == 0)
  {
    printf("eof reached\n");
  }
  else
  {
    for(i = 0; i < len; i++)
    {
      printf("%02x ", buf[i]);
      if(i % 8 == 7) printf("\n");
    }
  }
  close(fd);

  return 0;
}
joe_bruin is offline   Reply With Quote
Old 05-26-2003, 11:02 AM   #3 (permalink)
Danish
Registered User
 
Join Date: Mar 2003
Posts: 12
Danish is on a distinguished road
Send a message via AIM to Danish
Oh, thank you very much. I'll try this right away and see what happens. I guess I was just having problems understanding how to use read()...

Edit the first: It's still stopping after 0x1a; it detects that as EOF... I'm using MinGW, does that make a difference?

Edit the second: I tried your code, and made a file in.txt; I put in some text, and it read it fine. I opened up a hex editor, changed the fourth character to 0x1a, and it won't see past three characters. I'm still thinking this is a MinGW issue...?

Edit the third: AHA! Eureka. Change

Code:
fd = open("in.txt", O_RDONLY);
to
Code:
fd = open("in.txt", O_RDONLY | O_BINARY);
Just as I thought -- a flag in open()... well, thanks for the help.

And yeah, read() is faster than fread() afaict.
Danish is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
text file recursive string reading C toblerone Standard C, C++ 4 08-04-2004 11:05 PM
Reading in a file jello Standard C, C++ 4 05-04-2004 08:53 AM
File I/O sno2dude Java 0 04-02-2003 10:35 AM


All times are GMT -8. The time now is 01:02 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting