473,394 Members | 1,769 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,394 software developers and data experts.

File handling related question

shashankraj1231
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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. void main()
  6.  {
  7.     FILE *fp;
  8.     char ch[100];
  9.     char chn[50];
  10.     int chk=0;
  11.     char *line;
  12.     char *name;
  13.     clrscr();
  14.     printf("\n Enter the name of the file: ");
  15.     scanf("%s", name);
  16.     fp =fopen(name,"w");
  17.     if(ferror(fp))
  18.     {
  19.     printf("\n Error opening file: ");
  20.     getch();
  21.     exit(1);
  22.     }
  23.  
  24.     /* ENTERING TEXTS INTO THE FILE */
  25.     /*------------------------------*/
  26.  
  27.     printf("\n Enter a few line of texts :");
  28.     printf("\n Enter $ to exit :");
  29.     while(strlen(gets(ch))>0)
  30.     {
  31.     if(ch=="$")
  32.     break;
  33.     else
  34.     {
  35.      fputs(ch, fp);
  36.      fputs("\n",fp);
  37.     }
  38.    }
  39.    fclose(fp);
  40.  
  41.    /*---* Now to look for vowels per line *----*/
  42.    /*------------------------------------------*/
  43.  
  44.    fp= fopen(name,"r");
  45.    while(!feof(fp))
  46.    {
  47.     while(chn!='\n')
  48.     {
  49.        if(chn='a'|chn='A'|chn='e'|chn='E'|chn='i'|chn='I'|chn='o'|chn='O'|chn='u'|chn='U')
  50.        {
  51.        /*stat[chk]++;*/
  52.        chk++;
  53.        line=
  54.     }
  55. /*--------------- i am not sure what to do here ----*/
  56.  fclose(fp);
  57.  getch();
  58.  }
  59.  
  60.  
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.
Jun 23 '10 #1

✓ answered by vrsub

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,rollno,sex,marks;
char file_name[30];
char c;
printf("Enter the file name with extension..");
scanf("%s",&file_name);
out=fopen("file_test.txt","w");
if(fptr=fopen(file_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(fptr);
fseek(out,1L,SEEK_CUR);
putc('\n',out);
}
sum=0;
}
i=isvowel(c);
if(i==1)
{
sum+=1;
}
if(sum>=2)
{
option=ftell(fptr);
fseek(fptr,choice-option,SEEK_CUR);
while((c=fgetc(fptr))!='.')
{
putc(c,out);
}
choice=ftell(fptr);
}
}
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;

8 4386
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. LOOP
  2.    READ A RECORD
  3.    CALL VOWEL FUNCTION
  4.    IF RETURNS 1 PRINT FOUND A VOWEL
  5.    IF RETURNS 0 PRINT NO VOWERL
  6. UNTIL END OF FILE
Jun 23 '10 #2
donbock
2,426 Expert 2GB
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.
Jun 23 '10 #3
@weaknessforcats
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...
Jun 25 '10 #4
@donbock
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.
Jun 25 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Jun 25 '10 #6
@weaknessforcats
wow, this looks promising. Let me try that and i shall generate the code for the same as well and see if this works.
Jun 26 '10 #7
vrsub
1
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,rollno,sex,marks;
char file_name[30];
char c;
printf("Enter the file name with extension..");
scanf("%s",&file_name);
out=fopen("file_test.txt","w");
if(fptr=fopen(file_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(fptr);
fseek(out,1L,SEEK_CUR);
putc('\n',out);
}
sum=0;
}
i=isvowel(c);
if(i==1)
{
sum+=1;
}
if(sum>=2)
{
option=ftell(fptr);
fseek(fptr,choice-option,SEEK_CUR);
while((c=fgetc(fptr))!='.')
{
putc(c,out);
}
choice=ftell(fptr);
}
}
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;
Feb 20 '12 #8
Not exactly what I wanted but that'll do. Thanks for the reply.
Feb 20 '12 #9

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

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
11
by: Josh | last post by:
Hi, I am having a problem with Python. I am new to Python as a programming language, but I do have experience in other languages. I am experiencing strange problems with File handling and wonder...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
9
by: Jay Kim | last post by:
Hi, We're implementing a Windows application using Visual Basic .NET. One of the key features we need to implement is that we should be able to get the accurate byte offset of user selected...
4
by: BHARAT | last post by:
Hi I am a little bit new to C's advanced topics: I need help in file handling. I have Turbo C and I wrote a simple program to write data to file: #include<stdio.h> #include<dir.h>...
7
by: D & J G | last post by:
Is there a way to instruct Windows file-handling - 'Opens with:' - from within VB6? I want to ensure that files with the .rtf (rich text) extension always go to Word Pad, no matter which future...
3
by: Binu C | last post by:
hi am new to VB. I need to develop an application. The requirements are as follows: We have a notepad which consists of some data(Say A). Now we have a form in which we have some text fields & a...
2
by: phpmagesh | last post by:
Hi, I want to create a xml document using php-file handling . that i have a Database named as Total_category and table name as students_one. and i have fields like name, age, email, title,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.