about how to read a file and print it in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • summy
    New Member
    • Dec 2007
    • 3

    about how to read a file and print it in C

    Write a program includes ALL the features:
    1.Read the specify file sample.txt and then print the contents of the file on screen.
    2.Print each lines of the file in reversed order on screen.
    3.Print a table indicating the number of occurrences of each letter of the 4.alphabet in the sample file.
    5.Print a table indicating the number of one-letter words, two-letter words, three- letter words, etc., appearing in the sample file.

    sample.txt is :
    Code:
     I believe that children are our future
    Teach them well and let them lead the way
    Show them all the beauty they posesess inside
    Give them a sense of pride to make it easier
    Let the children's laughter remind us now we used to be
    all he want is like this:

    screenshot

    my code is :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
     
    int main()
    {
     
    	 char text[200];
     
    	FILE *cfPtr;
     
     
    	if(( cfPtr=fopen("sample.txt","r"))==NULL)
    	{printf("file could not be opened\n");
    	}
    	else {
     
     
    	fscanf(cfPtr,"%s",text);
     
     
     
    	printf("%s\n",text);
     
     
     
     
     
    	fclose(cfPtr);
     
    	}
    	return 0;
    }
    but my code only print "I".
    whats wrong with my code.. please help me..
  • mattmao
    New Member
    • Aug 2007
    • 121

    #2
    Hi

    fscanf(cfPtr,"% s",text);

    This means: read a "string" from file content and put it into the variable "text".
    Since a "string" can be terminated by space, tab or return, the fscanf function would stop right after it reaches the space character after the "I"...

    My suggestion:
    Think about using the function fgetc, it reads the file content "character by charactrer", meantime, use other functions to print out this character to standard output, until you reach the EOF.

    That would solve the first question, hope that helps.

    Comment

    • summy
      New Member
      • Dec 2007
      • 3

      #3
      can you explain using your code??
      i like to see your code using fgetc..
      thanks..

      Comment

      • manjuks
        New Member
        • Dec 2007
        • 72

        #4
        I tried with fgetc.

        while((ch = fgetc(cfPtr)) != EOF){
        printf("%c", ch);
        }

        It will print the contents of file. Now try to print it in reverse.

        Comment

        • mattmao
          New Member
          • Aug 2007
          • 121

          #5
          Another way to solve the first question would be like this:
          read the guidelines, no full code solutions.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            mattmao-

            Please check your Private Messages, accessible through the PM's link in the top right corner of the page.

            Thanks

            Comment

            • summy
              New Member
              • Dec 2007
              • 3

              #7
              ok i already solve problem number 1..
              now i try to reverse it.. do you know how yo reverse it?? and count the total letters?
              thanks

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                summy:

                Please do not put links in your posts. They are a virus source and cause our browsers to warn us of possible infection.

                Comment

                • mattmao
                  New Member
                  • Aug 2007
                  • 121

                  #9
                  Originally posted by summy
                  ok i already solve problem number 1..
                  now i try to reverse it.. do you know how yo reverse it?? and count the total letters?
                  thanks
                  For question 2:

                  Divide and conquer.

                  If you can break the big task into smaller ones, say, line by line, then think about how to reverse a line of words. Hints are: fgets would give you the "line of words".

                  For question 3:

                  It is the old counting question again. Your program can count how many times it reads a character by fgetc, after finishing the whole text, check your note to see the total.

                  Comment

                  • Freaky Chris
                    New Member
                    • Dec 2007
                    • 24

                    #10
                    Hi, i may be able to provide some help as to revering the the text.

                    The conecpt it's self is not hard, first off i would set up an array to store the current text, as you are retreiveing the text from the file character by character you will be able to store each one as you go. Once you have reached the end of the file you can then output the text in reverse by simple setting up a loop to print each character to the screen from the array starting at the final entry of the array and working backwards towards myArray[0].

                    And as for the sceond problem simple use and interger that is incremented by 1 each time a character is added to the array

                    I hope this is helpful to you.

                    Chris

                    Comment

                    • mattmao
                      New Member
                      • Aug 2007
                      • 121

                      #11
                      Originally posted by Freaky Chris
                      Hi, i may be able to provide some help as to revering the the text.

                      The conecpt it's self is not hard, first off i would set up an array to store the current text, as you are retreiveing the text from the file character by character you will be able to store each one as you go. Once you have reached the end of the file you can then output the text in reverse by simple setting up a loop to print each character to the screen from the array starting at the final entry of the array and working backwards towards myArray[0].

                      And as for the sceond problem simple use and interger that is incremented by 1 each time a character is added to the array

                      I hope this is helpful to you.

                      Chris
                      Hello Chris:

                      Your logic is good, but there might be a tiny issue to take extra considerrations :
                      You cannot tell the total "length" of the text before reaching the EOF tag, can you? Unless you use malloc to dynamically allocate enough memory space for a huge array, the content of file may sometimes overflow the boundary of the array.

                      Thus personally I believe that it would be better to read the content of the file line by line, which is what function fgets does, then process that "smaller" array and print it out to the standard output.

                      BTW, if you are still worried about the overflow problem, say, declare a char text[200] is not large enough to hold a single line of characters, you may use a stack to get whatever you can have until the '\n', then print them out. They are reversed "automatically" .

                      Comment

                      • Freaky Chris
                        New Member
                        • Dec 2007
                        • 24

                        #12
                        I see where you are coming from matt and i thank you for pointing that out. Readin the file line by line would be more appropriate and in that case could you not use getline, to return an entire line to a variable then use the length of that to declare an array of that size then begin to iterate through the text and store each character to the array.

                        No doubt i have missed another error, but that is proberly due to my lack of experience in programming. however i do hope that my input is of some help.

                        Chris

                        Comment

                        • manjuks
                          New Member
                          • Dec 2007
                          • 72

                          #13
                          We can reverse using fgets. fgets will return a string. then apply reverse function on that string which is returned by fgets. keep this in loop until end of file is reached.

                          Comment

                          Working...