fgets() is being weird when scanning lines that contain a % symbol [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This is part of a school project, so I can't show anybody my code, but suffice to say, my project has to read, copy, and edit its own source code (because we're studying polymorphic malware). I'm using fgets() to read each line of code, but whenever I have a line of code with a modulo operation or format string, the % symbol and whatever non-whitespace character that comes after it is missing. I'm guessing that fgets() is treating that % symbol like a format string, so how do I tell it not to do that?
Post answer edit:
I am posting one line of code to demonstrate what was wrong.
This is how I was getting strings from a file:
fgets(line, 128, src_file);
This is how I was writing that string to a new file (which was wrong)
fprintf(out_file, line);

If all you need is to write a string to a file you better use fputs():
fputs(line, out_file);

Your call to fprintf(out_file, line) is wrong.
When line contains the % character, it will be interpreted by fprintf() as a format specifier, which is not what you want.
You should change it to fprintf(out_file, "%s", line) instead.

Related

WinAPI ReadFile stops when reaching null [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 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.

Why the output of printf("%d","") is 173 in c language? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include<stdio.h>
#include<conio.h>
void main()
{
printf("%d","");//printing output
getch();
}
The output is 173 .I am not getting why the output is 173.
First, you're trying to print a string as decimal integer, which means the decimal you try to print is going to be the pointer to the string (actually a pointer to the array of characters) and not the string itself. To use an individual character use single-quotes, not double-quotes.
To accomplish what you're actually trying to do, do this:
printf("%d", ' ');
Note there is an actual space between the two single-quotes.
The result will be 32, which is the decimal value for the ASCII Space character.

int value changing when passing it into a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm passing in an int to a function in my lame program. It's passing in a number to convert it to a binary representation as an int array.
typedef int bool;
bool* conv2bin(int num)
{
blah blah blah return binary as bool array
}
I pass in 78 and if I printf() immediately after it's passed in, I get 781237412753-124?
I'm new to C (coming from C++) so please tell me if I'm doing something really dumb?
This seems like it should be really easy but it isn't...?
EDIT:
Have I done goofed:
printf("%d", num);
EDIT 2:
It has to be something with the int because at the end of the function, it checks to see if we subtracted numbers sufficiently to get to num==0 but it says we're not at 0. It's doing really weird things. It also says that the binary is 0000000001001111, and it should be 0000000001001110.
Edit 3:
Wow I suck. Thank you Floris! It's been a long day.
Guessing here…
Your printout starts with the correct two digits: 78.
But if you do not include a \n at the end of your formatting string, then the next thing you print will be concatenated. As will the next thing, and the next.
I suspect your problem will disappear when you change your print statement to
printf("%d\n", num);

Weird printf output in C (unknown characters & signs) [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 just started getting a weird printf output, has anyone ever seen this? Any idea what it could be caused by?
http://imgur.com/4Mt6xdi
Edit
Here's the code. I'm new to c so if anything (even if it's not causing the error) looks wrong or uncommon please tell me.
printf(f2,"%8.31f\t%8.31f\n",x[0],x[2]);
fprintf(f2,"%8.31f\t%8.31f\n",x[1],x[2]);
In the code you write:
if (x[0]*oldx<0)
{
printf(f2,"%8.31f\t%8.31f\n",x[0],x[2]);
fprintf(f2,"%8.31f\t%8.31f\n",x[1],x[2]);
}
where f2 is a pointer to FILE, which shall not be passed as the first parameter of printf. Just remove it.
At least one problem is on lines 96-97:
printf(f2,"%8.31f\t%8.31f\n",x[0],x[2]);
fprintf(f2,"%8.31f\t%8.31f\n",x[1],x[2]);
The first line should call fprintf, not printf.
Any compiler should give you at least a warning for calling printf with a FILE* as the first argument. Did you see such a warning? If so, why did you ignore it?
Compiling with additional warnings enabled should show you a number of other problems. Fix those before doing anything else.

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