Random Sentence From A File

This is a discussion on Random Sentence From A File within the C Programming forums, part of the General Programming Boards category; Random Sentence From A File -------------------------------------------------------------------------------- Hi; I wanna write a program which will show a random sentence from a ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    1

    Random Sentence From A File

    Random Sentence From A File

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

    Hi;
    I wanna write a program which will show a random sentence from a specific file for each execution. Sentence will separated by "\n".

    I wrote a code but it isn't work. is there any suggestion.

    Please help me...

    My Code:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    #include <math.h>
    
    
    void main(void)
    {
    char **line,c;
    int i=0,j=0,*slen,a,k;
    FILE *myfile;
    
    myfile = fopen("mycookies.txt","r"); 
    if (myfile == NULL)
    {
    printg("Warning!Cannot open file.");
    exit -1;
    }
    
    
    while ( !feof(myfile) )
    {
    fscanf(myfile,"%c",c);
    while(c!='\n')
    {
    line[i][j]=c;
    fscanf(myfile,"%c",&c);
    j++; 
    }
    slen[i]=j;
    i++
    
    }
    
    a=randomize(i);
    for(k=0;k<slen[i];k++)
    printf("%c",line[a][k]);
    
    getch();
    return 0; 
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > void main(void)
    main always returns an int.

    > char **line
    You don't allocate any space with this.
    char line[100][80];
    would allow you to store upto 100 lines, with up to 80 chars in each line

    > exit -1;
    If you really want to call the function, then you need
    exit ( -1 );

    > while ( !feof(myfile) )
    Why using feof() in a loop is a bad idea is also in the FAQ
    For example, if the last line is missing a newline, then your while(c!='\n') will loop forever, trashing memory as it goes.
    Furthermore, you don't reset your counter back to 0 when you go from one line to the next, nor do you end each line with a \0 to make it a proper string.

    > slen[i]=j;
    More unallocated memory.

    Seriously, consider using fgets() to read a whole line at a time rather than trying to do it yourself one character at a time.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Here are a few things I noticed:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    #include <math.h>

    void main(void)
    >This line won't even pass on my compiler -(. Probably should use int main(void)

    {
    char **line,c;
    int i=0,j=0,*slen,a,k;
    FILE *myfile;

    myfile = fopen("mycookies.txt","r");
    if (myfile == NULL)
    {
    printg("Warning!Cannot open file.");
    >I think you meant printf("Warning!Cannot open file.");

    exit -1;
    >Missing ()
    }


    while ( !feof(myfile) )
    >You opened the file, but you aren't reading anything in. Change this line to something like

    while(fread(....) !=0)

    {
    fscanf(myfile,"%c",c);
    while(c!='\n')
    {
    line[i][j]=c;
    fscanf(myfile,"%c",&c);
    j++;



    }
    slen[i]=j;
    i++

    }

    a=randomize(i);
    for(k=0;k<slen[i];k++)
    printf("%c",line[a][k]);

    getch();
    return 0;

    In
    }

  4. #4
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    You should probably avoid pointers for anything except little test programs until you know how they work. The problem can be easily solved with an array to hold one line and two passes through the file:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ( void )
    {
      FILE *fp = fopen ( "mycookies.txt", "r" );
    
      if ( fp != NULL ) {
        char line[BUFSIZ];
        int nlines = 0, r;
    
        srand ( (unsigned)time ( NULL ) );
    
        /* Count the lines in file */
        while ( fgets ( line, sizeof line, fp ) != NULL )
          ++nlines;
    
        /* Restart file & get random line # */
        rewind ( fp );
        r = rand() % nlines;
    
        /* Find line #r */
        while ( fgets ( line, sizeof line, fp ) != NULL ) {
          if ( r-- <= 0 )
            break;
        }
    
        printf ( "Random line:\n%s", line );
      }
    
      return 0;
    }
    As a few people already said, you have a bunch of syntax errors in your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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