Results 1 to 6 of 6
Hello everybody
I'm porting a c/c++ applications from windows to linux, and I'm having problems with the open() function. I opened the files as binary in windows, but in linux ...
- 05-21-2007 #1Just Joined!
- Join Date
- May 2007
- Posts
- 3
open files as binary...
Hello everybody
I'm porting a c/c++ applications from windows to linux, and I'm having problems with the open() function. I opened the files as binary in windows, but in linux I cannot do that. The O_BINARY flag does not exist... :o
When I started checking out why, I found a nice bit of information on the fopen() function that rocked my world...
(...)
The mode string can also include the letter ``b'' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with C89 and has no effect; the ``b'' is ignored on all POSIX conforming systems, including Linux.
(...)
I have a quite large application and I'm having some problems because the files are opened as text and the read fails sometimes (other times it dosn't . don't know why
)
Any ideas how I can give this problem a twist?
well, thanks for all the help I hope is coming my way
André
- 05-22-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
What problems? How do you read the files? Post some snippet of your code.
Here an example how you can read a textfile:
Note that in DOS files a line end with CRLF instead of LF.Code:int main() { FILE *inp; char readbuf[256]; inp = fopen("textfile", "r"); while(fgets(readbuf, 255, inp) != NULL) { printf("%s\n", readbuf); } fclose(inp); return(0); }
The following command convert a DOS file to Linux format:
Press Ctrl-V then Ctrl-M to get the ^M.Code:sed 's/^M$//' input.txt > output.txt
Regards
- 05-22-2007 #3Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
I misread your question, this is an example of reading and writing a binary file:
Code:#include <stdio.h> #include <fcntl.h> #define PERMS 0666 int main (int argc, char *argv[]) { int inp, outp; char readbuf[BUFSIZ]; int n; inp = open("bin_file1", O_RDONLY, 0); outp = creat("bin_file2", PERMS); while((n = read(inp, readbuf, BUFSIZ)) > 0) { if(write(outp, readbuf,n) != n) perror("Write"); } return(0); }
Regards
- 05-23-2007 #4Just Joined!
- Join Date
- May 2007
- Posts
- 3
Franklin52
thanks for your replies but I'm doing the same thing as in you example code.
I defined the flags as
iand used openCode:nt flags[4][3] = { {O_RDWR|O_CREAT,S_IREAD|S_IWRITE,SH_DENYRW|S_IRWXU}, {O_RDONLY,S_IREAD,SH_DENYNO|S_IRWXU}, {O_WRONLY|O_TRUNC|O_CREAT,S_IREAD|S_IWRITE,SH_DENYWR|S_IRWXU}, {O_WRONLY|O_APPEND|O_CREAT,S_IREAD|S_IWRITE,SH_DENYWR|S_IRWXU} };
As much as I've read so far, in Linux all files are handled as text and there is no support for binary files... in this page http://djgpp.linux-mirror.org/v2faq/faq9_4.html It is stated that:Code:handle=open(nome,flags[my_mode][0],flags[my_mode][2],flags[my_mode][1]);
and adding to all my problemsCode:Note that the above distinction between binary and text files is written into the ANSI/ISO C standard, so programs that rely on the Unix behavior whereby there's no such distinction, are strictly speaking not portable.

So my problem persists(?) how do handle this...Code:Well the difference is that text files contain lines (or records) of text and each of these has an end-of-line marker automatically appended to the end of it whenever you indicate that you have reached the end of a line. There is an end of line at the end of the text written with the C fwrite() function or in C++ when you <<endl. Binary files are not broken up into separate lines or records so the end-of line marker is not written when writing to a binary file. Reading from a text file or binary file is different too as a text file is automatically broken up into separate records as it read in based on the location of the end-of-line markers.
regards
- 05-23-2007 #5Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Your open statement doesn't use proper arguments, try:
To read random from the file you can use the functions fseek, ftell, rewind, fgetpos, fsetpos.Code:handle=open(nome, O_RDONLY, 0);
Check the man pages.
Regards
- 05-28-2007 #6Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
welcome to the forum
> the files are opened *as*text*
the files are opened--text, binary, whatever--it's all the same in *nix
quoting Kernighan & Ritchie: "...our fopen does not recognize the "b"
that signals binary access, since that is meaningless on UNIX
systems..." K&R,2nd ed'n,p.178
> the read fails sometimes...
well, I think you'll have to look elsewhere for your problem.the sun is new every day (heraclitus)


Reply With Quote