How to write bytes to disc in C? [closed] - c

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.

Related

How to get file size without holes in C [closed]

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".

What are some common uses for the tcpdump -dd option? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
In reading the man pages for tcpdump, I saw that the -dd arguement would output the dump as a fragment of a C file. In what situations is that useful? I take it this is to quickly include and compile the fragment in a program that will be used to process the data according to code we write ourselves? Does this have its utility with unknown or new protocols? Is there some other common, standing situation in which this is needed? Just curious.
It's useful if you're writing a program using libpcap/WinPcap that would use a filter but that, for whatever reason, wouldn't run pcap_compile() to translate a filter string into BPF machine code; it lets you do the compilation with tcpdump and generate some text that you could use in the initialization of an array of struct bpf_insn (a pointer to which, and a count of elements in which, you'd put in a struct bpf_program).
I'm not sure who would do that, however.

Advantages/disadvantages of mapping a whole file vs. blocks when needed [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
What are the advantages/disadvantages of mapping a whole file once vs. mapping large blocks when needed in an algorithm?
Intuitively, I would say it makes most sense just to map the whole file and then let the OS take care of reading/writing to disk when needed instead of making a lot of system calls since the OS is not actually reading the mapped file before accessed. At least on a 64 bit system where the address space isn't an issue.
Some context:
This is for an external priority heap developed during a course on I/O algorithms. Our measurements shows that is slightly better to just map the whole underlying file instead of mapping blocks (nodes in a tree) when needed. However, our professor does not trust our measurements and says that he didn't expect that behaviour. Is there anything we are missing?
We are using mmap with PROT_READ | PROT_WRITE and MAP_SHARED.
Thanks,
Lasse
If you have the VM space, just map the whole file. As others have already said, this allows the OS the maximum flexibility to do read-aheads, or even if it doesn't it will bring in the data as required through page faults which are much more efficient than system calls as they're being made when the system is already in the right kernel context.
Not sure why your professor doesn't expect that behaviour, but would be good to understand his rationale?

Reading Linux via Windows [C program] [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My teacher asked my class to make a c program to read data on my linux partition via a c program in windows .
As such , after a good amount of research , i have been unsuccessful to find even the smallest hint as to how .
I realize the eof is different , but changing that allows me to read a file created in linux , not directly access the linux partition itself .
I was hoping that someone could please gimme a clue .
P.S I would really appreciate it if i was not given the complete code / info , i just wanna know where to start .
Can you read a "windows" file on "windows"? This is a good starting point along with manual pages for fopen etc.
EOF is the same everywhere - it is the end of file. The library "stdio" treats it the same wherever. That is why it is called standard IO
Just mount the linux partition onto your windows machine. I think the lecturer was doing this to make sure you was awake during the course.

copy web informations in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm new to C so please explain your answers as much as you can :)
What i would like to do is this:
i want to create in C a program that goes to a specific site and copies certain data to a txt file from where i can analyze them.
The question is how i copy these informations?
is this something that can be done in C?(if yes tell me the library that i have to use or the code)
for example i want to make my program do this:
go to this site - copy the lines(1-10)
thanx for your help
Simply use curl link to the webpage -o file or wget link to page in your program with system(),
But by default you will get HTML source of page. From that you can copy your required info.
if your link is a text file like this
Then it will fetches as it is. Then you can open that file with fopen().read first ten lines with fgets(). copy into another file.
you can also use libcurl library

Resources