S A
I don't really understand, what are u going to do after finding the underscore,
are u going to ignore the rest of the line and start fetching a new line? I
edited the code under the above assumption, but haven't tried to compile it.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp1, *fp2;
char ch;
clrscr();
fp1=fopen("11.txt","r"); //the text file from which I have to extract the
string.
if(fp1==NULL)
{
printf("\n File not found");
exit(0);
}
fp2=fopen("12.txt", "w"); // the file into which I have to write the
extracted string
while(!feof(fp1))
{
ch=fgetc(fp1); //the loop which extracts the character till _ is reached
int j=1;
if(ch!='_')
{
fputc(ch, fp2);
j=0;// j will be 0 if readen character is not underscore
}
if(j!=0) //underscore was found and consecutive char. till enter will not
be written into the file
do
{ ch=fgetc(fp1);}
while (ch!='\n')
//writing enter in the output file to start a new line
if(ch=='\n')
{fputc(ch, fp2);}
}
printf("\n Successfully completed...");
fclose(fp1);
//fclose(fp2);
getch();
return 0;
}
pushkar raj <[EMAIL PROTECTED]> wrote: Hi
everybody,
I have a text file which contains a sequence of characters terminated by
newline.
I need to extract a sequence of character from the file until i encounter
underscore character.
The moment I encounter underscore character, I need to move to the next line.
This process is to be repeated till I reach the end of file.
The code that I have been trying is given below.....
The problem with this code is that the loop doesn't terminate.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp1, *fp2;
char ch;
clrscr();
fp1=fopen("11.txt","r"); //the text file from which I have to extract the
string.
if(fp1==NULL)
{
printf("\n File not found");
exit(0);
}
fp2=fopen("12.txt", "w"); // the file into which I have to write the
extracted string
while(!feof(fp1))
{
ch=fgetc(fp1); //the loop which extracts the character till _ is reached
if(ch!='_')
{
fputc(ch, fp2);
}
else if(ch!='\n') //the loop which runs till new line is reached, this loop
doesn't terminate.
{
ch=fgetc(fp1);
}
}
printf("\n Successfully completed...");
fclose(fp1);
//fclose(fp2);
getch();
return 0;
}
Please suggest an improvement on this piece of code or debug the logical error
in the code.
The sample of file is as shown......
pus||hkar_sjs
pu#|shkar2001_sjs
I need only the sequence of character upto _..
Rest of the characters are not be copied into the other file.
---------------------------------
Win a BlackBerry device from O2 with Yahoo!. Enter now.
[Non-text portions of this message have been removed]
---------------------------------
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.
[Non-text portions of this message have been removed]