944,145 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 446
  • C RSS
Oct 22nd, 2009
0

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

Expand 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.
Last edited by riahc3; Oct 22nd, 2009 at 9:15 pm.
Similar Threads
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Oct 22nd, 2009
0
Re: Reading all pair lines from a file and then putting them in another file
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 22nd, 2009
0
Re: Reading all pair lines from a file and then putting them in another file
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...
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Oct 23rd, 2009
0
Re: Reading all pair lines from a file and then putting them in another file
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 23rd, 2009
0
Re: Reading all pair lines from a file and then putting them in another file
Click to Expand / Collapse  Quote originally posted by riahc3 ...
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.
Reputation Points: 13
Solved Threads: 3
Junior Poster
Gaiety is offline Offline
115 posts
since Sep 2009
Oct 23rd, 2009
0
Re: Reading all pair lines from a file and then putting them in another file
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
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Oct 23rd, 2009
0
Re: Reading all pair lines from a file and then putting them in another file
Just increment i for every line read, right?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 23rd, 2009
0
Re: Reading all pair lines from a file and then putting them in another file
Quote ...
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)

Quote ...
I hope this clears it up for everyone. Thanks to everyone for trying to help out
the program is not correct yet....!
Reputation Points: 13
Solved Threads: 3
Junior Poster
Gaiety is offline Offline
115 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Function for Intersection of 2 circles
Next Thread in C Forum Timeline: Speeding up Euler Pr 12





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC