I'm working on a dictionary implementation in C, which requires me to read in a multiline file with the word and definitions. I can get the file to read in correctly, however instead of EOF, a full stop . is used to mark the end of the file. I have tried to stop the program from reading the file once it reaches the . but to no avail.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_WORD_SIZE 40
#define MAX_DESC_SIZE 200
int d_read_from_file(const char * filename){
char dbuffer[MAX_WORD_SIZE+MAX_DESC_SIZE+2];
char word[MAX_WORD_SIZE+1];
char meaning[MAX_DESC_SIZE+1];
int i = 0;
FILE *file = fopen(filename, "r");
if (file == 0)
{
printf("Could not open file\n");
return 0;
}
while((fgets(dbuffer, sizeof(dbuffer), file))) {
if (strcmp(word, ".") == 0) {
break;
}
sscanf(dbuffer, "%s %[^\n]",word, meaning);
printf("%s\n", word);
printf("%s\n", meaning);
}
return 1;
/* d_read_from_file(filename);*/
}
int main(int argc, char ** argv)
{
int i;
for (i=1; i<argc; i++)
d_read_from_file(argv[i]);
}
I know the code looks a bit messy right now, but I was just trying to get it to stop once it hit the . character.
Here is an example of the input:
computer Electronic device for processing data according to instructions
playground Area of outdoor play or recreation
plateau Land area with high, level surface
aardvark Unfriendly, nocturnal mammal native to Africa
.
And the output I get from the code I have written:
computer
Electronic device for processing data according to instructions
playground
Area of outdoor play or recreation
plateau
Land area with high, level surface
aardvark
Unfriendly, nocturnal mammal native to Africa
.
Unfriendly, nocturnal mammal native to Africa
It appears to continue once more around the loop and using . as the word and then printing out the last meaning to be read in. Any ideas on how to fix this? Also if there is a more efficient way of doing what I am doing then also let me know.
You're checking word
before extracting it from dbuffer
. It still has the value from the last time around the loop.
Since the last line may or may not include a newline, the easiest way out is to check dbuffer
against both versions before proceeding:
while((fgets(dbuffer, sizeof(dbuffer), file))) {
if ((strcmp(dbuffer, ".") == 0) || strcmp(dbuffer, ".\n") == 0) {
break;
}
// ...
}
Quick answer: change:
if (strcmp(word, ".") == 0) {
break;
}
sscanf(dbuffer, "%s %[^\n]",word, meaning);
to:
sscanf(dbuffer, "%s %[^\n]",word, meaning);
if (strcmp(word, ".") == 0) {
break;
}
Also, it is good practice to check that the return value of sscanf
here is 2
, otherwise meaning
is meaningless, and will likely have the previous value, as your output shows. If it is not 2
, and word
is not "."
, then your input file has a syntax error, and your should eg quit with an error message.
And, in your original code, word
was uninitialized the first time it was checked.
sscanf(dbuffer, "%s %[^\n]",word, meaning);
printf("%s\n", word);
printf("%s\n", meaning);
You scan the word in and print it before you check if it is "."
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.