Connecting Tech Pros Worldwide Help | Site Map

Storing words in 2D array from text file

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 18 '09
The goal is to read the words from a text file and store them in 2D array. Pointers aren't allowed. I have been struggling with this for a while and help with would be much appreciated. This is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. FILE *fp;
  4. char readFile();
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     char ch, ar[50][10];
  9.     int word, letter,i=0,j;
  10.     if (argc != 2)
  11.         puts("ERROR");
  12.  
  13.     else{
  14.         fp = fopen(argv[1],"r");
  15.         if (fp == NULL){                
  16.             puts("cannot open file");
  17.             return 1;
  18.         }
  19.         else    
  20.             while(feof(fp) !=1){
  21.                 for (word=0;word<50;word++){
  22.                     for (letter=0;letter<10;letter++){
  23.                         ar[word][letter]=readFile(ar,fp);
  24.                     }
  25.                 }
  26.  
  27.             }
  28.             fclose(fp);
  29.         }
  30.  
  31.     }
  32.     return 0;
  33. }
  34. char readFile(char ar[][10], FILE*fp)
  35. {
  36.     char ch;
  37.     fscanf(fp,"%c",&ch);
  38.  
  39.     return(ch);
  40. }
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,165
#2: Oct 18 '09

re: Storing words in 2D array from text file


That's nice, what was your actual question?
Was this program not working?
Did you get unexpected output or behaviour?
What was the unexpected behaviour?
What was the expected behaviour?
What was the input data?
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 830
#3: Oct 18 '09

re: Storing words in 2D array from text file


Refer to Arrays Revealed for general information about 2D arrays.
Reply


Similar C / C++ bytes