Reading all pair lines from a file and then putting them in another file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Reading all pair lines from a file and then putting them in another file

 
0
  #1
Oct 22nd, 2009
Hey

I have a text file (1.txt) with the lines:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

and Im trying to read all the pair lines from that file and write them to another text file called 2.txt which would result in this:

This is line 2
This is line 4

My code works but the problem is at the end it runs into a infinite loop and the result is

This is line 2
This is line 4
This is line 5
This is line 5
This is line 5
This is line 5
This is line 5
This is line 5
This is......

until I manually stop the program. I imagine there is something wrong with my EOF loop and/or the length I set. Also maybe the program doesnt know it should jump to the next line when it reads a /n or something. The variables are in spanish but I THINK they could be understood. If not, Ill translate

  1. #include <assert.h>
  2. #include <complex.h>
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fenv.h>
  6. #include <float.h>
  7. #include <inttypes.h>
  8. #include <iso646.h>
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <math.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include <stdarg.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <wchar.h>
  23. #include <wctype.h>
  24.  
  25. int main()
  26. {
  27. FILE *archivopar;
  28. FILE *archivocopiado;
  29. char texto[50];
  30. int i=1;
  31.  
  32. archivopar=fopen("archivopar.txt","r");
  33. archivocopiado=fopen("archivocopiado.txt","w");
  34.  
  35.  
  36. if ((archivopar==NULL) || (archivocopiado==NULL))
  37. {
  38. printf ("No se puede abrir algun archivo!");
  39. return 1;
  40. }
  41. else
  42. {
  43. do{
  44. fgets(texto,50,archivopar);
  45. if (i % 2 == 0)
  46. {
  47. fprintf(archivocopiado,"%s\n",texto);
  48. }
  49. if (archivopar!=EOF)
  50. {
  51. i=i+1;
  52. }
  53. }while(archivopar!=EOF);
  54. fclose(archivopar);
  55. fclose(archivocopiado);
  56. return 0;
  57. }
  58. }

I know its problably something stupid but I cant figure it out. Thanks for the help.
Last edited by riahc3; Oct 22nd, 2009 at 9:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,413
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #2
Oct 22nd, 2009
Change
        do{
            fgets(texto,50,archivopar);
            if (i % 2 == 0)
            {
                fprintf(archivocopiado,"%s\n",texto);
            }
            if (archivopar!=EOF)
            {
                i=i+1;
            }
        }while(archivopar!=EOF);
to
        while(fgets(texto,sizeof texto,archivopar))
         {
            if (i % 2 == 0)
            {
                fprintf(archivocopiado,"%s\n",texto);
            }
            if (archivopar!=EOF)
            {
                i=i+1;
            }
        }
...more or less.
Last edited by Dave Sinkula; Oct 22nd, 2009 at 10:58 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 22nd, 2009
Originally Posted by Dave Sinkula View Post
Change
        do{
            fgets(texto,50,archivopar);
            if (i % 2 == 0)
            {
                fprintf(archivocopiado,"%s\n",texto);
            }
            if (archivopar!=EOF)
            {
                i=i+1;
            }
        }while(archivopar!=EOF);
to
        while(fgets(texto,sizeof texto,archivopar))
         {
            if (i % 2 == 0)
            {
                fprintf(archivocopiado,"%s\n",texto);
            }
            if (archivopar!=EOF)
            {
                i=i+1;
            }
        }
...more or less.

Thanks but now its just weird lol....

It copies in the file only the forth line and the seventh line...
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,413
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #4
Oct 23rd, 2009
Yup. It has to do with the incrementing of i. Why would it need to be conditional as you have it?

[edit]I suppose I should have pointed that out earlier. I guess that chunk of my play code didn't tag along with my post. I'd trimmed down unused headers and other things -- I tend to get carried away sometimes.
Last edited by Dave Sinkula; Oct 23rd, 2009 at 12:31 am.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 76
Reputation: Gaiety is an unknown quantity at this point 
Solved Threads: 2
Gaiety's Avatar
Gaiety Gaiety is offline Offline
Junior Poster in Training
 
0
  #5
Oct 23rd, 2009
Originally Posted by riahc3 View Post
Hey

I have a text file (1.txt) with the lines:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

and Im trying to read all the pair lines from that file and write them to another text file called 2.txt which would result in this:

This is line 2
This is line 4

My code works but the problem is at the end it runs into a infinite loop and the result is

This is line 2
This is line 4
This is line 5
This is line 5
This is line 5
This is line 5
This is line 5
This is line 5
This is......

until I manually stop the program. I imagine there is something wrong with my EOF loop and/or the length I set. Also maybe the program doesnt know it should jump to the next line when it reads a /n or something. The variables are in spanish but I THINK they could be understood. If not, Ill translate

  1. #include <assert.h>
  2. #include <complex.h>
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fenv.h>
  6. #include <float.h>
  7. #include <inttypes.h>
  8. #include <iso646.h>
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <math.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include <stdarg.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <wchar.h>
  23. #include <wctype.h>
  24.  
  25. int main()
  26. {
  27. FILE *archivopar;
  28. FILE *archivocopiado;
  29. char texto[50];
  30. int i=1;
  31.  
  32. archivopar=fopen("archivopar.txt","r");
  33. archivocopiado=fopen("archivocopiado.txt","w");
  34.  
  35.  
  36. if ((archivopar==NULL) || (archivocopiado==NULL))
  37. {
  38. printf ("No se puede abrir algun archivo!");
  39. return 1;
  40. }
  41. else
  42. {
  43. do{
  44. fgets(texto,50,archivopar);
  45. if (i % 2 == 0)
  46. {
  47. fprintf(archivocopiado,"%s\n",texto);
  48. }
  49. if (archivopar!=EOF)
  50. {
  51. i=i+1;
  52. }
  53. }while(archivopar!=EOF);
  54. fclose(archivopar);
  55. fclose(archivocopiado);
  56. return 0;
  57. }
  58. }

I know its problably something stupid but I cant figure it out. Thanks for the help.
i dont know spanish but i hope u understand this..

the segments

  1. if (archivopar!=EOF)

and
  1. while(archivopar!=EOF);

are wrong.
archivopar is a FILE pointer and EOF is -1 these will never get equal.
i mean the comparision is wrong.

fgets returns the address of the next line read. And incase if there is no line i.e whenit reaches EOF, it returns the NULL, so you better compare the return address of the fgets to check whether it reached EOF.
NOTE:
fgets read atmost size -1(here 50 -1 = 49) no.of characters including '\n'. and '\n' will be placed in the array when read characters are less than size-1.

  1. char *temp;
  2.  
  3. temp=fgets(texto,50,archivopar);
  4. if (temp!=NULL)
  5. while(temp!=NULL);

it will work with the above changes.

copies only line with even numbers.
Minds are like parachutes - they only work when they are open
Gaiety
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training
 
0
  #6
Oct 23rd, 2009
Gaiety I did not understand what you mean/said so here is the code translated:

  1. #include <assert.h>
  2. #include <complex.h>
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fenv.h>
  6. #include <float.h>
  7. #include <inttypes.h>
  8. #include <iso646.h>
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <math.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include <stdarg.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <wchar.h>
  23. #include <wctype.h>
  24.  
  25.  
  26.  
  27.  
  28. int main()
  29. {
  30. FILE *filepairs; //This pointer points to the file that contains the "this is line 1" etc
  31. FILE *filecopied; //This pointer points to where the lines will be copied to
  32. char text[50]; //A string array
  33. int i=1; //a counter
  34.  
  35. /* Opening the files */
  36. filepairs=fopen("filepairs.txt","r");
  37. filecopied=fopen("filecopied.txt","w");
  38.  
  39.  
  40. if ((filepairs==NULL) || (filecopied==NULL))
  41. {
  42. printf ("One of the files cannot be opened!");
  43. return 1;
  44. }
  45. else
  46. {
  47. while (fgets(text,sizeof text,filepairs))
  48. {
  49. fgets(text,50,filepairs);
  50. if (i % 2 == 0)
  51. {
  52. fprintf(filecopied,"%s\n",text);
  53. }
  54. if (filepairs!=NULL)
  55. {
  56. i=i+1;
  57. }
  58. }
  59. fclose(filepairs);
  60. fclose(filecopied);
  61. return 0;
  62. }
  63. }

I hope this clears it up for everyone. Thanks to everyone for trying to help out
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,413
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #7
Oct 23rd, 2009
Just increment i for every line read, right?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 76
Reputation: Gaiety is an unknown quantity at this point 
Solved Threads: 2
Gaiety's Avatar
Gaiety Gaiety is offline Offline
Junior Poster in Training
 
0
  #8
Oct 23rd, 2009
Gaiety I did not understand what you mean/said so here is the code translated:
try your code with this input:

(i)
filepairs.txt:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7

you will get out put as :
This is line 4

(ii)
filepairs.txt:
This is line 0
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6

you will get out put as :
This is line 3
This is line 7

Find out whats worng

please explain when the belowstatement becomes false and what is the need of that.

  1.  
  2. if (filepairs!=NULL)

I hope this clears it up for everyone. Thanks to everyone for trying to help out
the program is not correct yet....!
Minds are like parachutes - they only work when they are open
Gaiety
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC