EDIT: Guess it took me too long to write this, I see you got it working :P. Still, much of this is worth considering.
In the future, please provide more complete information, such as the input file you are using that is causing problems. Also, please post code that is not full of strange characters like \240 and \302. Here's what I got when I first compiled
Code:
$ make foo
gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
foo.c:4:1: error: stray ‘\302’ in program
foo.c:4:1: error: stray ‘\240’ in program
foo.c:5:1: error: stray ‘\302’ in program
How do you know it doesn't write anything to the binary file? It seemed to work just fine for me:
Code:
$ cat matches.txt
aaa,bbb,1,2
bbb,ccc,2,3
ccc,ddd,3,4
ddd,eee,4,5
eee,aaa,5,1
$ hexdump -C matchesbin.bin
00000000 61 61 61 00 00 00 00 00 00 00 00 00 00 00 00 00 |aaa.............|
00000010 00 00 00 00 62 62 62 00 00 00 00 00 00 00 00 00 |....bbb.........|
00000020 00 00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 |................|
00000030 0a 62 62 62 00 00 00 00 00 00 00 00 00 00 00 00 |.bbb............|
00000040 00 00 00 00 63 63 63 00 00 00 00 00 00 00 00 00 |....ccc.........|
00000050 00 00 00 00 00 00 00 00 02 00 00 00 03 00 00 00 |................|
00000060 0a 63 63 63 00 00 00 00 00 00 00 00 00 00 00 00 |.ccc............|
00000070 00 00 00 00 64 64 64 00 00 00 00 00 00 00 00 00 |....ddd.........|
00000080 00 00 00 00 00 00 00 00 03 00 00 00 04 00 00 00 |................|
00000090 0a 64 64 64 00 00 00 00 00 00 00 00 00 00 00 00 |.ddd............|
000000a0 00 00 00 00 65 65 65 00 00 00 00 00 00 00 00 00 |....eee.........|
000000b0 00 00 00 00 00 00 00 00 04 00 00 00 05 00 00 00 |................|
000000c0 0a 65 65 65 00 00 00 00 00 00 00 00 00 00 00 00 |.eee............|
000000d0 00 00 00 00 61 61 61 00 00 00 00 00 00 00 00 00 |....aaa.........|
000000e0 00 00 00 00 00 00 00 00 05 00 00 00 01 00 00 00 |................|
000000f0 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000100 00 00 00 00 61 61 61 00 00 00 00 00 00 00 00 00 |....aaa.........|
00000110 00 00 00 00 00 00 00 00 05 00 00 00 01 00 00 00 |................|
00000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
...<snip>...
00002560 ff ff ff ff 00 00 00 00 b8 1c 92 bc ff 7f 00 00 |................|
00002570 a8 31 95 0d 86 7f 00 00 c0 94 f2 0d 86 7f 00 00 |.1..............|
Other things:
- Don't use magic numbers. 20 and 200 are meaningless. Use constants with sensible names: MAX_TEAM_NAME, MAX_MATCHES.
- Work on better names. For example, PtrToTxt and PtrToBinary are confusing since they can imply that they point to generally usable memory, not a file structure. Consider names like TextFilePtr and BinaryFilePtr or InputFilePtr and OutputFilePtr instead, which stress that these are for file handling.
- If you fail opening the binary file, you should close the input file before you exit.
- You're checking the value of fscanf incorrectly: scanf(3): input format conversion - Linux man page. Notice the return value section, that states it returns the number of items successfully scanned in. You ask for 4 items (2 %[ and 2 %i). If it returns any number other than 4, you have a problem.
- You're calling fwrite incorrectly (specifically the second and third parameters): fwrite(3): binary stream input/output - Linux man page. You may get away with it, but it's not a good idea. The second parameter should be the size of a single element, i.e. sizeof(matchresults[0]). The third parameter should be the number of elements, i.e. MAX_MATCHES or you could use the calculation sizeof(matchresults)/sizeof(matchresults[0]).
- You could declare a single struct matches to read into with fscanf (see below).
- When your fscanf loop terminates, you should check to see why it stopped. You can use the feof and ferror functions (man page).
Code:
struct matches foo;
...
while (fscanf(InputFilePtr, "%[^,] , %[^,] , %i , %i", foo.teamA, foo.teamB, &foo.scoreA, &foo.scoreB) == 4)
...
if ferror
print error and exit
else if not feof
loop stopped before reaching end of file, but no error...possible input file has bad format
print error and exit