| | |
Reading and Writing file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
Can anyone help me...i have a file modt.txt and i want to save the content in modt2.txt but it seems my code is not working. Moreover,
if i have the content of the 1st file Have a nice day. i want to save it to the 2nd file without double space it should be Have a nice day. I have this as my code:
if i have the content of the 1st file Have a nice day. i want to save it to the 2nd file without double space it should be Have a nice day. I have this as my code:
C Syntax (Toggle Plain Text)
#include <stdio.h> void copyf(char *fname1, char *fname2){ FILE *fp1,*fp2; int x; if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)) { printf("error:unable to open the filed\n"); exit(1); } while((x=getc(fp1))!=EOF) putc(x,fp2); fclose(fp1); fclose(fp1); } main(){ copyf("modt.txt","modt2.txt"); }
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
im sory the first txt file should have a content of "Have_ _ a nice day" two spaces then i have to save it to the 2nd file and should only have one spcae "Have_a nice day"...
C Syntax (Toggle Plain Text)
while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } }
Last edited by andor; Oct 2nd, 2006 at 4:51 am.
If you want to win, you must not loose (Alan Ford)
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
Of course you must to define spaceOcured at the begining of copyf function, for example to char and intialise it to zero.C Syntax (Toggle Plain Text)
while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } }
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
i got it i initialize spaceOccured as char
char spaceOcured=0;
with this code whats wrong?:
C Syntax (Toggle Plain Text)
#include <stdio.h> char spaceOcured; void copyf(char *fname1, char *fname2){ FILE *fp1,*fp2; int x; spaceOcured=0; if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)) { printf("error:unable to open the filed\n"); exit(1); } while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } } fclose(fp1); fclose(fp1); } main(){ copyf("d:\modt.txt","d:\modt2.txt"); getch(); }
Hm are you shore?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> void copyf(char *fname1, char *fname2){ FILE *fp1,*fp2; int x; char spaceOcured = 0; if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)){ printf("error:unable to open the filed\n"); exit(1); } while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } } fclose(fp1); fclose(fp1); } int main(){ copyf("modt.txt","modt2.txt"); return 0; }
If you want to win, you must not loose (Alan Ford)
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
Hm are you shore?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> void copyf(char *fname1, char *fname2){ FILE *fp1,*fp2; int x; char spaceOcured = 0; if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)){ printf("error:unable to open the filed\n"); exit(1); } while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } } fclose(fp1); fclose(fp1); } int main(){ copyf("modt.txt","modt2.txt"); return 0; }
The only diffrence is the while loop and the declaration of spaceOcured. There is no sense that mod2 is not created. Create a new dir and copy the c file and the modt.txt. Try again. It should create the file modt2.txt. Do you heve this error message
error:unable to open the filed? If U still have problem comment the while loop and leave only the fopen functions and see the result. Last edited by andor; Oct 2nd, 2006 at 6:03 am.
If you want to win, you must not loose (Alan Ford)
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Reading numbers from a text file (Java)
- C++ Reading from a text file (C++)
- What errors can occur while reading a file? (C)
- Reading specific line in a file. / Searching (C++)
- Read/write to same file > once, Help (C++)
- Reading binary data from a file and writing it (Visual Basic 4 / 5 / 6)
- writing to a file (Java)
- file input and output (C)
Other Threads in the C Forum
- Previous Thread: Binary Search of an array of three strings
- Next Thread: passing strings
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks binarysearch calculate changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking hardware histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi





