![]() |
|
[C] C - Read pairs of integers from file - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] C - Read pairs of integers from file (/showthread.php?tid=6129638) |
C - Read pairs of integers from file - Golden Kappa - Dec 26, 2020 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: Code 0 1The 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>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 1How 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? RE: C - Read pairs of integers from file - guardian - Dec 26, 2020 I am pretty tired, so I hope I got everything right Have a look at this By splitting each line and using a whitespace as the delimiter, also putting them all into an array afterwards, you could catch each line that only contains a single number, by simply checking whether the array contains more than one value, if the values are in the right format etc Please let me know, if I got something wrong, as I said, I am pretty tired haha RE: C - Read pairs of integers from file - cghhhhasd - Dec 27, 2020 Quote:There is no restriction regarding line breaks within the file or the order of each pair of coordinates within the file. Quote: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? This is your same file, re-formatted: Code 0 1Where are the 3 single numbers? There are 21 valid pairs and 1 invalid pair (Because it has no Y coordinate). What I did in my code was I read the entire text file into memory, went trough each one of its bytes and checked if it is between 0x30 and 0x39 (These are the numbers from 0 to 9 in the ASCII table). If it is, then I increase the number count. If the number count is even, it means we reached a new pair ("0 1 0 2 0 3 5" = "X Y X Y X Y X"), so we store the current number into X, otherwise it means we finished a pair so we will store it in Y. At the end I check if the total number of numbers in the text file is uneven/odd, which means there is an extra number without a pair, given the requirements of how the program should work you mentioned in your post. Here is the output of my code with that list above: Code STATUS: 2 // (CORRUPT_COORDINATE_FILE)My code, written for Windows: Code #include <Windows.h>RE: C - Read pairs of integers from file - saltycracker - Dec 27, 2020 (Dec 26, 2020 10:57 AM)Yawnerd Wrote: 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? Pro-tip.. When you post a question, post just the question. We don't all the other info. We just need the stuff for the question. Example: You have a ascii text file with pairs of numbers. Give example of text file. You need to read pairs of numbers. Note what is a valid pair. You need to indicate an error if single numbers(non-pairs) occur. Note what a non-pair number is. To answer your question.. scanf family of functions return(quote from my manpages) Quote:On success, these functions return the number of input items success‐ |