Reputation: 25
I am trying to understand how you can handle some unwanted input with fscanf.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *myFile;
myFile = fopen("../map1.map", "r");
if (myFile == NULL){
printf("Couldn't open file.\n");
exit(-1);
}
char buffer[16];
if (fscanf(myFile, "%15s", buffer) != 1){
printf("The file is not formated correctly.\n");;
fclose(myFile);
exit(-1);
}
}
I have this code. I want to make sure that I print an error and then exit out of the program in these scenarios (I dont care what happens with the overflowing characters I just want to exit):
Can't find/open the file. (This works)
The buffer gets overflowed. I dont want it to just grab the first 15 characters in this case. I want the program to exit. I also want to be able to continue reading the file later in the program. It shouldnt matter if there are more lines in the file. (This is the main issue).
I get no value at all (This works.)
I get 2 words (example: "hello" = 1 words, "hello world" = 2 words) (I cant figure this out either)
I want to know how I can test these things. What comparisons do I use etc.
Im not a very experienced user so please point out any obvious flaws and I will fix them.
To make it clearer. I dont want to make this work. I want to know how to exit out if it doesnt work. This works fine with the intended input.
Upvotes: 0
Views: 244
Reputation: 9203
For 2. You can do an early detection as -
int length;
fscanf(myFile, "%*[^\n]%n",&length);
if(length > 10)
//exit with failure
You can then again compare length with 10
. Do remember to fseek
back to where you were using
fseek(myFile, -length, SEEK_CUR);
For case 4 - since you already know the end all you have to do is read a word and check if you have read all as -
fscanf(myFile, "%15s", buffer);
if (strlen(buffer) < length)
//return failure and exit
Upvotes: 1
Reputation: 25536
At very first, your buffer needs to be large enough to store the characters read from the file, including the terminating null character:
fscanf(myFile, "%15s", buffer)
So your buffer needs to be 16 characters large!
Then for checking the string not to be cut off, read one character more(!):
char buffer[17]; // sufficiently large to store string + 0 character
if (fscanf(myFile, "%16s", buffer) != 1)
// ^ (!)
And then check the string length:
else if(strlen(buffer) > 15)
// invalid string
Edit: handling the 'multiple words' problem...
Assuming you've read the word successfully as above, you need yet to check if another one follows. So you need to analyze the rest of the following whitespace:
for(char c = fgetc(myFile); c != EOF && c != '\n'; c = fgetc(myFile))
// ^ not the desired one
// ^ not end of file, we are not yet done
{
if(!isspace((unsigned char)c))
exit(-1); // undesired non-whitespace (-> multiple words)
}
As you exit anyway, you do not need to care about losing the first character of the subsequent word either...
Upvotes: 0
Reputation: 40145
try like this
char buffer[32+1];//First prepare a buffer of sufficient length.
char word[16+1], extra_word[4];
if (fscanf(myFile, "%32[^\n]%*c", buffer) != 1 || //try read one line (or use fgets)
strlen(buffer)>16 || //Examine the length or length longer than the expected format
sscanf(buffer, "%s %3s", word, extra_word) == 2){//extra word exist ?
printf("The file is not formated correctly.\n");
fclose(myFile);
exit(-1);
}
Upvotes: 0