File handling related question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shashankraj1231
    New Member
    • May 2010
    • 13

    File handling related question

    I am trying to write a program in "c" language that accepts file as input and prints only those lines of the file, that contains at least one vowel.

    I have written the code so far as

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main()
     {
        FILE *fp;
        char ch[100];
        char chn[50];
        int chk=0;
        char *line;
        char *name;
        clrscr();
        printf("\n Enter the name of the file: ");
        scanf("%s", name);
        fp =fopen(name,"w");
        if(ferror(fp))
        {
        printf("\n Error opening file: ");
        getch();
        exit(1);
        }
    
        /* ENTERING TEXTS INTO THE FILE */
        /*------------------------------*/
    
        printf("\n Enter a few line of texts :");
        printf("\n Enter $ to exit :");
        while(strlen(gets(ch))>0)
        {
        if(ch=="$")
        break;
        else
        {
         fputs(ch, fp);
         fputs("\n",fp);
        }
       }
       fclose(fp);
    
       /*---* Now to look for vowels per line *----*/
       /*------------------------------------------*/
    
       fp= fopen(name,"r");
       while(!feof(fp))
       {
        while(chn!='\n')
        {
           if(chn='a'|chn='A'|chn='e'|chn='E'|chn='i'|chn='I'|chn='o'|chn='O'|chn='u'|chn='U')
           {
           /*stat[chk]++;*/
           chk++;
           line=
        }
    /*--------------- i am not sure what to do here ----*/
     fclose(fp);
     getch();
     }
    Please guide me as to how to proceed with the same as it an interesting but complicated question. A code snippet for the later part would be much appreciated.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need to read from your file into an array.

    Then I would write a function you can pass the array to and have that function look of a vowel. When it finds one it returns 1 for true and returns a zero if it does not find a vowel.

    Just call this function inside the loop that is reading from your file:

    Code:
    LOOP
       READ A RECORD
       CALL VOWEL FUNCTION
       IF RETURNS 1 PRINT FOUND A VOWEL
       IF RETURNS 0 PRINT NO VOWERL
    UNTIL END OF FILE

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Presumably this program doesn't work the way you want. It would help us if you told us what was wrong.

      I notice that line 15 uses scanf to get the name of the input file from the user and store it in the memory buffer pointed at by name. Unfortunately, name hasn't been initialized yet so disaster ensues.

      Comment

      • shashankraj1231
        New Member
        • May 2010
        • 13

        #4
        Originally posted by weaknessforcats
        You need to read from your file into an array.

        Then I would write a function you can pass the array to and have that function look of a vowel. When it finds one it returns 1 for true and returns a zero if it does not find a vowel.

        Just call this function inside the loop that is reading from your file:

        Code:
        LOOP
           READ A RECORD
           CALL VOWEL FUNCTION
           IF RETURNS 1 PRINT FOUND A VOWEL
           IF RETURNS 0 PRINT NO VOWERL
        UNTIL END OF FILE
        Actually, I have to Print the LINE that contains the vowel, not just find that vowel. I mean, every time, that file is opened, there would be multiple lines in that.
        Out of those line, We need to actually PRINT the entire line that contains at least a vowel.
        I not able to devise a strategy to print the lines to the output screen.
        Also, If I HAVE TO ARRANGE THE LINES according to their length and print it to the console, how would I do that.
        PLEASE HELP ME, As i am not able to find the answer in any text book or the Internet for the same...

        Comment

        • shashankraj1231
          New Member
          • May 2010
          • 13

          #5
          Originally posted by donbock
          Presumably this program doesn't work the way you want. It would help us if you told us what was wrong.

          I notice that line 15 uses scanf to get the name of the input file from the user and store it in the memory buffer pointed at by name. Unfortunately, name hasn't been initialized yet so disaster ensues.
          I am able to go past that and the program works fine to input data, store it to the file and output to the console.
          It is just the part where I have to PRINT the entire line that contains at least a vowel, confusing me.
          I have tried many strategies but they don't work.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You are reading a line into an array of char.

            The \n should be converted to a \0 in the array so you have a string.

            Just display the array to diaplsy the line.

            As to displaying by the length, I would
            a) create an array of unsigned int for the lengths of each line.
            b) create a corresponding array of char* for the lines. c) The line array[x] will correspond to length array[x]

            d)call strlen on your array and allocate memory for strlen+ 1 bytes. Then use strcpy and copy your array to the allocation.
            e) copy the address of the allocation to your line array[x]
            f) copy the strlen value for this allocation to length array[x].

            When you have reached the end of the file, just display the line array according to the lengths sotred in the length array. You might even sort the length array first.

            Comment

            • shashankraj1231
              New Member
              • May 2010
              • 13

              #7
              Originally posted by weaknessforcats
              You are reading a line into an array of char.

              The \n should be converted to a \0 in the array so you have a string.

              Just display the array to diaplsy the line.

              As to displaying by the length, I would
              a) create an array of unsigned int for the lengths of each line.
              b) create a corresponding array of char* for the lines. c) The line array[x] will correspond to length array[x]

              d)call strlen on your array and allocate memory for strlen+ 1 bytes. Then use strcpy and copy your array to the allocation.
              e) copy the address of the allocation to your line array[x]
              f) copy the strlen value for this allocation to length array[x].

              When you have reached the end of the file, just display the line array according to the lengths sotred in the length array. You might even sort the length array first.
              wow, this looks promising. Let me try that and i shall generate the code for the same as well and see if this works.

              Comment

              • vrsub
                New Member
                • Feb 2012
                • 1

                #8
                Hi .. try this code.
                One prerequisite is, evry line in the file should end with a (.)dot. Also, i ve printed lines with atleast two vowels. U can modify it as per ur wish


                #include<stdio. h>
                int isvowel(char);
                void main()
                {
                FILE *out,*in,*fptr;
                int sum=0,j,cnt=0,i =0,line=0;
                long choice,option,r ollno,sex,marks ;
                char file_name[30];
                char c;
                printf("Enter the file name with extension..");
                scanf("%s",&fil e_name);
                out=fopen("file _test.txt","w") ;
                if(fptr=fopen(f ile_name,"r"))
                {
                printf("File contents are..\n\n");
                while((c=fgetc( fptr))!=EOF)
                {
                printf("%c",c);
                }
                fclose(fptr);
                }
                else
                printf("FILE OPEN FAILED");
                choice=0;
                fptr=fopen(file _name,"r");
                while((c=fgetc( fptr))!=EOF)
                {
                if(c=='\n')
                {
                if(sum<2){
                choice=ftell(fp tr);
                fseek(out,1L,SE EK_CUR);
                putc('\n',out);
                }
                sum=0;
                }
                i=isvowel(c);
                if(i==1)
                {
                sum+=1;
                }
                if(sum>=2)
                {
                option=ftell(fp tr);
                fseek(fptr,choi ce-option,SEEK_CUR );
                while((c=fgetc( fptr))!='.')
                {
                putc(c,out);
                }
                choice=ftell(fp tr);
                }
                }
                fclose(fptr);
                fclose(out);
                in=fopen("file_ test.txt","r");
                if(in==NULL)
                printf("File open failed..");
                else
                {
                printf("\n\nThe contents are..\n");
                while((c=fgetc( in))!=EOF)
                printf("%c",c);
                }
                fclose(in);
                getch();
                }
                int isvowel(char c)
                {
                if(c=='a'||c==' A'||c=='e'||c== 'E'||c=='i'||c= ='I'||c=='o'||c =='O'||c=='u'|| c=='U')
                return 1;
                else
                return 0;

                Comment

                • shashankraj1231
                  New Member
                  • May 2010
                  • 13

                  #9
                  Not exactly what I wanted but that'll do. Thanks for the reply.

                  Comment

                  Working...