Thread: Need Help Inputing from a data file

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    Unhappy Need Help Inputing from a data file

    Hello my fellow programmers -- I am need of your help yet again.

    I am making a hangman game for my final C project but I cannot figure out the syntax for reading from a data file.

    this is the scenario... I have a data file called
    "dictionar.lib" that has a list of words in it.

    I want to first check to see if the file exists.. which I did fine..

    Now comes the hard part. I want to chose a word from this list using a randomly generated number and return that number to an array variable as the word to guess for the game.

    So far my TA's have told me that I have to input the entire file into an array and to test for the end of a word -- which from the data file i suppose would be a 'return' or '/n' .. each time the function finds a carrage return it would determine all the previous letters to be part of an entire word.

    So far this is my code: the pr(); pe(); and ps() are formatting functions so just disregard them. see if you can figure out the problem.

    int library(void){

    char filib[STRINGSIZE];
    char word[NUMOFWORDS][WORDSIZE];
    char getword[WORDSIZE];
    char letter, y;
    FILE *dict;
    int i=0,j=0, r;


    dict=fopen("dictionary.lib", "r");

    /* If unable to locate lilbrary do this */
    if (dict==NULL){
    pe();pr(20);
    printf("Read Error : Unable to locate Local Library.\nPlease make sure it is in the same directoy as the game file.");
    printf("Would you like to locate the missing file? (y/n) ");
    scanf("%c", &y);
    if(y=='y'){
    for(scanf("%s", filib);
    (dict=fopen(filib, "r"))==NULL;
    scanf("%s", filib))
    {
    pe();
    ps(5); printf("Cannot open %s for input\n", filib);
    ps(5); printf("Re-Enter Local Library name> ");
    i++;
    if(i==3){
    break;
    }
    }

    }
    pe(); pr(20); ps(5);
    printf("Load Error : Unable to load Local Library");
    }
    /* end null error */

    else if(dict!=NULL){
    pe(); pr(20); ps(5);
    printf("Local Library Detected. Loading File\n\n");
    for(i=0; !EOF; i++){
    for(j=0; j!='\n'; j++){
    fscanf(dict,"%c",&getword[j]);
    printf("%d",j);
    word[i][j]=getword[j];
    }
    }

    r=rand()+1;
    /* printf("%d", r);*/

    for(i=0; i<10; i++){
    printf("%s\n", word[i]);
    }

    fclose(dict);
    printf("\nLibrary Unloaded..\n\n");
    }


    }
    /**/
    /** end Load Library -----*/

    YOUR HELP IS WELCOME AND MUCH APPRECIATED!@

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you want to pick a string at random from the file, keep two arrays large enough to hold the longest string in the file. Read the first string and place it in the first array, then the second string into the second array and choose one at random. Then read the third string into the array of the string that was not chosen and choose between those two at random.

    Do this all of the way through the file and when you get to the end, you have a string that was chosen completely at random from the file. This is inefficient, but if the file is small you'll see no difference in speed.

    The advantage to this is that the code for choosing the random string will be very small.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    Question ??

    Thanks Prelude-- Well my main problem is that I cannot get the input from the data file. I am able to locate the file but the data is not being inputed or is not being done properly. The syntax is a bit confusing to me.

    Any ideas??

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    use a while loop and a pointer to get your file like so
    Code:
    while((ch = fgetc(dict) != EOF)  /* ch should be int as EOF is an integer */
        *getword_ptr++ = ch;
    This will put your whole file in the array pointed to by getword_ptr so make sure getword is large enough.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    Thumbs up Thanks!

    Gracias my friend! However that is only part of the solution to my problem. Now what I need to do is find one word in the entire string. How do I test for the "\n" character?

    --------------------------------------------------

    while((ch=fgetc(dict) !=EOF){
    *getword_ptr++= ch;

    /* getword[i][j] is an array of words */
    /* i=20 is the max number of words */

    for(i=0; i<20; i++){

    /* j=10 is max number of characters per word */

    for(j=0; j<10; j++){

    /* getword is an array of words */
    ch=getword[i][j];

    if(ch=="\n"){
    j=10;
    }
    }
    }
    }

    --------------------------------------------------
    what do you think about that?

    Thank you for all of your help!

    SUJR21!

  6. #6
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    You can use the idea of the while loop
    Code:
    while(*getword_ptr != '\n')
         *word_ptr++ = *getword_ptr++;
    This will put characters into word until a newline is found
    Then you can process the word.

    word and getword should be declared as
    Code:
    char getword[TOTWORDS], *getword_ptr;  /* TOTWORDS should be the max characters in your file */
    char word[MAXWORD], *word_ptr;  /* MAXWORD should be the max characters in a single word */
    
    getword_ptr = getword;
    word_ptr = word;
    One more thing you need to reset pointers back to the begining of the arrays before using them agian.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM
  3. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  4. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM