1

I'm working on a project using C and for the project I must read in a text file and store each word into an array. I also have to remove the punctuation off the words, so I need to use a 2-Dimensional array in order to edit the words. I am having trouble figuring out how to get the words in the 2-D array it self. This is what I have done so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1001
#define LINES 81

int main(void) {
    int stringSize;
    int i =0;
    char *x[MAX][LINES];
    char str[MAX];
    char y[MAX];
    FILE *fp;

    fp = fopen("TwoCitiesStory.txt","r");
    if(fp == NULL) {
        printf("Cannot open file.\n");
        exit(1);
    }

    while(!feof(fp)) {
        for(i=0;i<MAX;i++){
            fscanf(fp,"%s",x[i][LINES]);
        }
    }

    return 0;
}
ProfOak
  • 541
  • 4
  • 10
  • Do you want to read a line from file... as %s will only read words – avinash pandey Apr 01 '15 at 04:02
  • For example if the text file contained "Hi my name is Bob". I need the array to hold x[0][]="Hi" x[1][]="my" and so on. The maximum amount of words in the text file is 1000 and the maximum length of a word is 80 characters. – beginner_coder Apr 01 '15 at 04:04
  • so what problem are you facing with this code – avinash pandey Apr 01 '15 at 04:05
  • `char x[MAX][LINES];`..`for(i=0;i – BLUEPIXY Apr 01 '15 at 04:08
  • You are using `feof` incorrectly: http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – William Pursell Apr 01 '15 at 04:09
  • "Cannot open file." is the canonical useless error message: `FILE * Fopen(const char *path, const char *mode) { FILE *fp = fopen(path, mode); if( fp == NULL) {perror(path); exit( EXIT_FAILURE);} return fp; }` – William Pursell Apr 01 '15 at 04:10
  • Why do you think you need arrays at all? What are you actually trying to do with the data? – William Pursell Apr 01 '15 at 04:13
  • After I store the data in the array, for example if the text file contained "Hi my name is Bob". I need the array to hold x[0][]="Hi" x[1][]="my" and so on. I will have a have another file with a list of words, and I will compare and see how many times the words in the list appear in the first file. – beginner_coder Apr 01 '15 at 04:28
  • Just a heads up, whether you read a `line` at a time, or a `word` at a time, your punctuation removal will be virtually identical. After having your text stored in either a 2-D array of chars or an array of pointers to string, you will create a temporary string (say `tmp`) long enough to hold the word/line and then step through the word/line copying each char that is not punctuation to `tmp`. When done, replace your word/line with `tmp` (null-terminate if `tmp` is a line). Then repeat for each word/line. – David C. Rankin Apr 01 '15 at 05:15

2 Answers2

1
  1. Read the whole line using fgets()
  2. Store the read line into the 2D array

The whole code looks like

char x[row][col];
char buf[300];
int i=0,j=0;
memset(x,0,sizeof(x));
while(fgets(buf,sizeof(buf),fp))
{
  size_t n = strlen(buf);
  if(n>0 && buf[n-1] == '\n')
  buf[n-1] = '\0';
  if(i>= row && n> col)
  break;
  strcpy(x[i],buf);
  i++;

}

Edits:

If you need each word separately in the array. buf is being used to read the whole line. strtok() is used to break the line into words with space as delimiter. Then store each word in each row.

size_t n;
while(fgets(buf,sizeof(buf),fp))
{
   char *p = strtok(buf," ");
   while( p != NULL)
   {
      n = strlen(p);
      if(i>= row && n> col)
      break;
      strcpy(x[i],p);
      i++;
      p = strtok(NULL," ");
   }
}

If you want to print out the array go for

int i;
for(i=0;i<row;i++)
printf("%s\n",x[i]);

Why feof() is wrong

Community
  • 1
  • 1
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • This doesn't separate each word in the array. For example if the text file contained "Hi my name is Bob". I need the array to hold x[0][]="Hi" x[1][]="my" and so on. The maximum amount of words in the text file is 1000 and the maximum length of a word is 80 characters. – beginner_coder Apr 01 '15 at 04:21
  • The code it self runs successfully, but I am a bit confused on how you reached this answer. Is row the max number of words and col the length of the words themselves? First you created char x[row][col]; which created an array of characters and then you created buf[300];. But what does buf do? If I have to print the words after, which array do I call? I tried printing like this: for(i=0;i – beginner_coder Apr 01 '15 at 04:42
  • @beginner_coder You can print out the 2D array as shown in the edits – Gopi Apr 01 '15 at 04:45
1

The following line

char *x[MAX][LINES];

declared a 2D array of pointers. What you need is just a 2D array of characters.

char x[MAX][LINES];

The code for reading the words can be simplified to:

while( i < MAX && fscanf(fp, "%80s", x[i]) == 1 )
{
   ++i;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270