473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 7753
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,208 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

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

Similar topics

2
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:
6
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...
5
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...
1
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
1
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!
17
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...
22
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...
2
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...
4
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.