Thread: Reading from text file to array help please!

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Reading from text file to array help please!

    Code:
    char getData(FILE* fp)
    {
    int i, j;
    char *ptemp;
    char **plist;
    char *list[MAXFILESIZE];
    char temp[100];
    
    
    //char *wordCounter[MAXFILESIZE];;
            *list = (char *)malloc(MAXFILESIZE * sizeof(char));
            if(*list == NULL)
            {
            printf("NOT ENOUGH MEMORY!\n");
            exit(100);
            }
    
    
    for(i = 0; i < MAXFILESIZE; i++)
    {
    while(fscanf(fp, "%[^ ' ' ^ \n]", temp[i++])!=EOF)
    {
            //wordCounter++;
            if(ispunct(temp[strlen(temp) - 1 ]))
            {
                    (temp[strlen(temp) - 1]) = '\0';
            }
            {
                    for(i = 0; i < strlen(temp); i++)
            {
                            temp[i] = tolower(temp[i]);
            }
            printf("temp:%s\n", temp);
            }
            }
    }
    -I've been able to get my program to work for just the first word in the file. But I want to create a loop for all of the words until end of file. But now I'm getting a segmentation fault. PLEASE HELP! I don't know how to get my program to work for all of them...
    -Also, I have to use fscanf, its for an assignment. But yes, I realize that there are other functions that would make this easier.
    Thank you in advance.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Code:
    while(fscanf(fp, "%[^ ' ' ^ \n]", temp[i++])!=EOF)
    you aren't giving it a pointer to a location in 'temp'. you are giving it the value. try &temp[i++]; and you will probably overrun 'temp' if the sum of word lengths is >= 100.

    edit: your incrementing of 'i' is out of control. you increment it in several places. you need to look at what the value of 'i' will be at the various locations where it is used.
    Last edited by dmh2000; 05-24-2012 at 09:39 AM.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by dmh2000 View Post
    Code:
    while(fscanf(fp, "%[^ ' ' ^ \n]", temp[i++])!=EOF)
    you aren't giving it a pointer to a location in 'temp'. you are giving it the value. try &temp[i++]; and you will probably overrun 'temp' if the sum of word lengths is >= 100.

    edit: your incrementing of 'i' is out of control. you increment it in several places. you need to look at what the value of 'i' will be at the various locations where it is used.
    Code:
    char getData(FILE* fp)
    {
    int i, j;
    char *ptemp;
    char **plist;
    char *list[MAXFILESIZE];
    char temp[100];
    //char *wordCounter[MAXFILESIZE];
    
    
            *list = (char *)malloc(MAXFILESIZE * sizeof(char));
            if(*list == NULL)
            {
            printf("NOT ENOUGH MEMORY!\n");
            exit(100);
            }
    
    
    while(fscanf(fp, "%[^ ' ' ^ \n]", temp))
    {
            //wordCounter++;
            if(ispunct(temp[strlen(temp) - 1 ]))
            {
                    (temp[strlen(temp) - 1]) = '\0';
            }
            {
                    for(i = 0; i < strlen(temp); i++)
            {
                            temp[i] = tolower(temp[i]);
    
    
    
    
            }
            printf("temp:%s\n", temp);
            }
    }
    Sorry, that first code still had remainders of when I was trying to experiment with getting it to loop across the file properly. This one doesn't. Like I said before, it works great for the first work of the file. Makes everything lowercase, gets rid of the punctuation at the end, etc. But I don't know how to get it to do that for every single word. And where to store those words either. Please help me out! This has become ridiculous because I feel like I've tried a hundred different ways and nothing works. Also, we have to use fscanf, so please don't suggest other functions. I know they exist.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by hotshotennis View Post
    Code:
    while(fscanf(fp, "%[^ ' ' ^ \n]", temp))
    Also, we have to use fscanf, so please don't suggest other functions. I know they exist.
    If you have to use fscanf I think you should reread the man page because your format string doesn't do what you want it to do.
    Hint: carefully read the the part about "%[" and "%s" .

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    if your loop is quitting after the first word, then it is because fscanf is returning 0, which means it couldn't find a match to your format specification. so do what Andreas said.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Code:
    while(fscanf(fp, "%s[^ ' ' ^ \n]", temp)!=EOF)
    -Ok so if I'm not sure if this is that you guys meant. But I noticed in the man page that they use the %s. I thought our teacher told us that we didn't need to use it in this particular case. Anyway, I seemed to get it to work! But now the problem is, I don't know what to do next... haha. I think I need to use some pointers and I need to move the words to my main list array. Also, I need to use counter and keep a running tally on how many words I have(having no duplicate words, just the amount of them)
    Output Example:

    13 words
    Maximum length: 7
    ======= =====
    Word count
    ======= =====
    a 1
    boat 1
    but 1
    down 1
    dream 1
    gently 1
    is 1
    life 1
    merrily 4
    row 3
    stream 1
    the 1
    your 1
    -If someone would at least get me started in the right direction of how to achieve this, I would appreciate it!

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by hotshotennis View Post
    Code:
    while(fscanf(fp, "%s[^ ' ' ^ \n]", temp)!=EOF)
    -Ok so if I'm not sure if this is that you guys meant. But I noticed in the man page that they use the %s. I thought our teacher told us that we didn't need to use it in this particular case. Anyway, I seemed to get it to work!
    Ok, it seems I've set you on he wrong track :-(.
    For a start, "%s" and "%[" are two distinct conversion specifiers and if you would like to combine them you would need to use it like "%s%[x]". But then you would also need a second pointer argument which will save the result from the second conversion specification (%s would be the first, %[x] would be the second).
    In your specification ("%s[^ ' ' ^ \n]") you match all characters up to a whitespace followed by the literal sequence "[^ ' ' ^ \n]". But since the first whitespace fscanf() encounters stays in the input stream, it's impossible to match the literal sequence because you wrote it in a way that it should follow immediately after the string (without the whitespace). Therefore fscanf() stopps the scanning at the first whitespace and disregards any other directive in the conversion specification -> you get the same result as if just use
    Code:
    while(fscanf(fp, "%s", temp)!=EOF)
    and that's why your program is working as it should.

    But you have also misunderstood the behaviour of the "[%" specifier, because you can't use something like ' ' within the brackets to match a space character. Inside the brackets you have to use the characters as they are: %[^ ' ' ^ \n] would match all characters except a space, a single quote character ('), a circumflex (^) and a newline (\n, you can use escape sequences within the brackets).

    But now the problem is, I don't know what to do next... haha. I think I need to use some pointers and I need to move the words to my main list array. Also, I need to use counter and keep a running tally on how many words I have(having no duplicate words, just the amount of them)
    What's your skill level? Do you understand structures?

    Bye, Andreas

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Oh ok. I dont know, our teacher told us that we could use the brackets and escape character for new lines, so I figured that maybe I could implement it for a space to. So what youre saying is that all I need is the %s?
    As for skill level, in your eyes probably a newbie, in college courses, intermediate. I'm taking a class and this is a homework assignment and my learning process. So sorry if I make any stupid errors. Anyway, I'm just confused on how to allocate everything dynamically and what to put in which array and how to access it.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by hotshotennis View Post
    So what youre saying is that all I need is the %s?
    If you just regard everything between two whitespaces as a word, than %s is all you need.

    As for skill level, in your eyes probably a newbie, in college courses, intermediate. I'm taking a class and this is a homework assignment and my learning process. So sorry if I make any stupid errors. Anyway, I'm just confused on how to allocate everything dynamically and what to put in which array and how to access it.
    I'm new to C myself (although I have some experience in other programming languages).
    I've just asked because depending on your level there are different possibilities to solve your problem. Do you have any guidelines from your teacher? Usually you do exercises in context to the topic you currently learn.
    How would you solve this problem with paper and pencil?

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by AndiPersti View Post
    If you just regard everything between two whitespaces as a word, than %s is all you need.

    I am regarding everything between two whitespaces as a word. The only caveat is that it is multiple lines, and I need my fscanf to not stop on the first newline.

    I'm new to C myself (although I have some experience in other programming languages).
    I've just asked because depending on your level there are different possibilities to solve your problem. Do you have any guidelines from your teacher? Usually you do exercises in context to the topic you currently learn.
    How would you solve this problem with paper and pencil?

    Bye, Andreas
    I might take your paper and pencil suggestion and try to solve it out. That could help. As far as what the teacher said, we need to use strings and some pointers to solve this. So I'm guessing that for the counting part, I should use strcmp to see if I have duplicates of a word.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading ints from text file to an array
    By hotshotennis in forum C Programming
    Replies: 4
    Last Post: 04-23-2012, 12:33 PM
  2. Reading in text file to an array?
    By Le23ron in forum C Programming
    Replies: 15
    Last Post: 02-12-2012, 11:02 PM
  3. trouble reading a text file into an array
    By Ciaran in forum C Programming
    Replies: 15
    Last Post: 04-04-2011, 05:14 PM
  4. reading a string from a text file into a 2d array
    By duelord in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2009, 07:29 AM
  5. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 03:17 PM