WinAPI ReadFile stops when reaching null [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to write a very simple win32 program. I open a file for reading using CreateFile(), and then read it's content using ReadFile()
HANDLE hfile=CreatFileW(L"Capturejpg.jpg", GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
bReadResult=ReadFile(hFile, ReadBuff, BUFFERSIZE-1, &dwBytes, &OL);
when I'm reading a .txt file for example, this works just fine, the problem is when the file I am reading contains NULL (which is the case with most files) then ReadFile function stops reading(or maybe writing bytes to ReadBuff) when reaching the first NULL.
before asking I searched and found two answers.
use something other that char array and char *: for this, I don't know what else I can use, cause my goal is to read the file and search for the files extension(for example if it's .gif then the first 3 characters read "Gif")
change DCB: the problem with this one is that I have no idea what DCB is actually, how to change it, and change exactly what in it.
EDIT: other posts with the same problem: this one and this one

ReadFile does not care one bit about the content that it reads. It will quite happily read zero bytes and continue reading beyond that point in the file. It wouldn't be much use if it could not do that.
You have just misdiagnosed the problem. You have read into a character array ReadBuffer and then printed like this:
printf("%s", ReadBuffer);
Now, printf will indeed stop when it reaches a zero byte. You will need to find some other way to output the content of this file.

Related

How to get input from user via command line while bison/flex reads file? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
Improve this question
I'm new to flex and bison and i couldn't figure it out. I want to read a file. This is okey for now. But while bison/flex parse and read file, I want to make it wait for new input and get it from command line. Is there a keyword or way to do it?
I tried abort yyparse() with yyerror() and get input by scanf(). But it didn't work. It doesn't wait for input and goes on.
I did a project school named mini_shell and what we asked to do was to code something like bash.
the main thing was to wait for user input and execute the command he entered, you can do that by a function named.
char *readline(const char *prompt); (you can read the man page to understand more https://man7.org/linux/man-pages/man3/readline.3.html) but I think you need to install it first before you can use it, you can find a lot of tutorials explaining how to do that based on your OS.

How to read all the contents of a file using read() system call 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 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.

C - Read from file 1 line at a time without using fgets / getline [closed]

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 5 years ago.
Improve this question
Schoolwork — No code please. Pseudo code ok.
We are only allowed to use 3 libc functions: read, malloc, and free. However, during the course of school we have implemented and written many of our own versions of libc functions, and those ones we are allowed to use. I would list them, but there are about 50 of them, so instead I will be sure to mention which ones I cannot use.
My question...What are the steps I must take to read a line from a file and store that line as a string, without the \n? Is anyone able to walk me through the process? Because right now, I don't even know where to begin. I know how to use read, and I would consider myself somewhere between beginner and intermediate skill level with C, but after spending an entire day searching Google, every time this same question has been asked, the accepted answer always involves using fgets or getline, which we are not allowed to use, for obvious reasons. I have implemented my own versions of many libc functions that could potentially help in this project, but anything that would make this project quick and easy is obviously not allowed.
Again, please no code, though pseudo code is fine, but I would much prefer it if somebody could help me better understand what I need to 'tell the computer' to do, and from there I should be fine to write the code myself.
Pseudo-code to read 1 line
buffer to 0, size to 0, size_used to 0
loop
read 1 character
no success? - break loop
size_used >= size
make buffer bigger (maybe 2x, at least 1)
[This involves allocating a new buffer, copy existing data, freeing old buffer]
add character to buffer
was character a \n? - break loop
Nothing read?
return NULL
right-size buffer to size_used+1
append \0
return buffer (calling code needs to eventual free it.)
Lots of efficiency improvement are possible. I suggest starting with a basic version, get functionality correct and then consider improvements such as
Performance: read from the file, maybe 4k bytes at a time.
Performance: re-using the returned buffer.
Make robust and check for allocation failures.

Printing big-length string not showing all chars [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to concatenate lots of strings together to produce a unique string which will be long enough. Here is an example code:
char *s;
s= strconcat("A big-length string",NULL);
fprintf(stdout,"%s",s);`
When I try to print it, the string is less that is printed is smaller than the actual string.
Assume that the strconcat() function works well. Do I have to malloc() some space for s??
Also when I try strlen(s) it shows me the right number like 1078. just the printing is the matter.
EDIT
The problem solved when after all strings I try to inject \n like this
s= strconcat("A big-length string","\n",NULL);
Output using printf/fprintf is usually buffered.
If the output file is connected to a terminal, output is line buffered, so the buffer will be flushed wfter each \n.
If the output file is not connected to a terminal, output is fully buffered, so the buffer will be flushed when it is full. Typical buffer sizes is 1kb-8kb.
to ensure that the buffer is flushed call:
fflush(stdout);
You can flush all file buffers with:
fflush(NULL);

read file in a specific region line by line in file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
I have a text file where every row has the format
A B:C
I want to read it using C language, but all i want to get in every line is what comes after the ':'. In other words I want to get only the C value in every line of the file, but all I want to do is read the file line-by-line and get every row's values.
Any idea to do that?
open the file for reading
fp = fopen("file.txt" , "r");
Use fgets() to read the line
fgets (str, MAX_LENGTH, fp);
Use Strtok to break according to ":"
ptr = strtok(str,":");
Read file line by line by using fgets()
fgets(line,MAX_SIZE,file_stream);
And use strchr(), to get the position from where C starts
char *ptr=strchr(line,':'); //ptr points to the : location
//now if you Move ptr location next to ':' ptr points to C
printf("C=%s",ptr+1);
And with out using additional pointer, You can do like this
printf("C=%s",strchr(line,':')+1);
Repeat these steps until reaching end of file. By checking the return value of fgets() against NULL.
Note: In this A B:C , if A or B consists : then You need to Apply logic accordingly.
You can also use strrchr()

Resources