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

This is a discussion on Home work getting stuff from a file and putting it into an array within the C Programming forums, part of the General Programming Boards category; I am writing a program where i need to open a file and store the data from the file into ...

  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
    20,976
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    331
    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.
    http://www.malcolmmclean.site11.com/www

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, 01:48 PM
  3. Home Work
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-24-2003, 08: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, 06:34 PM
  5. Putting stuff in my window...
    By Brian in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2002, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21