Thread: Reading input from a file. Program crashes

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    69

    Reading input from a file. Program crashes

    Okay, so I'll start off by telling you what I am trying to do.
    I have a text file that has words on each line. E.g.
    Code:
    this
    is
    a
    test
    forMyProgram
    I want my program to scan through the file and store each word on its own line in my variable 'fullSub'. I want to then be able to use that variable as a string. Here's what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        FILE *fp;
        static const char filename[] = "subdomains.txt";
        char *subs;
        char *fullSub = malloc(sizeof(char));
    
        fp = fopen(filename, "r");
    
        if (fp != NULL)
        {
    
            while((subs = fgetc(fp)) != EOF && subs != '\n' && subs != ' ')
            {
                realloc(fullSub, fullSub+1);
                strcat(fullSub, subs);
            }
    
        }else{
            fprintf(stderr, "Failed to open file '%s'!\n", filename);
            return 1;
        }
    
    
    free(fullSub);
    
        system("pause");// just here for testing purposes... I will use getchar() at a later date.
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's no wonder it crashes, with so many problems.
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:17:21: warning: assignment makes pointer from integer without a cast [enabled by default]
    foo.c:17:34: warning: comparison between pointer and integer [enabled by default]
    foo.c:17:49: warning: comparison between pointer and integer [enabled by default]
    foo.c:17:65: warning: comparison between pointer and integer [enabled by default]
    foo.c:19:13: warning: passing argument 2 of ‘realloc’ makes integer from pointer without a cast [enabled by default]
    /usr/include/stdlib.h:485:14: note: expected ‘size_t’ but argument is of type ‘char *’
    foo.c:5:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
    foo.c:5:26: warning: unused parameter ‘argv’ [-Wunused-parameter]
    foo.c:19:20: warning: ignoring return value of ‘realloc’, declared with attribute warn_unused_result [-Wunused-result]
    Think about what kind of 'array' of strings you need.
    You can have one of 4 choices.
    Code:
    char strings[10][100];  // fixed number of strings, fixed maximum length
    char *strings[10]; // fixed number of strings, variable length of each
    char (*strings)[100]; // variable number of strings, fixed maximum length
    char **strings;  // variable number of strings, with variable length
    I suggest you start with the first one, to allow you to focus on your 'read a single word' from the file to begin with.
    When that works, you can move onto how to allocate and store the string in your chosen array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yet another example (you're not the first today) of someone hacking together code without any real thought, and then wondering why it doesn't work.

    The program is not crashing. Your compiler would have complained bitterly (plenty of error messages) and refused to compile your code.

    It would help if you had bothered to read the documentation for all the functions you're using. After reading the documentation, it would then help if you used the function in a manner that was even slightly consistent with the documentation.

    fgetc() does not return a pointer to char. strcat() works with nul terminated strings, not pointers to single characters. realloc(x,newlen) does not change x (it returns a value if it succeeds).

    A pointer to character (eg subs) cannot be compared with a single character ('\n').
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-20-2011, 04:22 AM
  2. Program not reading file input :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2009, 02:55 PM
  3. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  4. program crashes when copying binary file
    By bored_guy in forum C Programming
    Replies: 9
    Last Post: 11-21-2008, 03:02 AM
  5. Program crashes on reading a line from a file
    By bluescreen in forum C Programming
    Replies: 5
    Last Post: 10-19-2006, 05:23 AM