468,512 Members | 1,419 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

about how to read a file and print it in C

3
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 :
Expand|Select|Wrap|Line Numbers
  1.  I believe that children are our future
  2. Teach them well and let them lead the way
  3. Show them all the beauty they posesess inside
  4. Give them a sense of pride to make it easier
  5. Let the children's laughter remind us now we used to be
all he want is like this:

screenshot

my code is :
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main()
  5. {
  6.  
  7.      char text[200];
  8.  
  9.     FILE *cfPtr;
  10.  
  11.  
  12.     if(( cfPtr=fopen("sample.txt","r"))==NULL)
  13.     {printf("file could not be opened\n");
  14.     }
  15.     else {
  16.  
  17.  
  18.     fscanf(cfPtr,"%s",text);
  19.  
  20.  
  21.  
  22.     printf("%s\n",text);
  23.  
  24.  
  25.  
  26.  
  27.  
  28.     fclose(cfPtr);
  29.  
  30.     }
  31.     return 0;
  32. }
but my code only print "I".
whats wrong with my code.. please help me..
Dec 18 '07 #1
12 7456
mattmao
121 100+
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.
Dec 18 '07 #2
summy
3
can you explain using your code??
i like to see your code using fgetc..
thanks..
Dec 18 '07 #3
manjuks
72
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.
Dec 18 '07 #4
mattmao
121 100+
Another way to solve the first question would be like this:
read the guidelines, no full code solutions.
Dec 18 '07 #5
sicarie
4,677 Expert Mod 4TB
mattmao-

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

Thanks
Dec 18 '07 #6
summy
3
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
Dec 18 '07 #7
weaknessforcats
9,207 Expert Mod 8TB
summy:

Please do not put links in your posts. They are a virus source and cause our browsers to warn us of possible infection.
Dec 18 '07 #8
mattmao
121 100+
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.
Dec 19 '07 #9
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
Dec 19 '07 #10
mattmao
121 100+
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".
Dec 19 '07 #11
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
Dec 19 '07 #12
manjuks
72
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.
Dec 20 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Pernell Williams | last post: by
5 posts views Thread by wolfgang haefelinger | last post: by
1 post views Thread by Jose Reckoner | last post: by
22 posts views Thread by Sandman | last post: by
4 posts views Thread by OhKyu Yoon | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.