| | |
Reading all pair lines from a file and then putting them in another file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
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
I know its problably something stupid but I cant figure it out. Thanks for the help.
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
C Syntax (Toggle Plain Text)
#include <assert.h> #include <complex.h> #include <ctype.h> #include <errno.h> #include <fenv.h> #include <float.h> #include <inttypes.h> #include <iso646.h> #include <limits.h> #include <locale.h> #include <math.h> #include <setjmp.h> #include <signal.h> #include <stdarg.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <wchar.h> #include <wctype.h> int main() { FILE *archivopar; FILE *archivocopiado; char texto[50]; int i=1; archivopar=fopen("archivopar.txt","r"); archivocopiado=fopen("archivocopiado.txt","w"); if ((archivopar==NULL) || (archivocopiado==NULL)) { printf ("No se puede abrir algun archivo!"); return 1; } else { do{ fgets(texto,50,archivopar); if (i % 2 == 0) { fprintf(archivocopiado,"%s\n",texto); } if (archivopar!=EOF) { i=i+1; } }while(archivopar!=EOF); fclose(archivopar); fclose(archivocopiado); return 0; } }
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.
0
#2 Oct 22nd, 2009
Change
to ...more or less.
do{ fgets(texto,50,archivopar); if (i % 2 == 0) { fprintf(archivocopiado,"%s\n",texto); } if (archivopar!=EOF) { i=i+1; } }while(archivopar!=EOF);
while(fgets(texto,sizeof texto,archivopar)) { if (i % 2 == 0) { fprintf(archivocopiado,"%s\n",texto); } if (archivopar!=EOF) { i=i+1; } }
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
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
0
#3 Oct 22nd, 2009
•
•
•
•
Change
todo{ fgets(texto,50,archivopar); if (i % 2 == 0) { fprintf(archivocopiado,"%s\n",texto); } if (archivopar!=EOF) { i=i+1; } }while(archivopar!=EOF);...more or less.while(fgets(texto,sizeof texto,archivopar)) { if (i % 2 == 0) { fprintf(archivocopiado,"%s\n",texto); } if (archivopar!=EOF) { i=i+1; } }
Thanks but now its just weird lol....
It copies in the file only the forth line and the seventh line...
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.
[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
0
#5 Oct 23rd, 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
C Syntax (Toggle Plain Text)
#include <assert.h> #include <complex.h> #include <ctype.h> #include <errno.h> #include <fenv.h> #include <float.h> #include <inttypes.h> #include <iso646.h> #include <limits.h> #include <locale.h> #include <math.h> #include <setjmp.h> #include <signal.h> #include <stdarg.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <wchar.h> #include <wctype.h> int main() { FILE *archivopar; FILE *archivocopiado; char texto[50]; int i=1; archivopar=fopen("archivopar.txt","r"); archivocopiado=fopen("archivocopiado.txt","w"); if ((archivopar==NULL) || (archivocopiado==NULL)) { printf ("No se puede abrir algun archivo!"); return 1; } else { do{ fgets(texto,50,archivopar); if (i % 2 == 0) { fprintf(archivocopiado,"%s\n",texto); } if (archivopar!=EOF) { i=i+1; } }while(archivopar!=EOF); fclose(archivopar); fclose(archivocopiado); return 0; } }
I know its problably something stupid but I cant figure it out. Thanks for the help.
the segments
C Syntax (Toggle Plain Text)
if (archivopar!=EOF)
and
C Syntax (Toggle Plain Text)
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.
C Syntax (Toggle Plain Text)
char *temp; temp=fgets(texto,50,archivopar); if (temp!=NULL) 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
Gaiety
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
0
#6 Oct 23rd, 2009
Gaiety I did not understand what you mean/said so here is the code translated:
I hope this clears it up for everyone. Thanks to everyone for trying to help out
c Syntax (Toggle Plain Text)
#include <assert.h> #include <complex.h> #include <ctype.h> #include <errno.h> #include <fenv.h> #include <float.h> #include <inttypes.h> #include <iso646.h> #include <limits.h> #include <locale.h> #include <math.h> #include <setjmp.h> #include <signal.h> #include <stdarg.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <wchar.h> #include <wctype.h> int main() { FILE *filepairs; //This pointer points to the file that contains the "this is line 1" etc FILE *filecopied; //This pointer points to where the lines will be copied to char text[50]; //A string array int i=1; //a counter /* Opening the files */ filepairs=fopen("filepairs.txt","r"); filecopied=fopen("filecopied.txt","w"); if ((filepairs==NULL) || (filecopied==NULL)) { printf ("One of the files cannot be opened!"); return 1; } else { while (fgets(text,sizeof text,filepairs)) { fgets(text,50,filepairs); if (i % 2 == 0) { fprintf(filecopied,"%s\n",text); } if (filepairs!=NULL) { i=i+1; } } fclose(filepairs); fclose(filecopied); return 0; } }
I hope this clears it up for everyone. Thanks to everyone for trying to help out
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
0
#8 Oct 23rd, 2009
•
•
•
•
Gaiety I did not understand what you mean/said so here is the code translated:
(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.
C Syntax (Toggle Plain Text)
if (filepairs!=NULL)
•
•
•
•
I hope this clears it up for everyone. Thanks to everyone for trying to help out
Minds are like parachutes - they only work when they are open
Gaiety
Gaiety
![]() |
Similar Threads
- Count Lines Of Text In Txt File (Pascal and Delphi)
- having trouble parsing more than one html file into a csv file (Python)
- reading a certain word from a text file (C#)
- Give me a chequebook balancing program. (C)
- Pass a text file through a batch file (Visual Basic 4 / 5 / 6)
- how to convert mp3 file to wave file? (C++)
- Help me plz File not Found yet file is there (Pascal and Delphi)
- Puzzled with reading multiple lines from file (C++)
- getting data from a text file and putting it in an excel file using visual basic 6.0 (Visual Basic 4 / 5 / 6)
- Reading specific line in a file. / Searching (C++)
Other Threads in the C Forum
- Previous Thread: Function for Intersection of 2 circles
- Next Thread: Speeding up Euler Pr 12
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






