Read Magic Number from .au File - c

I wrote a small program to get the magic number from an .au file and print it to console, but every time I try, instead of getting the intended .snd, I get .snd$ instead.
I'm not sure why this is happening, considering that I'm only reading in 4 bytes, which is what the magic number is comprised of. So, where is the extra character coming from?
#include <stdio.H>
int main()
{
FILE *fin;
int r;
char m[4], path[20];
scanf("%s", path);
fin = fopen(path, "r");
r = fread(&m, sizeof(char), 4, fin);
printf("magic number is %s\n", m);
return 0;
}

You're printing it as though it were a string, which in C, means that it's NUL-terminated. Change your code like this and it will work as you expect:
char m[5];
m[4] = '\0'; /* add terminating NUL */
Also, you should be aware that scanf is a dangerous function. Use a command line argument instead.

The problem is not how you are reading.
The problem is that your variable is only 4 chars length, and it needs a null character to indicate the end.
printf with %s will print the content of the variable until reach a null character, until that it can print garbage if your variable is not correctly ended.
To fix you can have a bigger variable and set the [4] char with null.
How the new code should look like:
#include <stdio.H>
int main()
{
FILE *fin;
int r;
char m[5], path[20];
scanf("%s", path);
/*Scanf can be dangerous because it can cause buffer overflow,
it means that you can fill your variable with more bytes than it supports, which can end up being used for buffer overflow attacks:
See more: http://en.wikipedia.org/wiki/Buffer_overflow */
fin = fopen(path, "r");
r = fread(&m, sizeof(char), 4, fin);
m[4] = '\0';
printf("magic number is %s\n", m);
return 0;
}

Related

Does fscanf move the file pointer backwards?

These are the contents of my file, 'unsorted.txt' :
3 robert justin trump
This is my code:
#include <stdio.h>
int main(void) {
FILE *f = fopen("unsorted.txt", "r");
char n;
printf("%d\n", ftell(f));
fscanf(f, "%s", &n);
int l = n - '0';
printf("%d %d\n", l, ftell(f));
return 0;
}
on execution it gives the following output:
0
3 -1
why did it return -1 in second case? It should move from 0 to 1 right?
NOTE: the file can be opened, because then how would it print 0 in the first call and the first character from the file without being able to be opened?
fscanf(f,"%s",&n);
is very wrong, since you declared char n; (of only one byte). You got undefined behavior. Be very scared (and next time, be ashamed).
I recommend:
Test that fopen don't fail:
FILE *f = fopen("unsorted.txt","r");
if (!f) { perror("fopen unsorted.txt"); exit(EXIT_FAILURE); };
Declare a buffer of reasonable size (80 was the size of punched cards in the 1970s).
char buf[80];
clear it (you want defensive programming):
memset(buf, 0, sizeof(buf));
Then read carefully about fscanf. Read that documentation several times. Use it with a fixed size and test its result:
if (fscanf(f, "%72s", buf) > 0) {
(72 was the usable size in PL/1 programs of punched cards; it is less than 80)
Don't forget to read documentation of other functions, including ftell.
Important hint:
compile with all warnings and debug info (gcc -Wall -Wextra -g with GCC), improve the code to get no warnings, use the debugger gdb to run it step by step.
PS. As an exercise, find the possible content of unsorted.txt which made your initial program run correctly. Could you in that case predict its output? If not, why??
There are multiple problems in your code:
You do not test the return value of fopen(). Calling ftell() with a NULL pointer has undefined behavior. You cannot draw conclusions from observed behavior.
printf("%d\n", ftell(f)); is incorrect because the return value of ftell() is a long. You should use the format %ld.
fscanf(f, "%s", &n); is incorrect because you pass the address of a single char for fscanf() to store a null-terminated string. fscanf() will access memory beyond the size of the char, which has undefined behavior. Define an array of char such as char buf[80]; and pass the maximum number of characters to store as: fscanf(f, "%79s", buf); and check the return value, or use %c to read a single byte.
int l = n - '0'; is not strictly incorrect, but it is error prone: avoid naming a variable l as it looks confusingly similar to 1.
printf("%d %d\n", l, ftell(f)); is incorrect as the previous call to printf: use the conversion specifier %ld for the return value of ftell().
Note also that the return value of ftell() on a text stream is not necessarily the byte offset in the file.
Here is a corrected version:
#include <stdio.h>
int main(void) {
FILE *f = fopen("unsorted.txt", "r");
char c;
if (f != NULL) {
printf("%ld\n", ftell(f));
if (fscanf(f, "%c", &c) == 1) {
int diff = c - '0';
printf("%d %ld\n", diff, ftell(f));
}
}
return 0;
}
Output:
0
3 1

Storing String Inside a String?

My problem is when I try to save the string (series[0]) Inside (c[0])
and I display it, it always ignore the last digit.
For Example the value of (series[0]) = "1-620"
So I save this value inside (c[0])
and ask the program to display (c[0]), it displays "1-62" and ignores the last digit which is "0". How can I solve this?
This is my code:
#include <stdio.h>
int main(void)
{
int price[20],i=0,comic,j=0;
char name,id,book[20],els[20],*series[20],*c[20];
FILE *rent= fopen("read.txt","r");
while(!feof(rent))
{
fscanf(rent,"%s%s%s%d",&book[i],&els[i],&series[i],&price[i]);
printf("1.%s %s %s %d",&book[i],&els[i],&series[i],price[i]);
i++;
}
c[0]=series[0];
printf("\n%s",&c[0]);
return 0;
}
The use of fscanf and printf is wrong :
fscanf(rent,"%s%s%s%d",&book[i],&els[i],&series[i],&price[i]);
Should be:
fscanf(rent,"%c%c%s%d",&book[i],&els[i],series[i],&price[i]);
You have used the reference operator on a char pointer when scanf expecting a char pointer, also you read a string to book and else instead of one character.
printf("1.%s %s %s %d",&book[i],&els[i],&series[i],price[i]);
Should be:
printf("1.%c %c %s %d",book[i],els[i],series[i],price[i]);
And:
printf("\n%s",&c[0]);
Should be:
printf("\n%s",c[0]);
c is an array of char * so c[i] can point to a string and that is what you want to send to printf function.
*Keep in mind that you have to allocate (using malloc) a place in memory for all the strings you read before sending them to scanf:
e.g:
c[0] = (char*)malloc(sizeof(char)*lengthOfString+1);
and only after this you can read characters in to it.
or you can use a fixed size double character array:
c[10][20];
Now c is an array of 20 strings that can be up to 9 characters long.
Amongst other problems, at the end you have:
printf("\n%s",&c[0]);
There are multiple problems there. The serious one is that c[0] is a char *, so you're passing the address of a char * — a char ** — to printf() but the %s format expects a char *. The minor problem is that you should terminate lines of output with newline.
In general, you have a mess with your memory allocation. You haven't allocated space for char *series[20] pointers to point at, so you get undefined behaviour when you use it.
You need to make sure you've allocated enough space to store the data, and it is fairly clear that you have not done that. One minor difficulty is working out what the data looks like, but it seems to be a series of lines each with 3 words and 1 number. This code does that job a bit more reliably:
#include <stdio.h>
int main(void)
{
int price[20];
int i;
char book[20][32];
char els[20][32];
char series[20][20];
const char filename[] = "read.txt";
FILE *rent = fopen(filename, "r");
if (rent == 0)
{
fprintf(stderr, "Failed to open file '%s' for reading\n", filename);
return 1;
}
for (i = 0; i < 20; i++)
{
if (fscanf(rent, "%31s%31s%19s%d", book[i], els[i], series[i], &price[i]) != 4)
break;
printf("%d. %s %s %s %d\n", i, book[i], els[i], series[i], price[i]);
}
printf("%d titles read\n", i);
fclose(rent);
return 0;
}
There are endless ways this could be tweaked, but as written, it ensures no overflow of the buffers (by the counting loop and input conversion specifications including the length), detects when there is an I/O problem or EOF, and prints data with newlines at the end of the line. It checks and reports if it fails to open the file (including the name of the file — very important when the name isn't hard-coded and a good idea even when it is), and closes the file before exiting.
Since you didn't provide any data, I created some random data:
Tixrpsywuqpgdyc Yeiasuldknhxkghfpgvl 1-967 8944
Guxmuvtadlggwjvpwqpu Sosnaqwvrbvud 1-595 3536
Supdaltswctxrbaodmerben Oedxjwnwxlcvpwgwfiopmpavseirb 1-220 9698
Hujpaffaocnr Teagmuethvinxxvs 1-917 9742
Daojgyzfjwzvqjrpgp Vigudvipdlbjkqjm 1-424 4206
Sebuhzgsqpyidpquzjxswbccqbruqf Vuhssjvcjjylcevcisdzedkzlp 1-581 3451
Doeraxdmyqcbbzyp Litbetmttcgfldbhqqfdxqi 1-221 2485
Raqqctfdlhrmhtzusntvgbvotpk Iowdcqlwgljwlfvwhfmw 1-367 3505
Kooqkvabwemxoocjfaa Hicgkztiqvqdjjx 1-466 435
Lowywyzzkkrazfyjuggidsqfvzzqb Qiginniroivqymgseushahzlrywe 1-704 5514
The output from the code above on that data is:
0. Tixrpsywuqpgdyc Yeiasuldknhxkghfpgvl 1-967 8944
1. Guxmuvtadlggwjvpwqpu Sosnaqwvrbvud 1-595 3536
2. Supdaltswctxrbaodmerben Oedxjwnwxlcvpwgwfiopmpavseirb 1-220 9698
3. Hujpaffaocnr Teagmuethvinxxvs 1-917 9742
4. Daojgyzfjwzvqjrpgp Vigudvipdlbjkqjm 1-424 4206
5. Sebuhzgsqpyidpquzjxswbccqbruqf Vuhssjvcjjylcevcisdzedkzlp 1-581 3451
6. Doeraxdmyqcbbzyp Litbetmttcgfldbhqqfdxqi 1-221 2485
7. Raqqctfdlhrmhtzusntvgbvotpk Iowdcqlwgljwlfvwhfmw 1-367 3505
8. Kooqkvabwemxoocjfaa Hicgkztiqvqdjjx 1-466 435
9. Lowywyzzkkrazfyjuggidsqfvzzqb Qiginniroivqymgseushahzlrywe 1-704 5514
10 titles read

create a binary file in C

I'm currently working on a binary file creation. Here is what I have tried.
Example 1:
#include<stdio.h>
int main() {
/* Create the file */
int a = 5;
FILE *fp = fopen ("file.bin", "wb");
if (fp == NULL)
return -1;
fwrite (&a, sizeof (a), 1, fp);
fclose (fp);
}
return 0;
}
Example 2:
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char str[256] = {'\0'};
strcpy(str, "3aae71a74243fb7a2bb9b594c9ea3ab4");
fp = fopen("file.bin", "wb");
if(fp == NULL)
return -1;
fwrite(str, sizeof str, 1, fp);
return 0;
}
Example 1 gives the right output in binary form. But Example 2 where I'm passing string doesn't give me right output. It writes the input string which I have given into the file and appends some data(binary form).
I don't understand and I'm unable to figure it out what mistake I'm doing.
The problem is that sizeof str is 256, that is, the entire size of the locally declared character array. However, the data you are storing in it does not require all 256 characters. The result is that the write operation writes all the characters of the string plus whatever garbage happened to be in the character array already. Try the following line as a fix:
fwrite(str, strlen(str), 1, fp);
C strings are null terminated, meaning that anything after the '\0' character must be ignored. If you read the file written by Example 2 into a str[256] and print it out using printf("%s", str), you would get the original string back with no extra characters, because null terminator would be read into the buffer as well, providing proper termination for the string.
The reason you get the extra "garbage" in the output is that fwrite does not interpret str[] array as a C string. It interprets it as a buffer of size 256. Text editors do not interpret null character as a terminator, so random characters from str get written to the file.
If you want the string written to the file to end at the last valid character, use strlen(str) for the size in the call of fwrite.

Not understanding the C format specifiers when using fscanf()

So I am reading a text file in this format:
ABC 51.555 31.555
DEF 23.445 45.345
I am trying to use fscanf() to parse the data, because this file could grow or shrink it needs to be dynamic in the way it loads hence why i used malloc and i also want to store it in the struct below. I think the issue is with a space or even possible not writing the whole format specifier right. Here is my code.
typedef struct data
{
char name[4];
char lat[7];
char lng[7];
}coords;
int main(int argc, char *argv[])
{
////////////CREATES FILE POINTER/////////
FILE* fp;
///////////CREATES MALLOC POINTER TO STORE STRUCTS/////////////
coords* cp;
//////////OPENS FILE//////////
fp = fopen(argv[1], "r");
/////////GET THE TOTAL AMMOUNT OF LINES IN THE FILE/////////
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
rewind(fp);
//////SKIPS FIRST LINE//////////
while(fgetc(fp) != (int)'\n')
{};
/////////ASSIGNS MEMORY THE SIZE OF THE FILE TO //////////
cp = malloc(sizeof(coords) * size);
//////////READS FILE AND STORES DATA///////
fscanf(fp,"%s[^ ] %s[^ ] %s[^\n]", cp->name, cp->lat, cp->lng);
printf("%s\n%lf\n%lf\n", cp->name, cp->lat, cp->lng);
fclose(fp);
return 0;
}
And yes I am aware I did not include the header files but I have got the right ones stdlib and stdio
UPDATE 1:
I have tried both replies and I get this on my screen:
ABC51.555
0.000000
0.000000
How come the 51.555 has not gone to the next item in the struct?
Thanks
///////////////////////////////////////////////////////////////UPDATE 2////////////////////////////////////////////////////////
Okay I have modified my code to do the following.
typedef struct data
{
char name[4];
char lat[6];
char lng[6];
}coords;
int main(int argc, char *argv[])
{
////////////CREATES FILE POINTER/////////
FILE* fp;
///////////CREATES MALLOC POINTER TO STORE STRUCTS/////////////
coords* cp;
//////////OPENS FILE//////////
fp = fopen(argv[1], "r");
/////////GET THE TOTAL SIZE OF THE FILE/////////
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
long lines = -1;
rewind(fp);
//////GETS TOTAL AMMOUNT OF LINES/////////
char c;
while(c != EOF)
{
c = fgetc(fp);
if(c == '\n')
{
lines++;
}
}
rewind(fp);
////////////SKIPS FIRST LINE//////////
while(fgetc(fp) != (int)'\n')
{};
/////////ASSIGNS MEMORY THE SIZE OF THE FILE TO //////////
cp = malloc(sizeof(coords) * size);
//////////READS FILE AND STORES DATA///////
printf("Lines of text read: %d\n", lines);
fscanf(fp,"%s %s %s[^\n]", cp[0].name, cp[0].lat, cp[0].lng);
printf("%s\n", cp[0].name);
fclose(fp);
return 0;
}
Now when i try to print cp[0].name; I get the whole of the first line with no space in, like this.
ABC51.55531.555
If i got print cp[0].lat; I get this.
51.55531.555
And when i print cp[0].lng; I get this.
31.555
Which is the only correct one, I can not understand this behaviour. Why is it behaving like this? all the posts suggest (As i first thought) that each %s in fscanf would put it in to its own variable not concatenate them. Not mater if i use the dot notation or the direct -> it still has the same result.
Thanks :)
The format specifier "%s[^... attempts to read a whitspace delimited string, followed by the character [ and then the character ^. Since the string will always end at whitespace, the next character will always be whitespace, which won't match the [, and none of the rest of the format specifier will match.
ALWAYS check the return value of fscanf to make sure you read all the things you thing you did. If the return value is wrong, give a diagnostic.
ALWAYS use field size limits when reading into fixed size string arrays.
So in your case what you want is:
if (fscanf(fp, "%3s%6s%6s", cp->name, cp->lat, cp->lng) != 3) {
fprintf(stderr, "Incorrect data in input file, exiting!\n");
abort(); }
I'm not sure that you want to use the space delimiter [^ ]. fscanf already parses the string on whitespace as default. Try this and see if the string is correctly parsed:
fscanf(fp, "%s %s %s[^\n]", cp->name, cp->lat, cp-lng);
output should result in:
cp->name ---- ABC
cp->lat ----- 51.555
cp->lng ----- 31.555

confused by C syntax

i'm new at c.. and still having trouble at the syntax, hope you can help me... cause i'm stuck at this code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
FILE *stream = NULL;
stream = fopen("studentinfo.txt", "a+");
/*some of initialization were used for testing purposes only*/
char arr[5];
char arr2[5];
int i;
char name[3];
char course[5];
printf("enter details: ");
scanf("%s", arr2);
while(!feof(stream)){
fgets(arr, 100, stream);//i am confused if the line capture was stored at arr[0]
if(strcmp(arr, arr2)==0){//i want to compare
printf("success");//testing
}
printf("%s", arr);//i wonder does fgets just overwrites the new line to arr[0]
}
fclose(stream);
getch();
}
thanks guys...
You're opening studentinfo.txt for appending, but then reading from it (and you don't check the open succeeded
you've allocated 5 characters for arr, but read up to 100 characters into it with the fgets. This will overflow and cause memory corruption
you've allocated 5 characters for arr2, but read an arbitary number of characters into it - this will overflow and cause memory corruption
Fgets reads characters into memory starting at arr. arr[0] is the first character. &arr[0] is the same as arr
What's the getch() at the end for?
Also, "a+" positions the stream at the end of the file, so you won't be able to read anything.
if you have an existing file... and your file has data on it. then you could check if the data you typed is existing on the file or not. i'm not sure if this is what you want.
example if you typed... love
and the file also contains the exact word... love (on one line)
then it will print "success".
if the data you typed is not existing on the file, it will be appended on the file (on the next line).
int main(void){
char arr[5];
char arr2[5];
int i;
int n=0;
FILE *stream = NULL;
FILE *append = NULL;
stream = fopen("studentinfo.txt", "rt");
append = fopen("studentinfo.txt", "a+");
printf("enter details: ");
scanf("%s", arr2);
while(!feof(stream)){
fgets(arr, 6, stream);
if(strcmp(arr, arr2)==0){
printf("success");
} else n=-1;
}
if (n==-1){
fprintf(append, "%s\n", arr2);
}
fclose(stream);
fclose(append);
system("pause");
}
I am not sure why you are opening the stream with a+ because you never actually write to it. Maybe you want to make sure the file exists even if 0 length? You should still check that the open succeeded though.
You are then reading 100 characters into an array of just 5 bytes so you will get a serious memory overwrite if the file really does contain that number.
The scanf is unsafe too of course as the user may enter too many characters (they are actually limited to 4 because there is a NULL terminator that gets read).
At the end you appear to be writing the last line randomly if the user did not enter a matching line from the file.

Resources