Thread: problem with scaning words from text file into an array

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    2

    problem with scaning words from text file into an array

    im trying to scan words from a file into an array of strings.
    i dont understand why the compiler giving me this warning:

    t2.c: In function ‘get_words’:
    t2.c:12:27: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
    while(fscanf(stream,"%s",neword[j])!=EOF);
    ~^ ~~~~~~~~~
    t2.c:14:22: warning: comparison between pointer and integer
    if(neword[j] == ":" || neword[j] == "\n")
    ^~
    t2.c:14:42: warning: comparison between pointer and integer
    if(neword[j] == ":" || neword[j] == "\n")
    ^~
    t2.c:17:21: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
    words[i]=*neword;
    ^
    t2.c: In function ‘main’:
    t2.c:42:5: warning: attempt to free a non-heap object ‘d_words’ [-Wfree-nonheap-object]
    free(d_words);


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define N 100
    #define WORD_L 20
    
    void get_words(FILE *stream, char *words[])
    {
        int i=0, j=0;
        char neword[WORD_L];
        while(fscanf(stream,"%s",neword[j])!=EOF);
        {
            if(neword[j] == ":" || neword[j] == "\n")
            {
                words[i]=(char*)malloc((j+1)*sizeof(char));
                words[i]=*neword;
                i++;
                printf("%s", words[i]);
            }
            printf("%s\n", neword);
            j++;
        }
    }
    
    
    int main()
    {
        FILE *story, *dictionary, *new_story;
        char *d_words[N], *s_words[N];
        char *p_story, *p_diction, *p_rewrite;
        
        story=fopen("story.txt","r");
        dictionary=fopen("dictionary.txt","r");
        new_story=fopen("new_story.txt","w");
        if(story==NULL || dictionary==NULL || new_story==NULL)
        {
            printf("can't open input file!\n");
            exit(1);
        }
        get_words(dictionary, d_words);
        free(d_words);
        return 0;
    
    }
    a little background ... i need to replace some incorrect words from a story.. i have dictionary for it..
    please help..
    thank you
    Last edited by shovaldaniel; 12-29-2018 at 11:20 AM.

  2. #2
    Registered User
    Join Date
    May 2016
    Posts
    104
    1. neword[j] references character j in the array. The function expects an array of characters, that would be plain newords, not newords[j].
    2. fscanf will read the whole file in one go, and store it where you specify, in this case newords. That means you need to make sure that a) newords is sufficiently large to store the whole file, and b) you only need to call the function once.
    3. The comma at the end of line 12 is misplaced.
    4. There's a difference between ":" and ':'. One is a string literal the other is a character literal. You can not compare string literals with comparison operators.
    5. You need to learn how to use arrays and pointers correctly before trying to mess around with memory allocations or doing text operations in general. An introductory material covering arrays and pointers should allow you to do this. It is OK to help you with any sticky points you may have but we can't teach you the basics.

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    2
    Quote Originally Posted by Dren View Post
    1. neword[j] references character j in the array. The function expects an array of characters, that would be plain newords, not newords[j].
    2. fscanf will read the whole file in one go, and store it where you specify, in this case newords. That means you need to make sure that a) newords is sufficiently large to store the whole file, and b) you only need to call the function once.
    3. The comma at the end of line 12 is misplaced.
    4. There's a difference between ":" and ':'. One is a string literal the other is a character literal. You can not compare string literals with comparison operators.
    5. You need to learn how to use arrays and pointers correctly before trying to mess around with memory allocations or doing text operations in general. An introductory material covering arrays and pointers should allow you to do this. It is OK to help you with any sticky points you may have but we can't teach you the basics.
    thank you
    i dont want you to teach me the basics... thats exactly why im doing this code if you didnt notice..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what's your question?

    Dren seemed to make a pretty good job of explaining your error messages.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Organize words from a text file
    By doia in forum C Programming
    Replies: 8
    Last Post: 06-20-2010, 10:09 PM
  2. Storing Words from a text file
    By matt_570 in forum C++ Programming
    Replies: 18
    Last Post: 12-10-2008, 12:35 PM
  3. outputting words in array of text
    By hopeolicious in forum C++ Programming
    Replies: 3
    Last Post: 03-17-2005, 12:28 PM
  4. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  5. counting words in a text file...
    By flightsimdude in forum C Programming
    Replies: 10
    Last Post: 09-19-2003, 07:02 PM

Tags for this Thread