#include<stdio.h>
#include<conio.h>

int main()
{
    FILE *p,*p2;
    char c;
    long l;
    long x;
    p=fopen("file1.txt","w");

    printf("enter the content of file:");
    while(c=getchar()!=EOF)
    {
           putc(c,p);
    }
    fclose(p);
    p=fopen("file1.txt","r");
  //  l=fseek(p,1L,2);
  l=ftell(p);
    p2=fopen("file2.txt","r");
    c=getc(p);
    while(l!=-1)
    {
           if(c==' ')
           {
                   x=ftell(p);
                  //  fseek(p,-1L,1);       
                           while(c=getc(p)!=' ')
                           {
                                 putc(c,p2);
                                 fseek(p,-2L,1);                          
                           }
                           
                        //   fseek(p,x+1,0);
           }
           fseek(p,x,0);
           l=ftell(p);
           c=getc(p);
    }
    fclose(p);
  //  fclose(p2);
    
    p=fopen("file2.txt","r");
    while(c=getc(p)!=EOF)
    {
           printf("%c",c);
    }
    fclose(p);
    getch();
}

it is not writing anything to file2.txt. what is the problem? it is compiling correctly.it is writing to file1.txt but not to the file2.txt. why is it so ???

in this, i am reversing each word of the file1.txt and then writing it to the file2.txt. :'(

Edited 4 Years Ago by gourav1: n/a

21 : p2=fopen("file2.txt","r");

You are opening it in read mode and trying to write

it is still not writing. i have changed it as u said.when i enter a string like "hello i m aman garg " and then i press "enter" and then i presss ctrl+Z(for EOF) and then it doesn't print anything and stops and doesn't even exit from the program which it must be as per coded.:'( help!!

Edited 4 Years Ago by gourav1: n/a

The code breaks the loop when ftell(p) returns -1. At what circumstances do you expect this to happen?

The code breaks the loop when ftell(p) returns -1. At what circumstances do you expect this to happen?

when p reaches end of file , then it (ftell) will return -1 ,so at that time loop breaks. is it right or wrong ?

Wrong. Reaching end of file is not an error.

then tell me the solution please ? correct it .. how to check the end of file then ?? but i have read that when end of file is reached then ftell returns -1 which means that end of file is reached. i have read it in a book. i m not saying on my own.

Edited 4 Years Ago by gourav1: n/a

The code breaks the loop when ftell(p) returns -1. At what circumstances do you expect this to happen?

i m using now c=getchar()!=EOF in place of l!=-1. still now not wiritng to file. hey when i m giving input i m giving like this.

"hello i m aman aggarwal"-->[enter pressed]--> then ctrl+'Z'. but if i do like this--> "hello i m aman aggarwal" and along with it i press then ctrl+'Z' then the input loop don't get terminates after pressing enter key.i have to enter press enter again and then again press ctrl+Z then enter and finally input loop terminates. so what is this problem ? should i need to press enter after entering string and then ctrl+Z and then enter to terminate the loop(input)?

Edited 4 Years Ago by gourav1: n/a

We can't see your screen to see what the code looks like now.

#include<stdio.h>
#include<conio.h>

int main()
{
    FILE *p,*p2;
    char c;
    long l;
    long x;
    int m=0;
    p=fopen("file1.txt","w");
    char d[50];
    char t;
    printf("enter the content of file:");
    gets(d);
    t=d[0];
   while(t!='\0')
    {      
           putc(t,p);
           m++;
           t=d[m];
    }
    fclose(p);
    p=fopen("file1.txt","r");
  //  l=fseek(p,1L,2);
  l=ftell(p);
    p2=fopen("file2.txt","w");
    c=getc(p);
    while(c!=EOF)
    {
           if(c==' ')
           {
                   x=ftell(p);
                    fseek(p,-2L,1);       
                           while(c=getc(p)!=' ')
                           {
                                 putc(c,p2);
                                 fseek(p,-2L,1);                          
                           }
                           
                           putc(' ',p2);
                           
                        //   fseek(p,x+1,0);
           }
           fseek(p,x,0);
           l=ftell(p);
           c=getc(p);
    }
    fclose(p);
    fclose(p2);
    
    p=fopen("file2","r");
    while(c=getc(p)!=EOF)
    {
           printf("%c",c);
    }
    fclose(p);
    getch();
}

this is edited code the changes i have made as mentioned in last post.

Edited 4 Years Ago by gourav1: n/a

so what is this problem ?

Lack of understanding about how the command prompt in Windows works. Yes, you need to type Ctrl+Z on a line by itself for EOF to be properly signaled in your program.

Please explain what this section of code is doing and why? Line by line...

if(c==' ')
           {
                   x=ftell(p);
                    fseek(p,-2L,1);       
                           while(c=getc(p)!=' ')
                           {
                                 putc(c,p2);
                                 fseek(p,-2L,1);                          
                           }
                           
                           putc(' ',p2);
                           
                        //   fseek(p,x+1,0);
           }
           fseek(p,x,0);
           l=ftell(p);

Lack of understanding about how the command prompt in Windows works. Yes, you need to type Ctrl+Z on a line by itself for EOF to be properly signaled in your program.

means which case? at the end of string entered or after the enter pressed and then press ctrl+Z ? which case u are saying ?

Please explain what this section of code is doing and why? Line by line...

if(c==' ')
           {
                   x=ftell(p);
                    fseek(p,-2L,1);       
                           while(c=getc(p)!=' ')
                           {
                                 putc(c,p2);
                                 fseek(p,-2L,1);                          
                           }
                           
                           putc(' ',p2);
                           
                        //   fseek(p,x+1,0);
           }
           fseek(p,x,0);
           l=ftell(p);

okies! see it now! first c is checking that if the character is a space. if it is so, then,i am storing at which place my pointer was that at that time because i need to come back at this later on.as after reading one character pointer shifts by 1, so by fseek, i am shifting it to character which is just adjacent to the space(assuming space given between each word is 1).then i am taking my pointer back till previous space come back(as this tell that word ends here).after fseek, then i am reading each character till space and writing it to the other file .and again with fseek, i am shifting pointer 2 steps back because it shifts forward but i need to move backward.so when i read space again, loop ends. and the position which we have stored earlier is used to make the position of pointer back. and that's how loop is going till end of file is there.

Edited 4 Years Ago by gourav1: n/a

okies! see it now! first c is checking that if the character is a space. if it is so, then,i am storing at which place my pointer was that at that time because i need to come back at this later on.as after reading one character pointer shifts by 1, so by fseek, i am shifting it to character which is just adjacent to the space(assuming space given between each word is 1).then i am taking my pointer back till previous space come back(as this tell that word ends here).after fseek, then i am reading each character till space and writing it to the other file .and again with fseek, i am shifting pointer 2 steps back because it shifts forward but i need to move backward.so when i read space again, loop ends. and the position which we have stored earlier is used to make the position of pointer back. and that's how loop is going till end of file is there.

What a convoluted piece of illogical code.

Read the characters.
Put them in a buffer.
Use the buffer to do all that crap. Don't do it in the file!

If you get a non-space, add it to the buffer.
If you get a space, your buffer contains a word.

What a convoluted piece of illogical code.

Read the characters.
Put them in a buffer.
Use the buffer to do all that crap. Don't do it in the file!

If you get a non-space, add it to the buffer.
If you get a space, your buffer contains a word.

okies i admit my code is illogical. now tell me the logic to build this rather than laughing on me ?okies tell me the best way to do it. it must be done with file handling ok ? now tell me the logic and the way to do this question. thanks in advance. any kind of help will be appreciated(including laughing and making fun :-)).

now tell me the logic to build this rather than laughing on me ?

There was more to Walt's post, you should read it rather than shooting back with a question that was already answered.

There was more to Walt's post, you should read it rather than shooting back with a question that was already answered.

writing only 2 lines for a problem doesn't tell everything which i want. u can help me by telling the proper way of doing this question. i am just a beginner. i don't understand like you because you all are a high-fi professional in C.

writing only 2 lines for a problem doesn't tell everything which i want. u can help me by telling the proper way of doing this question. i am just a beginner. i don't understand like you because you all are a high-fi professional in C.

Sorry dude, but at some point you'll need to engage your brain. We didn't become "hi-fi professionals" in C by expecting other people to do our thinking for us. You'll get better results by trying something and then asking a smart question about something specific rather than the tired old "I'm a beginner, tell me how to do XYZ".

Edited 4 Years Ago by Narue: n/a

Sorry dude, but at some point you'll need to engage your brain. We didn't become "hi-fi professionals" in C by expecting other people to do our thinking for us. You'll get better results by trying something and then asking a smart question about something specific rather than the tired old "I'm a beginner, tell me how to do XYZ".

huhh! okies! i really don't know that you are high-fi professional. i was joking. but i never expect that my joke is actually a reality. woo! hats off then! it's nice to meet you here. and geeting a link like smart.html rather than answer of my question. phew!! tiring!!
thanks for help!!

#include<stdio.h>
#include<conio.h>

int main()
{
    FILE *p,*p2;
    char c;
    long l;
    long x;
    int m=0;
    p=fopen("file1.txt","w");
    char d[50];
    char t;
    printf("enter the content of file:");
   // gets(d);
   // t=d[0];
   while(c=getchar()!=EOF)
    {      
           putc(c,p);
           //m++;
         //  t=d[m];
    }
    fclose(p);
    p=fopen("file1.txt","r");
  //  l=fseek(p,1L,2);
  l=ftell(p);
    p2=fopen("file2.txt","w");
    if(p2==NULL)
    exit(0);
    //c=getc(p);
    while(c=getc(p)!=EOF)
    {
          /* if(c==' ')
          {
                  x=ftell(p);
                    fseek(p,-2L,1);*/       
                  /*         while(c=getc(p)!=' ')
                           {
                                 putc(c,p2);
                                 fseek(p,-2L,1);                          
                           }
                           
                           putc(' ',p2);
                           
                        //   fseek(p,x+1,0);
           }
           fseek(p,x,0);
           l=ftell(p);
           c=getc(p);*/
           putc(c,p2);
           
    }
    fclose(p);
    fclose(p2);
    
    p=fopen("file2.txt","r");
    while(c=getc(p)!=EOF)
    {
           printf("%c",c);
    }
    fclose(p);
    getch();
}

it is a very simple program of file handling. it is not writing anything to any file. it is making file as the desired place but not writing anything to it. please tell me what is the problem.?

Let's start with this:

while(c=getchar()!=EOF)

You're missing parens around c=getchar() , which means that the expression is evaluated as c = (getchar() != EOF) rather than (c = getchar()) != EOF .

Let's start with this:

You're missing parens around c=getchar() , which means that the expression is evaluated as c = (getchar() != EOF) rather than (c = getchar()) != EOF .

ya! it is working now!

Edited 4 Years Ago by gourav1: n/a

Let's start with this:

You're missing parens around c=getchar() , which means that the expression is evaluated as c = (getchar() != EOF) rather than (c = getchar()) != EOF .

#include<stdio.h>
#include<conio.h>

int main()
{
    FILE *p,*p2;
    char c;
    long l;
    long x;
    int m=0,f=0;
    p=fopen("file1.txt","w");
    char d[50];
    char t;
    printf("enter the content of file:");
   
   while((c=getchar())!=EOF)
    {      
           putc(c,p);
    }
    fclose(p);
    p=fopen("file1.txt","r");
    if(p==NULL)
    exit(0);
    p2=fopen("file2.txt","w");
    if(p2==NULL)
    exit(0);
  
    while((c=getc(p))!=EOF)
    {
          if(c==' ')
          {
                  x=ftell(p);
                    fseek(p,-2L,1);       
                           while(((c=getc(p))!=' ') && (f!=m))
                           {
                                                
                                 putc(c,p2);
                                 fseek(p,-2L,1);
                                 f++;                          
                           }
                           
                           putc(' ',p2);
                           fseek(p,x,0);
                           f=0;
                        
           }
            m++;
  }
    fclose(p);
    fclose(p2);
    
    p=fopen("file2.txt","r");
    while((c=getc(p))!=EOF)
    {
           printf("%c",c);
    }
    fclose(p);
    getch();
}

my reverse string code is working now. but there is problem. when i don't give a space at the end of my input(any) , then it don't print the last print only, but without this everything is normal. but when i give space at the end of input, then it works perfectly. please tell me how to remove the restriction of space at the end of input. please tell...

Edited 4 Years Ago by gourav1: n/a

Let's start with this:

You're missing parens around c=getchar() , which means that the expression is evaluated as c = (getchar() != EOF) rather than (c = getchar()) != EOF .

#include<stdio.h>
#include<conio.h>

int main()
{
    FILE *p,*p2;
    char c,h;
    long l,g=0L;
    long x;
    int m=0,f=0;
    p=fopen("file1.txt","w");
    char d[50];
    char t;
    printf("enter the content of file:");
   
   while((c=getchar())!=EOF)
    {      
           putc(c,p);
    }
    fclose(p);
    p=fopen("file1.txt","r");
    if(p==NULL)
    exit(0);
    p2=fopen("file2.txt","w");
    if(p2==NULL)
    exit(0);
  
    while((c=getc(p))!=EOF)
    {
          if(c==' ')
          {
                  x=ftell(p);
                    fseek(p,-2L,1);       
                           while(((c=getc(p))!=' ') && (f!=m))
                           {
                                                
                                 putc(c,p2);
                                 fseek(p,-2L,1);
                                 f++;                          
                           }
                           
                           putc(' ',p2);
                           fseek(p,x,0);
                           f=0;
                           m=0;
                        
           }
            m++;
           
     }
   m=m-2;
          [B][I][U]fseek(p,-3L,2);[/U][/I][/B]
     
           f=0;
                           while((f!=m))
                           {
                                 c=getc(p);         
                                 putc(c,p2);
                                 fseek(p,-2L,1);                        
                                 f++;
                           }
            
          
    fclose(p);
    fclose(p2);
    
    p=fopen("file2.txt","r");
    while((c=getc(p))!=EOF)
    {
           printf("%c",c);
    }
    fclose(p);
    getch();
}

my question is that why i have to do "-3L" as to make the loop reverse again? it must be "-1L" only. i think the problem is with input. please tell me that:

input: hello i am aman[enter]
[ctrl+Z][enter]

2nd input: hello i am aman[ctrl+Z][enter]
[ctrl+Z][enter]

3rd input: hello i am aman[ctrl+Z][enter]

these are two types of inputs when input loops terminates(1st and 2nd). i think i am doing -3L(i have underlined that line above about what i am talking here) because one character is newline and one is EOF ,so i need to do 3 times backward. but if i do second type of input, then loop don't get terminates. i have to again enter ctrl+Z,then terminates as shown.but i want to end my input loop when i do input like in 3rd case but it don't get terminates. why is it so ? and also tell me the solution of this ? please!! thanks in advance!!

Edited 4 Years Ago by gourav1: n/a

This article has been dead for over six months. Start a new discussion instead.