RyCSmith
RyCSmith

Reputation: 51

C -fscanf causing abort trap: 6 on EOF

I am new to C and have written a small program that reads a large number of tweets from a file, stores the hashtags in a hashtable and then prints the 10 hashtags that appear most frequently.

The program is now functional but I have been receiving an error that I do not understand, Abort trap: 6.

Through debugging I have determined that it occurs on the line:

if (fscanf(src_file, "%s", current_word) == EOF){

on the last iteration. Using printing, I've seen that the rest of the file is processed properly and that this always occurs when the aforementioned line hits the EOF.

What fixed the error was to increase the initial capacity of my char current_word[] variable from 257 to 1000. However, this is far larger than I will need for almost every word I process. Can anyone give me any more insight into what is going on with fscanf() when it reaches the end of the file and why I apparently need to allocate it more space?

Quick note: The code snippet calls functions that are not listed here but they were removed while chasing the error and don't affect the behavior of the error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "hashtable.h"
#include <ctype.h>

//NOTE: using 0 for all failiures and 1 for all success

int main(int argc, char **argv){
    //opens file via command line argument
    FILE *src_file = fopen(argv[1], "r");;
    if (src_file == NULL){
        fprintf(stderr, "There was an error opening the file.") ;
        return 0;
    }

    //define hashtable and malloc space
    hashtable* tweet_table = malloc(sizeof(hashtable));
    //read word by word and put any hashtags in hashtable
    char current_word[257];
    int looper = 1;
    while (looper == 1){
        if (fscanf(src_file, "%s", current_word) == EOF){
            looper = 0;
        }
        else if (current_word[0] == '#'){
            int i;
            for (i = 1; i < strlen(current_word); i+=1){
                current_word[i] = tolower(current_word[i]);
            }
            assert (put(current_word, tweet_table) == 1);
        }
    }

    //sorts and prints 10 most common tweets
    find_frequent(tweet_table);

    //close file when finished with operations
    fclose(src_file);
    printf("all good");

    return 1;
}

Upvotes: 1

Views: 543

Answers (1)

chux
chux

Reputation: 154130

Amend code to prevent from reading to much into current_word @Jonathan Leffler
Code may still need to use a larger value than 257.

char current_word[257];
...
// if (fscanf(src_file, "%s", current_word) == EOF){
// 256 is the max input string length, 1 less that array size
if (fscanf(src_file, "%256s", current_word) == EOF) {

Below are additional recommended changes:

    // Only one ; needed
    // FILE *src_file = fopen(argv[1], "r");;
    FILE *src_file = fopen(argv[1], "r");

    // Consider this style: IMO less error prone and easier to maintain
    // hashtable* tweet_table = malloc(sizeof(hashtable));
    hashtable* tweet_table = malloc(sizeof *tweet_table);

    // Simplify
    //int looper = 1;
    //while (looper == 1){
    //    if (fscanf(src_file, "%s", current_word) == EOF){
    //     looper = 0;
    while (fscanf(src_file, "%256s", current_word) == 1) {

    // calculate length once, use size_t (although with limits like 257, int will do)
    // int i;
    // for (i = 1; i < strlen(current_word); i+=1){
    size_t len = strlen(current_word);
    size_t i;
    for (i = 1; i < len; i+=1) {

Could add a test if (len == 256), then maybe your buffer size is too small. Should you want to write code with a dynamic buffer size, some more work is needed. Check if your system has getline().

Upvotes: 1

Related Questions