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;
}
9
  • Do you want to read a line from file... as %s will only read words Commented Apr 1, 2015 at 4: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. Commented Apr 1, 2015 at 4:04
  • so what problem are you facing with this code Commented Apr 1, 2015 at 4:05
  • char x[MAX][LINES];..for(i=0;i<MAX;i++){ fscanf(fp,"%80s",x[i]); } Commented Apr 1, 2015 at 4:08
  • You are using feof incorrectly: stackoverflow.com/questions/5431941/… Commented Apr 1, 2015 at 4:09

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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.
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<row;i++) for(i=0;i<col;i++) printf("%s\n",x[row][col]);
@beginner_coder You can print out the 2D array as shown in the edits

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.