Thread: stdin !

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    25

    stdin !

    Hi,
    I'm reading from a text file in my code(It's only 10 different words in that). The code should random a word from the text. how do I do that?
    Can I somehow turn the text into an array of strings?so that I can random the index of the array ? Or is this impossible ?!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you're reading from a file, what does that have to do with stdin?

    Google rand() and srand().

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can also check out strtok().

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    Sorry for the wrong title, I can not change it now ! I meant exactly the opposite!
    anyway...
    I know how to use random() or srandom(). My problem is , how to turn the words in the file to strings?
    I've been searching about it in the internet but I didn't get an answer that I could understand. It's explained in a high-level-programming language I guess!
    I tried this but ain't working.

    Code:
    int main(){
    FILE* sep;
     int ch,i;
     char flt[8];
    sep=fopen("hangman.dat","r");
     for(i=0;i<8;i++){
       ch=getc(sep);
       if(ch=='\n')
           flt[i]=ch;
     }
     fclose(sep);
     return 0;
    }
    I'll really appreciate it if someone gives me some hints on it.
    Last edited by sifeet; 12-12-2007 at 11:33 AM.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Google for fgets().

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    ok, I'm using fgets now in the code, and I try to save each line (only one word in each line) as an element in the array "flt" . but all elements of the array become the last line/word! I mean they all have one and the same string i.e. the last word in the file.
    flt[0]=organisation
    flt[1]=organisation
    .
    .
    .
    flt[7]=organisation !!!!
    Code:
    int main(){
      char ch[20];
      int i,j;
      char* flt[8];
    FILE *sep; 
    sep=fopen("hangman.dat","r");
     for(i=0;i<2;i++){
       fgets(ch,20,sep);
       flt[i]=ch;
      
     }
     for(j=0;j<2;j++){
       printf("%s",flt[j]);
     }
    
     fclose(sep);
    
     
     return 0;
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you are just storing the address of ch in your flt array. As you read new data, the content of ch changes. You need to either:
    - Have a 2D array
    - Allocate memory for your strings as you read them in, and copy the contents of ch into the allocated memory.

    [If you print the VALUE of flt[0..7], e.g printf("%p", flt[i]), you will see that all strings have the same address, and thus are actually just the same string multiple times].
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    Quote Originally Posted by matsp View Post
    Yes, you are just storing the address of ch in your flt array. As you read new data, the content of ch changes. You need to either:
    - Have a 2D array
    - Allocate memory for your strings as you read them in, and copy the contents of ch into the allocated memory.

    [If you print the VALUE of flt[0..7], e.g printf("%p", flt[i]), you will see that all strings have the same address, and thus are actually just the same string multiple times].
    --
    Mats
    tnx for d reply, but we haven't reached that far i C yet. I mean we haven't learned abt 2D array or allocated memory.
    So i'm wondering if there is another easier way to do this.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sifeet View Post
    tnx for d reply, but we haven't reached that far i C yet. I mean we haven't learned abt 2D array or allocated memory.
    So i'm wondering if there is another easier way to do this.
    No, there isn't. You'll just have to learn about 2D arrays [that's the less complex option].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    Code:
    int main(){
      char ch[20];
      int i,j;
      char* flt[9][20];
      FILE *sep; 
      sep=fopen("hangman.dat","r");
      for(i=0;i<9;i++){
        for(j=0;j<20;j++){
          flt[i][j]= fgets(ch,20,sep);
        }
      }
     
    
      fclose(sep);
     
     
      return 0;
    }
    still not working :-S

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I wouldn't expect that to work...

    1. You have a 2D array of pointers to char, you need a 2D array of char.
    2. You could then read straight into your array with
    Code:
      for(i=0;i<9;i++){
          fgets(flt[i],20,sep);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    Quote Originally Posted by matsp View Post
    I wouldn't expect that to work...

    1. You have a 2D array of pointers to char, you need a 2D array of char.
    2. You could then read straight into your array with
    Code:
      for(i=0;i<9;i++){
          fgets(flt[i],20,sep);
    --
    Mats
    It's working now =D thanks a lot! =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking stdin for input
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 09:06 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  4. value of stdin (not zero?)
    By taisao in forum Linux Programming
    Replies: 3
    Last Post: 05-27-2007, 08:14 AM
  5. stdin question
    By oomen3 in forum C Programming
    Replies: 6
    Last Post: 02-19-2003, 02:52 PM