Sudoku Game: How to test code that includes reading a binary file - c

I am assigned to create a Sudoku game, where the 9x9 table should take the first settings from a binary file. Every registry contains 3 bytes with this format:
1st byte is row
2nd byte is column
3rd byte is the number that we wish to enter, in the combination above
for example: 069
On the 1st row and 7th column we put number 9.
My question here is, how should I test the code(?) when my only choice for example is me creating a binary file and reading it again.

First, what's so bad about creating your own repository of test files? You can even write a program in bash or something to run your sudoku game on all of your input files automatically to check if your code still works.
If you are working with FILE* pointers, though, you can likely use fmemopen in test code to create an in-memory stream that you can use with fread, etc.
If you are working with fds, you can do something similar with pipe. Write into one end, read from the other.

Related

How to change file pointer or seperate lines when reading a file in NASM 16-bit?

I know how to write and read files in NASM 16-bit but is there a way to either move the file pointer to read a single line? Or at least split the lines in different strings? It's for a game that has a config file and a string list.
If you don't like my question, I'm just a beginner with files.
I don't think so. You're expected to read the file in chunks with INT 21h / AH=3Fh and parse it into lines yourself. Likewise, the only way to seek is AH=42h which takes a byte count, not a line count.
The OS does not keep any record of where the line breaks are in the file, so the only way to actually seek to a particular line would be to read through the entire file until the appropriate number of CRLFs have been seen. And that's something the application can do for itself, so there's no particular need to provide a separate system call for it.

How to use fscanf to read 2 different data types from a file?

I'm working on a code with the goal of reading a file that has list of random number combinations and random character combinations separated by a space.
eg
0016718719 #:#-;QZL=!9v
0140100781 &:`ziuiCM+UC
...and so on
I would like to read them into a binary search tree using the number as a key and the character combination as a linked piece of data. The number would represent something like an employee ID number and the character combination a password. I want to use an integer to store the numbers and a pointer to a string to store the characters in a while loop to transfer them into the tree.
test code to open the file
I have been trying to figure out the fscanf tool on a small scale program so far. The output of the program is the error accessing file message.
Any help is very much appreciated. I cannot figure out why the file won't open.

Writing matrix on binary file C

I am working in C with some binary files using the famous commands fwrite/fread.
I have to write pairs of numbers, one pair per line, like this:
double values[2];
for (int i=0 ; i<numPairs ; i++){
values[0]=rand();
values[1]=rand();
fwrite(&values, sizeof(double), 2, myFile);
}
where myFile is (as its name suggests) a file I've opened using fopen().
Although I’ve got a couple of questions:
in a binary file it is possible to write 2 numbers on the same line?
if so, will this command do the trick? I've been scavenging around for answers but I wasn't able to find something that confirms this point. It's ok with arrays and such, but for matrices...?
A binary file does not have a concept of "lines" - it's entirely up to your program.
Currently you write numPairs*2 doubles to the file, two at a time. You could equally well each double individually, or store them all in an array and wrote them all with one call to fwrite.
Likewise, the reading program is free to read them individually, or two at a time, or all at once.
I think you mean text file, since binary files don't have columns or rows just 1's and 0's which is only readable for computers

How to read a text based data file into an array in C?

I have to read a text based data file, with an unknown number of data points, into an array in C, but I can't work out how to do this. I can't even manage to get my program to successfully open the text file, let alone put it into an array etc
The file contains numerical values, so it is not a string it needs to be read into. Ideally this should be done by the user inputting the file name.
I basically need the program to:
Ask user to input file name (I understand this is just a simple printf job)
When the user inputs the file name, the program opens the text file, stores the data from it into an array of an appropriate size.
Print entire array to show that this has been done.
If anyone could give a step to step explanation of how this can be done I would really appreciate it.
Anything asked to be described step by step without asking your input would be copy of others work.
Best advice is to learn things step by step on your own.
File I/O in C: http://www.tutorialspoint.com/cprogramming/c_file_io.htm
If you want to add additional features like user input:How to read a string from user input in C, put in array and print
Do some research on file content and how it's being handled from program. (Seems that you are referring to ASCII format file).
You should have done some searching before asking this complexity level questions.
If you want same advice in future for this task, I suggest to add code here.

How to save a 2-Dimensional array to a file in C?

I am an beginner C programmer and I am currently working on a project to implement viola jones object detection algorithm using C. I would like to know how I would be able to store data in a 2-Dimensional array to a file that can be easily ported and accessed by different program files(e.g. main.c, header_file.h etc.)
Thank you in advance.
There's not quite enough detail to be sure what you're looking for, but the basic structure of what you want to do is going to look something like this:
open file.csv for writing
for(iterate through one dimension of the array using i)
{
for(iterate through the other dimension of the array using j)
{
fprintf(yourfilehandle,"%d,",yourvalue[i][j]);
}
fprintf(yourfilehandle,"\n");
}
close your file
As has been suggested by others, this will leave you with a .CSV file, which is a pretty good choice, as it's easy to read in and parse, and you can open your file in Notepad or Excel and view it no problems.
This is assuming you really meant to do this with C file I/O, which is a perfectly valid way of doing things, some just feel it's a bit dated.
Note this leaves an extraneous comma at the end of the line. If that bugs you it's easy enough to do the pre and post conditions to only get commas where you want. Hint: it involves printing the comma before the entry inside the second for loop, reducing the number of entries you iterate over for the interior for loop, and printing out the first and last case of each row special, immediately before and after the inner for loop, respectively. Harder to explain that to do, probably.
Here is a reference for C-style file I/O, and here is a tutorial.
Without knowing anything about what type of data you're storing, I would say to store this as a matrix. You'll need to choose a delimiter to separate your elements (tab or space are common choices, aka 'tsv' and 'csv', respectively) and then something to mark the end of a row (new line is a good choice here).
So your saved file might look something like:
10 162 1 5
7 1 4 12
9 2 2 0
You can also define your format as having some metadata in the first line -- the number of rows and columns may be useful if you want to pre-allocate memory, along with other information like character encoding. Start simple and add as necessary!

Resources