Thread: Home work getting stuff from a file and putting it into an array

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    Home work getting stuff from a file and putting it into an array

    I am writing a program where i need to open a file and store the data from the file into an array.

    Ex(what is in the file):

    por, because
    be, ser
    in, en

    now I keep getting these error


    • 5csce.c:21:5: warning: format â%sâ expects argument of type âchar *â, but argument 3 has type âchar (*)[2]â [-Wformat]
    • 5csce.c:21:5: warning: format â%sâ expects argument of type âchar *â, but argument 4 has type âchar (*)[2]â [-Wformat]
    • 5csce.c:27:1: warning: format â%sâ expects argument of type âchar *â, but argument 2 has type âintâ [-Wformat]

    what does it mean?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
    
    
        FILE * pFile;
    
    
        char words[100][2];
    
    
    //open file
    
    
        pFile = fopen("words.dat", "r");
    //Read date and store in an array
    
    
        fscanf(pFile, "%s, %s", words, words);
    
    
    // close fiel
    
    
        fclose(pFile);
    
    
    printf("%s\n", words[0][0], words [0][1]);
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It means that you are passing words as an argument to fscanf such that it matches %s, but words is an array of arrays of char that is converted to a pointer to an array of char, whereas %s matches a pointer to char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    You need a 3D array, 100 (line) by 2 (words per line), by the maximum word length, plus one for the nul.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. home work
    By manav in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-29-2008, 02:39 PM
  2. extracting data from file putting it into an array..
    By Cat00 in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2005, 02:48 PM
  3. Home Work
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-24-2003, 09:33 AM
  4. Putting strings into an array from a text file.
    By JLan in forum C Programming
    Replies: 5
    Last Post: 11-20-2003, 07:34 PM
  5. Putting stuff in my window...
    By Brian in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2002, 11:37 PM