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 : - 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 : - #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..
12 7712
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.
can you explain using your code??
i like to see your code using fgetc..
thanks..
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.
Another way to solve the first question would be like this:
read the guidelines, no full code solutions.
mattmao-
Please check your Private Messages, accessible through the PM's link in the top right corner of the page.
Thanks
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
summy:
Please do not put links in your posts. They are a virus source and cause our browsers to warn us of possible infection.
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.
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
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".
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Pernell Williams |
last post by:
Hi all:
Thank you for your responses. I have a more specific question about
"file.seek() and file.readline()" versus "file.seek() and file.xreadlines".
When I have the following code:
|
by: Ruben |
last post by:
Hello.
I am trying to read a small text file using the readline statement. I
can only read the first 2 records from the file. It stops at the blank
lines or at lines with only spaces. I have a...
|
by: wolfgang haefelinger |
last post by:
Greetings,
I'm trying to read (japanese) chars from a file. While doing so
I encounter that a char with length 2 is returned. Is this to be
expected or is there something wrong?
Basically...
|
by: asdsd sir |
last post by:
Hi!I'm new in Python and i'd like to ask some general questions about
stdin,stdout...
Firstly...
if we type like something like :
cat "file.txt"|python somefile.py
#somefile.py
import sys
|
by: Jose Reckoner |
last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script
to convert .DT1 and .DEM data to ASCII or some other format? I don't
need a viewer.
Thanks!
|
by: Eric_Dexter |
last post by:
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename + "orc"
for line in alllines:
if not...
|
by: Sandman |
last post by:
So, I have this content management system I've developed myself. The
system has a solid community part where members can register and then
participate in forums, write weblogs and a ton of other...
|
by: js |
last post by:
Hi list.
I'm writing a tail -f like program in python
and I found file.read() doesn't work as I think it should.
Here's the code illustrating my problem.
###
#!/usr/bin/env python
import...
|
by: OhKyu Yoon |
last post by:
Hi!
I have a really long binary file that I want to read.
The way I am doing it now is:
for i in xrange(N): # N is about 10,000,000
time = struct.unpack('=HHHH', infile.read(8))
# do...
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |