Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So I was trying to get the size of a file in C.
I am aware of 2 ways of getting a file size in C, to use the st_size member of the stat structure or use fseek between the beginning and end of the file.
However, I was concerned with whether the st_size method would report the "false size" of a file if it has holes.
I'd imagine the fseek method would definitely produce the incorrect result if the file has holes, correct?
According to the book "Advanced Programming in the UNIX Environment", section 4.12, one way of getting around this is to copy the file into memory and reading its size from memory. But I couldn't figure out how to do this in C so I was wondering how exactly you could do that.
Any help is appreciated.
Assuming you mean "holes" between physical blocks on disk where the file's data is stored - those don't count as part of the file, and both methods will only regard proper file data. In the file abstraction, there are no "holes".
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I want to read a file using read() system call and copy all its contents to another file. As the input file can be large, I don't know what buffer size to use. How to change the buffer size dynamically? Or is there any other approach like reading a file part by part using a fixed buffer ? Can anyone tell how to do this.
Read the file part-by-part using a fixed buffer. To copy a file, there is no reason why you have to read the entire file in one call.
If I understood your question correctly, each time you want to read with a different BUFFER_SIZE, read all the file and copy it to an other one.
I think you can easily read the file using BUFFER_SIZE each time, and join the string to what you've read before until Read returns zero which means end of file(check [read(2) — Linux manual page][1]), than you can write the whole thing to the other file, I hope that this answered your question.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I was trying to construct a function which would say given a line number replace that line with another string. Currently, I'm achieving this by reading the whole file into my RAM, modifying the line in RAM and rewriting the whole file back. I was wondering how databases managed to achieve this since this is a rather frequent operation over there.
line implies a text file, and since those generally have varying line lengths, there is no way to replace a line in the file on disk unless the new one has the same length (the operating system does not provide a way to open or close gaps in a file).
Databases, on the other hand, generally operate on fixed-size entities (per table), so there, the database can replace the content of a 'record' with new data and also keep a list of previously used records that are now deleted and can be recycled when new data is inserted.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The question I am asking is extremely simple. Lets just say I wanted to read a large file(6GB) without having the heap run out of memory. How would I do that. (What I am mainly asking is if there is a method to read part of the file clear the buffer and read the next part of the file)
The memory capacity and availability is platform and operating system dependent.
Some operating systems allow for memory mapping a file, in which the operating system manages the reading of data into memory for you.
Reading without overflow is accomplished by using block reading (a.k.a. fread in C and istream::read in C++). You tell the input function how much to read in the block and the function returns the quantity actually read. The block size should be less than or equal to the memory allocated for the data. The next read will start a the next location in the file. Perform in a loop to read in all the data.
Also, verify there is a reason to hold all the data in memory at the same time. Most programs only hold a small portion of the data for a limited time.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to write a C program in Linux system which the main function is read a data file (csv format 200MB file) in struct array and searching condition file (few lines) then output the matching result.
The read data function takes around 1 second to run and the matching part is pretty quick. I am thinking is that possible I can pre read the data file in memory by some methods then run the searching function for many time as I want.
It maybe similar to R. Read a csv file first then do some calculate from it.
Create a tokenizer using read system call to read until you hit the comma and then update up to that part to your struct using memcpy or strncpy. After that it would be easy for searching and validation.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I want to write a program that formats a disc to a new format (not NTFS or FAT32) and can read from and write to the disc, In order to do this, I have to have a way to write single bytes to a disc (without creating files). How do I do that?
EDIT: I found this. But I'm not sure if using CreateFile() as Eli Bendersky said in the first answer (when organized in order of votes) let's you write one byte (or even one bit) at a time, or do you have to write full sectors at a time.
Answer: Since I can't post an answer the question because it was closed, I will answer it write here. You don't need need API functions to do this. All you need to do is open the disk like you open any other file. Like this:
int hFile;
hFile=open("/dev/sdb",0_RDWR);
You program has to directly talk to the driver as you will be by-passing the file-system.