How can I read a magic number from file? [closed] - c

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 5 years ago.
Improve this question
I have a list of files. For each of those files, I need to find out if the starting two chars are - '#!'. How do I do this?

Load the two numbers using fgetc() (as #WilliamPursell suggested) and then compare them:
int i1, i2;
FILE *file;
file = fopen("yourfile", "rb");
if (file == NULL) {
printf("Error: failed to open file");
return 1;
}
i1 = fgetc(file);
i2 = fgetc(file);
// 23h...#, 21h...!
if (i1 == 0x23 && i2 == 0x21) {
// magic number
}

Related

Why am I getting an error in this C program? [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 5 months ago.
Improve this question
I am new to C programming but I stumbled on this code
int print(int nb)
{
if (nb < 0)
{
return (0);
}
printf("%d", nb + print(nb - 1));
nb --;
return (nb);
}
int main(void)
{
print(4);
return (0);
}
I ran the code and it gave me an output of 00246
why is that the output that, looking at it logically, the answer is not suppose to start with a 0
print(4) -> print(3) -> print(2) -> print(1) -> print(0) -> print(-1)
print(-1) stops the recursion returning 0, thus a call to printf() is emitted with 0 + 0, which is 0.
print(0) ends with -1 as value, and a call to printf() with 1 + -1 is emitted, which is 0.
etc.

When I run my code it shows some errors on terminal. Could any can tell me what is the error stands for? How can I debug it? [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 4 years ago.
Improve this question
Errors:
Test isPower2(0[0x0]) failed.
Gives 1[0x1]. Should be 0[0x0]
Code:
int isPower2(int x) {
int nonNega = (x>>31);
int result = !((x & (x-1)) ^ nonNega);
return result;
}
Your isPower2(0) returns true (1) but 0 is not a power of 2. So the expected result would be false (0).

Format for tokenizing "..." strings [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 8 years ago.
Improve this question
Lets say that I have file which contains the data
Data1 "X1 Y1 Z1"
Data2 "X2 Y2 Z2"
Data3 "X3 Y3 Z3"
Generally, how would I scan the file and make my program count "X1 Y1 Z1" as a single token?
#include <stdio.h>
int main(){
FILE *fp = fopen("data.txt", "r");
char data_name[16];
char data_string[32];
while(2==fscanf(fp, "%15s \"%31[^\"]\"", data_name, data_string)){
printf("%s, %s\n", data_name, data_string);
}
fclose(fp);
return 0;
}

About tail recursion [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 8 years ago.
Improve this question
Recently I am reading Mastering Algorithms with c, and in this book I have 1 exercise that I am not able to implement with c.
Tn = 1 if n=1 ,
Tn = 2T(n/2) + n if n > 1
Anyone can help me? I'll appreciate it a lot.
I have tried.
#include <stdio.h>
int test(int n) {
if (n == 1)
return 1;
else if( n > 1 )
return test(n / 2) * 2 + n;
}
int testtail(int n, int running_result) {
if (n == 1)
return running_result;
else
**return testtail(n / 2, ???? );** // How can I implement the second param
}
I am sorry guys! I am not a native English speaker! Maybe I made some mistakes in grammer! I should apologize for this!

How can i read characters from a file and split it into two 16bits? [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 8 years ago.
Improve this question
I want to read d characters of a file and split that into two 16bits say for example:
text[1]=0x7469206564616d20;
text[2]=0x7469206564616d20;
How to do that?
INSTEAD OF TEXT[0] AND TEXT[1] PREDEFINED VALUES I WANT IT 2 READ D CHARACTERS FROM A FILE AND SPLIT IT IN2 THAT FORM
You probably want this:
u64 text[2] ;
FILE *input = fopen("myfile.data", "r") ;
if (input == NULL)
{
printf ("Unable to open file\n") ;
return 1 ;
}
while (!feof(input))
{
int charsread ;
charsread = fread(text, 16, 1, input) ; // read at most 16 bytes into the text array
// process your text array here
// charsread contains the number of characters actually read
// this can be less than 16 if the total file length is not a
// multiple of 16. You mus deal with that.
}
fclose(input) ;

Resources