Hello guys, first off let me tell you I'm not experienced at all with C, so bare with me please. I'm helping a friend with one of his school C exercises, but we're stuck in one part, so I'm hoping some of you more advanced programmers can help us out. The exercise is basically making a program that can read a configuration file containing a map with the location of a series of mines, and it should provide the user with an interface to make changes to the map, like adding new mines or blow up an existing mine. Each position on the map is represented by 2 coordinates x and y. These coordinates are integers and they can assume values in the range [0, 24]. Each of the map's coordinates may be empty (without a mine) or have a mine, and each mine can only assume 2 states, either armed or off. The program should start by presenting the following menu to the user:
+-----------------------------------------------------
read <filename> - read input file
show - show the mine map
trigger <x> <y> - trigger mine at <x> <y>
plant <x> <y> - place armed mine at <x> <y>
export <filename> - save file with current map
quit - exit program
sos - show menu
+-----------------------------------------------------
The program then waits for user to input one of the commands from above. Now, the command we're having trouble with is the "read <filename>" one. When the user enters the read command, the program should read the file with the name "filename" and build the corresponding map. The input file will be a text-encoded file that should contain a set of pairs of coordinates that represent the locations of the mines in armed state. Mines in an off state or empty spaces are not represented in the file. Each coordinate is represented by a pair of integer values (X Y) separated by a blank space, X being the row number and Y the column number. There is no restriction regarding line breaks within the file or the order of each pair of coordinates within the file.
Example of input file:
The file must be read until the end of the file is detected. For each X coordinate represented in the file, there must be a Y coordinate. If this is not the case, it means that the file is poorly formatted. In this case, the program should show stderr stream this message: "File is corrupted".
With all that said, let me show you how far we've gotten with the piece of code we have for the read command:
We're not even sure if that's a good solution, but it is indeed working when the file is correctly formatted, as it prints out every pair of coordinates. What we don't know how to do is when the file is corrupted. Let's imagine a corrupted file like this:
How are we supposed to get the "File is corrupted" message with an input file like the above? We have 3 single numbers, how can we catch them?
+-----------------------------------------------------
read <filename> - read input file
show - show the mine map
trigger <x> <y> - trigger mine at <x> <y>
plant <x> <y> - place armed mine at <x> <y>
export <filename> - save file with current map
quit - exit program
sos - show menu
+-----------------------------------------------------
The program then waits for user to input one of the commands from above. Now, the command we're having trouble with is the "read <filename>" one. When the user enters the read command, the program should read the file with the name "filename" and build the corresponding map. The input file will be a text-encoded file that should contain a set of pairs of coordinates that represent the locations of the mines in armed state. Mines in an off state or empty spaces are not represented in the file. Each coordinate is represented by a pair of integer values (X Y) separated by a blank space, X being the row number and Y the column number. There is no restriction regarding line breaks within the file or the order of each pair of coordinates within the file.
Example of input file:
Code
0 1
0 2
0 3
5 9 6 4 6 5
0 4
0 5
1 4 2 3 2 6 2 9
3 4
3 9 4 9
5 2
5 3
5 4
6 9 7 6
7 2
8 3
8 6The file must be read until the end of the file is detected. For each X coordinate represented in the file, there must be a Y coordinate. If this is not the case, it means that the file is poorly formatted. In this case, the program should show stderr stream this message: "File is corrupted".
With all that said, let me show you how far we've gotten with the piece of code we have for the read command:
Code
#include <stdio.h>
#include <string.h>
#define SIZE 128
int main(void)
{
char line[SIZE], file_name[SIZE], command[SIZE];
int x[SIZE], y[SIZE], i = 0;
while (1)
{
fgets(line, SIZE, stdin);
if (!strncmp(line, "read", 4))
{
sscanf(line, "%s %s", command, file_name);
FILE *fp = fopen(file_name, "r");
while (fscanf(fp, "%d %d", &x[i], &y[i]) == 2)
{
printf("%d %d ", x[i], y[i]);
}
if (fp == NULL)
{
fputs("Couldn't open file.\n", stderr);
}
}
}
}We're not even sure if that's a good solution, but it is indeed working when the file is correctly formatted, as it prints out every pair of coordinates. What we don't know how to do is when the file is corrupted. Let's imagine a corrupted file like this:
Code
0 1
0 2
0 3
5 9 6 4 6 5
0 4
0 5
1 4 2 3 2 6 2
3 4
3 9 4 9
5
5
5 4
6 9 7 6
7 2
8 3
8 6How are we supposed to get the "File is corrupted" message with an input file like the above? We have 3 single numbers, how can we catch them?
null



![[Image: ZjBA6fR.gif]](https://i.imgur.com/ZjBA6fR.gif)