Thread: FGETS from text to array

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    41

    FGETS from text to array

    Hey guys,

    I need to access a text file and store the data into a 3 dimensional array.

    I'm having issues getting the info into an array, from there I can manipulate the text as needed. But the first step is stumping me.

    Code:
    #include <stdio.h>
    	int main(void)
    	{
            int count = 0;
            char text[50][50][21];
            
        		FILE *ptr_file;
        		char buf[1000];
    
        		ptr_file =fopen("proj4.txt","r");
        		if (!ptr_file)
            		return 1;
    
        		while (fgets(buf,1000, ptr_file)!=NULL){
            		text[count] = fgets(buf,1000, ptr_file);
            		count++;
                }
    		fclose(ptr_file);
    		getch();
        		return 0;
        		
        		
    	}

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look up the strcpy() function ...

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    A couple of things:

    1. How is the data stored in your file and what is the framework behind the need for a 3d array. Usually if you are just reading in lines of text an array would be 2d, e.g. [NUMLINES][LINESOFTEXT]. If the data is structured in a certain way in your file you could use a fgets-sscanf combination, or fscanf.

    2. You actually wind up reading in two lines from your file before you try to save it(in red). In addition I am not sure what you are trying to do with your array(in green), did this code compile?
    Code:
    while (fgets(buf,1000, ptr_file)!=NULL){
            text[count]=fgets(buf,1000, ptr_file);
            count++;
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    41
    I'm actually goign with a different approach:

    Code:
    void get_words(){
        int ch, i = 0, j = 0, k = 0;
        char ch_holder;
        
        while ((ch = getc(fp)) != EOF) {
          
              ch_holder = ch;
              if(!isalpha(ch)){
                  if(iscntrl(ch)) {
                        j++;
                                 
                  }
                  if(isspace(ch)) {  
                         j++;                  
                         k = 0;
                      }
                  if(isdigit(ch)){
                         j++;
                         k = 0;
                      }    
                  if(ispunct(ch)){ 
                         k++;
                            
                  }   
              }      
              if(isalpha(ch) && !isspace(ch) && !ispunct(ch) && !iscntrl(ch)){
                  ch_holder = tolower(ch);                   
                  textArray[i][j][k] = ch_holder; 
                  if(textArray[i][j][k] == '\0'){ k--; } 
                  k++; 
    
              }
        }
    }
    but still getting some weird errors in my array, can't seem to figure out how to avoid it storing certain characters in an array. Still going through the logic and understanding how to approach this. ... tips would be greatly appreciated. As the output is looking like this:
    1: information => 11
    2: technology => 10
    3: => 0 <-- this part here is what i'm trying to avoid and just store regular characters instead of these null spaces.
    4: => 0
    5: => 0
    ....

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't actually need ch_holder. Just use ch. Also:
    Code:
              if(isalpha(ch) && !isspace(ch) && !ispunct(ch) && !iscntrl(ch)){
    If it is alpha, then obviously it is not punct, space, cntrl. Did you mean: NOT isalpha?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    41
    no it doesn't really do anything, had that in there before i added the other code. I wanted it to store the alpha's only. Thanks for the catch. but it doesn't resolve my issue.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    "weird errors" doesn't tell us anything. What is textArray defned as, and what do you think you are doing here:
    Code:
    textArray[i][j][k] = ch_holder;
    Input string: "::11aa"
    Code:
    ispunct so k = 1
        you didn't actually do anything with k = 0
    second if does nothing, because it is punct
    ispunct so k = 2
        you didn't actually do anything with k = 1
    same as before, but with k = 2 and k = 3 for the numbers
    isalpha = true, so skip first iff
        textarray[ 0 ][ 0 ][ 3 ] = ch
        k = 4
    isalpha = true, so skip first iff
        textarray[ 0 ][ 0 ][ 4 ] = ch
        k = 5
    When in doubt, walk through it on paper.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    41
    Yeah i've been trying. Will give it another go around you cleared a few things up! thanks quzah!

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    41
    Code:
    void get_words(){
        int ch, i = 0, j = 0, k = 0, q = 0;
    
        
        while ((ch = getc(fp)) != EOF) {
          
              
              if(isspace(ch) || iscntrl(ch) || isdigit(ch) || ispunct(ch)){
                  q = 1;              
              }
              if(isalpha(ch)){
                  if(q == 1) { j++; k=0;}
                  ch = tolower(ch);                   
                  textArray[i][j][k] = ch;
                  k++; 
                  q = 0;
    
              }
        }
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You still aren't doing anything with most of your array's cells, which leads me to question why you are using a 3D array for this. If your file has "Hello World!" in it, and nothing else, how do you expect your 3D array to look at the end?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    41
    The way he broke it down was as follows:

    [i] = lines
    [j] = words
    [k] = characters

    but for the project i don't see the necessity in that. Do you?

    We are just storing the text into arrays, sorting through it doing different things, like finding unique words => put those into a different array, and calculate the mean, min, max of the words from the text file.
    Last edited by Oonej; 07-08-2011 at 05:18 PM.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    [ line 0 ][ word 0 ][ characters of "Hello" ]
    [ line 0 ][ word 1 ][ characters of "World" ]

    So what about spaces, and punctuation, where do those go? Where would the ' ' and the '!' go?

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    41
    We are simply doing a word counter, ignoring all punctuation and digits

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Oonej View Post

    [i] = lines
    [j] = words
    [k] = characters

    but for the project i don't see the necessity in that. Do you?
    Ahhh! I must admit I've been looking at your code for about 10 minutes trying to figure out what on earth you want a 3 dimensional array for.

    I don't see the benefit. It sounds like you just want an array of words (so a 2D array). Then you can just loop through them figuring stuff out. Much easier to understand, too.

    The fact that you've been asked to use a 3d array implies that next you might be asked to figure out the same stats per-line. So then you have a set of arrays of words... meh. It's a bit wasteful.

    Anyway, to your code:
    What is q meant to represent?

    I think you're trying to do too much with too little code.
    You have 3 dimensions: line, word, character.
    A newline means increment i.
    A new word (a space) means increment j
    Punctuation means continue on without doing anything
    Alpha characters need to be stored, and k incremented.

    So I think you need at least that many "if" statements in your loop.

    You should also mark the end of each word with a '\0' terminator if you want to use any string functions or printing functions. Otherwise the functions will access the garbage beyond the end of the word.
    And I guess you'll need to mark the end of line somehow too, maybe just a 0 value.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Will you be able to do all the statistics at once? If yes, then there isn't really a reason to use 3D, since you won't need to remember all this:
    [ line 0 ][ word 0 ][ characters of "Hello" ]
    [ line 0 ][ word 1 ][ characters of "World" ]
    That would only be helpful if for some reason the user got to control when certain statistics where calculated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fgets() & Fputs() to combine text files?
    By d2rover in forum C Programming
    Replies: 5
    Last Post: 11-20-2010, 03:58 PM
  2. need help with char array and fgets
    By fr0zen in forum C Programming
    Replies: 25
    Last Post: 12-07-2007, 04:46 AM
  3. Replies: 3
    Last Post: 10-15-2005, 09:13 AM
  4. fgets array size
    By stautze in forum C Programming
    Replies: 9
    Last Post: 04-29-2002, 01:25 PM
  5. help chack for array and fgets
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2002, 08:14 AM