Is using fscanf when opening a file in binary mode bad? I can't seem to find anything reasonable on the Internet. I am trying to open and read a PPM file and I've found this, but I am not sure if using fscanf is okay? And using netpbm is not okay, yeah.
Reading this with fread seems like a pain.
The scanf and fscanf functions are for reading characters, e.g., "1234", and converting them from a string to an integer. But integers are not stored as stings in a binary file. The actual bytes of the integer itself are stored. These need to be read directly into an integer with fread.
Related
The aim of the problem is to use only pread to read a file with the intergers.
I am trying to device a generic solution where I can read intergers of any length, but I think there must be a better solution from my current algorithm.
For the sake of explanation and to guide the algorithm, here is a sample input file. I have explicitly added \r\n to show that they exist in the file.
Input file:
23456\r\n
134\r\n
1\r\n
345678\r\n
Algorithm
1. Read a byte from the file
2. Check if it is number i.e '0' <= byte <= '9'
3.1 if yes, increment the offset and read the next byte
3.2 if not, is it \r
3.2.1 if yes, read the next and it should be \n.
Here the line is finished and we can use strtol to convert string to int.
3.2.2 // Error condition
I'm required to make this algorithm because if found out that pread reads the files as string and just pust the requested number of bytes in the provided buffer.
Question:
Is there an better way of reading intergers from the file using pread() instead of parsing each byte to determine the end-of-string and then converting to interget?
Is there an better way of reading intergers from the file using pread() instead of parsing each byte to determine the end-of-string and then converting to interget?
Yes, read big chunks of data into memory and then do the parsing on the memory. Use a big buffer (i.e. depending on system memory). On a mordern system where giga-bytes of memory is available, you can go for a buffer in the mega byte range. I would probably start out with a 1 or 2mega byte buffer and see how it performs.
This will be much more efficient that byte-by-byte reads.
note: your code needs to handle situations where a chunk from the file stops in the middle of an integer. That adds a little complexity to code but it's not that difficult to handle.
where I can read intergers of any length
Well, if you actually mean integers greater than the largest integer of your system, it's much more complicated. Standard functions like strtol can't be used. Further, you'll need to define your own way of storing these values. Alternatively, you can fetch a public library that can handle such values.
I am working on a database flat file project using c language. I have created a structure with few members. I am using fwrite() to write them in binary file and fread() to fetch the data. My two major question
1st can we write structure in text file? I have seen no good example. Is it practically wrong to write it in text format? when I write using "w" instead of "wb" I get the text format but with some extra words.
2nd how these fread() & fwrite works(). They operate on a block of data how they get the address of next block. I mean we do have the pointer but file doesnt have any address so how the pointer go to next block?
1st can we write structure in text file ? i have seen no good example
.is it practically wrong to write it in text format ?when i write
using "w" instead of "wb" i get the text format but with some extra
words
Imagine your structure contains some integers inside. Now if you write them using fwrite, these integers will be written in file in binary format.
If you try to interpret this as text, this won't work, text editor will try to interpret the binary values as characters - which will most likely not work as you expect.
e.g. if your structure contains integer 3, when written using fwrite, it will be stored as
00000000 0000000 0000000 00000011 (3 in binary)
assuming big endian notation. Now if you will try to read above using a text editor, of course you will not get desired effect.
Not saying anything about the padding bytes which maybe inserted in your structure by compiler.
2nd how these fread() & fwrite works(). They operate on a block of
data how they get the address of next block. I mean we do have the
pointer but file doesnt have any address so how the pointer go to next
block?
This is most likely taken care of using OS.
PS. I suggest you read more about serialization, and try to understand difference between text and binary files.
If I have a character array that is in EBCDIC format and I want to save that array to a file. I'm thinking of using fputs to output the character array without first converting it to another format.
Question) Is the use of fputs legal for writing EBCDIC? If not, should I convert the string to ASCII before outputting?
I've search online, but couldn't find anything to say fputs should not be used for outputting EBCDIC data.
If your character array that is in EBCDIC format is a c-style string in that in ends with a \0 byte, then there is no problem.
fputs(), in binary mode, is format agnostic other than it does not write a \0.
Assuming your program is written using the ASCII char set, it is important that your output file is opened in binary mode (e. g. "wb"), else the \n of C will not match the same in EBCDIC and some translations are possible.
On the other hand, are you going to do something with this file other than write and maybe read back?
Should your "character array that is in EBCDIC format" not end in \0 or have embedded \0 bytes, suggest you simple use fwrite(). Again be sure to use in binary mode, unless your entire system is EBCDIC.
Well, fputs takes a C string, and that uses the ASCII encoding . So, that won't work. I think you'll need to write the file using a lower level function. Perhaps use fwrite to write the file directly without using strings. Here's the man page on fwrite.
I'm stuck with my program here where it reads the data from a text file but it did not obtain the float part as float.
Instead of 43.23, it prints 43.00 after it reads from my .txt file.
Where did I do wrong?
dir[k].age=atol(strtok(NULL,","));
dir[k].weight=atol(strtok(NULL,","));
dir[k].height=atol(strtok(NULL,"\n"));
dir[k].weight=atol(strtok(NULL,","));
dir[k].height=atol(strtok(NULL,"\n"));
atol reads a long. You probably wanted atof.
dir[k].weight=atol(strtok(NULL,","));
dir[k].height=atol(strtok(NULL,"\n"));
You are using a function that converts to integers, so that doesn't know how to handle fractional parts. Use strtof instead, that even allows for error checking, in contrast to the ato* functions.
Here:
dir[k].weight=atol(strtok(NULL,","));
dir[k].height=atol(strtok(NULL,"\n"));
You are reading values as long
I would like to parse binary files with PCRE. My tactic until now was to use fgets to read a line of a file, then parse that line using pcre_exec.
This will not work for me now because the "lines" end with a null byte rather than a newline. I did not see a way to have fgets stop at a null byte rather than newline.
Edit
The functionality would be similar to running grep -az PATTERN FILE
In this case, no luck, you need to read your binary file byte by byte and check for '\0'.
You can then store this bytes on a buffer and:
Doing some comparaison on the fly with your PATTERN
or
If you want to keep data for later processing, you can store this buffers on a linked list for example (if you don't have a huge files).
Hope this help.
Regards.