Getting words from a file

This is a discussion on Getting words from a file within the C Programming forums, part of the General Programming Boards category; Im trying to make a program that reads the words in a file,and stores each word in a position of ...

  1. #1
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82

    Getting words from a file

    Im trying to make a program that reads the words in a file,and stores each word in a position of an array. Something similar to this: i have word[2],"Hello World", Hello goes into word[1] and World into word[2]. Can i do this with fscanf?How?I tried but the only thing i get is a char in every position of the array.HELP!

  2. #2
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Well,tha i knew how to do it.But i cant have a limit for the words or char.So,i must have something like this **text. An then store every word in *text[].Something like argv.Understand?

  3. #3
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    oh,and i have to work with a file,so there are 2 things i need to know.First,how to declare the array where the words will be stored.Second, how to write the words from the file in the array. And i have to use malloc,since theres no limit for the number of words,lines,or what so ever.My thought is making a word_counting function and then,before writing the words in the array text,do:
    text = (char *)malloc(word_counting(fp)),where fp is the file pointer.
    The rest of my project is working with this array,and its quite easy.

  4. #4
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Can u be more specific?How can i use that in this specific problem?

  5. #5
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Did i mentioned there are no limits to the number of words,chars,lines?your program uses limits,mine cant!Dead easy to understand,dont u think?

  6. #6
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Well,ok,im quite a begginer,so i cant understand how ill write words in the array.Suppose i have **text and ive read a word,i should do:
    *text[i] = word?,text=word,*(text + i)=word,how do i do this???

  7. #7
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    And how do i associate the word with the file?
    can i do text[i]=fscanf("%s", word)

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    There's a tutorial on this web site that shows you how to open a file and read one word from it (just use a loop to get the rest)... maybe that would help?

  9. #9
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82

    Smile

    Ok,thnks,but can u give the link?i searched the site and found only c++ tutorials,it must be on the board or something...

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Does this help:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      FILE *fp;
      char word[BUFSIZ];
        
      if ((fp = fopen("junk1.c", "r")) == NULL)
      {
        perror ("junk1.c");
        return 0;
      }
      
      while (fscanf(fp, "%s", word) == 1) /* Buffer overflow warning! */
      {
        puts(word);
      }
      
      fclose(fp);
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Whats wrong with this code?
    [CODE]#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define FORA 0
    #define DENTRO 1
    int conta_palavras(FILE *fp)
    {
    int np,estado,c;
    estado=FORA;
    np=0;
    while((c=fgetc(fp))!= EOF)
    {
    if(c== ' ' || c == '\n' || c == '\t')
    estado = FORA;
    else
    if(estado == FORA)
    {
    estado = DENTRO;
    ++np;
    }
    }
    return np;
    }
    main(int argc, char *argv[])
    {
    FILE *fp;
    char **texto=NULL;
    int i=0,n=0;
    char palavra[56];
    fp = fopen(argv[1],"r");
    if(argc != 2)
    printf("Numero de argumentos invalidos\n");
    else
    {
    if(fp == NULL)
    printf("Impossivel abrir o ficheiro\n");
    else
    {
    n=conta_palavras(fp);
    texto=malloc(n);
    while (fscanf(fp, "%s", palavra) == 1)
    {
    puts(palavra);
    }

    }
    }
    }

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Whats wrong with this code?
    If you read all the characters in the file using conta_palavras, you would need to rewind(fp) afterwards to start at the beginning of the file again.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Thnks,already notice that,im trying to use an auxiliary pointer to solve the problem.I also made a few changes to the rest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21